alzami Posted December 11, 2013 Share Posted December 11, 2013 in my previous post i posted a problem about my javascript calculator .in my calculator in made a log button which base is 10;it shows a perfect integer result.say it gives accurately 2 if i want to find the log of 100.but if i want log of 104 it doesn't work;how can i solve that function log10(el){ var m=document.getElementById(el); for(;{ var d=Math.pow(10,i); if(d==m.value){ m.value=i; i=0; break; } i++; }} Link to comment Share on other sites More sharing options...
Ingolme Posted December 11, 2013 Share Posted December 11, 2013 If I remember correctly, you can get the base 10 logarithm out of the natural logarithm by dividing by the logarithm of the base: function log10(n) { return Math.log(n)/Math.LN10; //Math.LN10 is the logarithm of 10.} You can also generalize the function: function log(n,base) { return Math.log(n)/Math.log(base);} Link to comment Share on other sites More sharing options...
alzami Posted December 11, 2013 Author Share Posted December 11, 2013 the problem is if i want log of 100 it gives 2.its not the problem.problem is if i want log of 104 ,it gives me no value.but normal scientific calculator gives value of log(104) .how can i persuade javascript to to such calculation? Link to comment Share on other sites More sharing options...
Ingolme Posted December 11, 2013 Share Posted December 11, 2013 The functions I just gave you solve those equations. function log10(n) { return Math.log(n)/Math.LN10; //Math.LN10 is the logarithm of 10.}alert(log10(104)); // Outputs 2.0170333392987803 Link to comment Share on other sites More sharing options...
alzami Posted December 11, 2013 Author Share Posted December 11, 2013 (edited) Math.log(n)/Math.LN10; is it a complete statement.or it meant i can use Math.log or Math.LN10?i have never encountered such kind of statement. if u see my calculator codes i already used Math.log. it says Math.log is an e based logarithm which is different frm 10 based logarithm Edited December 11, 2013 by alzami Link to comment Share on other sites More sharing options...
Ingolme Posted December 11, 2013 Share Posted December 11, 2013 It's a division, which I explained in a previous post. you can get the base 10 logarithm out of the natural logarithm by dividing by the logarithm of the base If you understand the properties of the logarithm you can obtain this expression. ln(10^N) = N*ln(10) therefore: ln(10^N) / ln(10) = N * ln(10)/ln(10) = N 1 Link to comment Share on other sites More sharing options...
Hadien Posted December 11, 2013 Share Posted December 11, 2013 I've recently used a formula to calculate in log base 10, and you do need to be wary of rounding errors when diving one log by another. for example: log2(100) / log2(10) == log10 (100) == 2 however in javascript Math.log(100) / Math.log(10) will equal something like 1.99999999...due to those floating-point rounding errors. something to be mindful of Link to comment Share on other sites More sharing options...
Ingolme Posted December 11, 2013 Share Posted December 11, 2013 My experiments haven't given any rounding errors up to now. Perhaps there might be if you using a floating point number as the argument., like 0.1 or 0.01. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now