As described in MDN the following polyfill can be used for Math.log10
:
Math.log10 = Math.log10 || function(x) {
return Math.log(x) / Math.LN10;
};
Unfortunately, due to floating point arithmetic (in the non native implementation), this:
Math.log10(1000000)
will result to:
=> 5.999999999999999
One way to get around this issue is to do some rounding:
rounding_const = 1000000;
function log10(n) {
return Math.round(rounding_const * Math.log(n) / Math.LN10) / rounding_const;
}
Is there a better approach (algorithmic?) which will not involve hacky rounding?