Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Math.log10 work on some systems but return undefined on others?

Tags:

javascript

I wrote an html5 canvas based app for plotting the electric potential as a color-map. I was using Math.log10 to re-scale the values and this worked well on quite a few systems (Chrome-Firefox-Opera; laptops and PC; Windows and Ubuntu; integrated and dedicate graphics). And then I found one PC and one laptop both with Windows where the plot would not work. The error was showing that Math.log10() could not be called as a function and just typing Math.log10 in the js console returned undefined. I got around this glitch by replacing Math.log10(someValue) with Math.log(someValue)/2.3. So my questions are: why does this happens and are there any other similar annoying differences?

like image 744
DorinPopescu Avatar asked Feb 03 '15 10:02

DorinPopescu


3 Answers

This is browser-specific. Not all browsers support the experimental Math.log10() function - the main one being Internet Explorer.

Math.log() however is a separate function which was introduced long before Math.log10(), and has much greater browser support.

The Mozilla Developer Network lists browser support for Math.log10():

Desktop Browsers

Chrome    Firefox (Gecko) Internet Explorer   Opera   Safari
38        25 (25)         Not supported       25      7.1

Mobile Browsers

Android         Chrome for Android    Firefox Mobile (Gecko)  IE Mobile      Opera Mobile    Safari Mobile
Not supported   Not supported         25.0 (25)               Not supported  Not supported   iOS 8
like image 166
James Donnelly Avatar answered Nov 13 '22 03:11

James Donnelly


Math.log10 = Math.log10 || function(x) {
  return Math.log(x) * Math.LOG10E;
};

Paste this in your common JS file and this will resolve the issue if log10 will not be supported by browser.

I know it is too late, but it might help if someone will reach your post while searching.

like image 30
Gaurav Nagpal Avatar answered Nov 13 '22 03:11

Gaurav Nagpal


Math.log() is the Napierian logarithm (ln).

So you should use Math.log(value)/Math.log(10) since log10(X) = ln(X)/ln(10)

and Math.log() has much greater browser support as stated in the previous post.

like image 1
Alejandro Gutierrez Avatar answered Nov 13 '22 02:11

Alejandro Gutierrez