Jump to content
Sign in to follow this  
sugan

log

Recommended Posts

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

Share this post


Link to post
Share on other sites

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);}

Share this post


Link to post
Share on other sites
:) 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 --

Share this post


Link to post
Share on other sites

The base change law is the first log law we learn :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...