sugan Posted October 10, 2008 Share Posted October 10, 2008 Hi,I had a problem with math log, i want to calculate20logbase10(0.5)The answer is -6.02when i calculate in calculator, the answer is right, but when i use Math.log it goes wrong, how to calculate this?Regards,Suganya Link to comment Share on other sites More sharing options...
Synook Posted October 10, 2008 Share Posted October 10, 2008 Math.log() returns the natural log. You can use the base change law to get a log in base 10. E.g. 20 * Math.log(0.5) / Math.log(10) function log(n, base) { return Math.log(n) / Math.log(base);} Link to comment Share on other sites More sharing options...
global.user Posted October 10, 2008 Share Posted October 10, 2008 Ooh, the time I typed that, Synook took my place !!! Search a bit further, Suganya !First, I thought: humm, if I can't do it in JS, let's try in php:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled 1</title></head><body><?php$x = log(.5,10);$y = 20*$x;print $y;?></body></html> Then I thought again: why doesn't it work in JS ?The answer is that JS uses natural log, meaning base e.So, you can still do it, more complicated, but it works : <html><body><script type="text/javascript">var x = Math.log(.5)/Math.log(10);var y = 20*x;document.write("<br />" + y); </script></body></html> Use the try-it editor to check it out: -- link to the log examples -- Link to comment Share on other sites More sharing options...
Synook Posted October 10, 2008 Share Posted October 10, 2008 The base change law is the first log law we learn Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.