JavaScript log
Returns the natural logarithm of the number, or the base log if base is specified
1 2 3 4 56 7 8 9 1011 12 13 | function log (arg, base) { // Returns the natural logarithm of the number, or the base log if base is specified // // version: 1109.2015 // discuss at: http://phpjs.org/functions/log // + original by: Onno Marsman // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: log(8723321.4, 7); // * returns 1: 8.212871815082147 return (typeof base === 'undefined') ? Math.log(arg) : Math.log(arg) / Math.log(base); } |
Examples
Running
1 | log(8723321.4, 7); |
Should return
1 | 8.212871815082147 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have log goodness in JavaScript.
Philip: I've checked for the behavior of the already present log function and it seems to already return NaN and -Infinity in the cases you've specified. So I don't see why the extra checks are necessary.
This is the same for your log10 function.
I've added log10 to the repositroy without the checks and credited you for your efforts. It will be visible on this site as soon as Kevin runs his scripts to do so.
Wow, strange, one would think that log() would be the common log and ln() (or something) would be the natural log. Oh well, here's the revised code, with your other suggestions as well:
[CODE="Javascript"]
function log(arg, base) {
if (base === undefined) {
return Math.log(arg);
} else {
return (arg == undefined || arg == 0) ? (-Infinity) : (arg < 0) ? (NaN) : (Math.log(arg)/Math.log(base));
}
}
function log10(arg) {
// http://kevin.vanzonneveld.net
// * example 1: log10(10);
// * returns 1: 1
// * example 1: log10(1);
// * returns 1: 0
return (arg == undefined || arg == 0) ? (-Infinity) : (arg < 0) ? (NaN) : Math.log(arg)/Math.log(10);
}
[/CODE]
how's it look?
Philip: Maybe you would want to look at this again:
- Math.log return the natural logarithm with a base of e and not 10. I haven't tested it but I doubt PHP and javascript would return the same value with this implementation.
- "-INF" and "NaN" are not valid javascript representations of these values. -Infinity and NaN (without quotes) should be used.
In fact, log() has a similar discrepancy:
[CODE="Javascript"]
function log(arg, base) {
if (base === undefined) {
return Math.log(arg);
} else {
return (arg == undefined || arg == 0) ? ("-INF") : (arg < 0) ? ("NAN") : (Math.log(arg)/Math.log(base));
}
}
[/CODE]
I'm also not sure if I should be using === when I'm using == in this post and previous posts, so perhaps someone more knowledgeable about that sort of thing should check it out.
Actually, a small fix:
[CODE="Javascript"]
function log10(arg) {
// http://kevin.vanzonneveld.net
// * example 1: log(10);
// * returns 1: 1
// * example 1: log(1);
// * returns 1: 0
return (arg == undefined || arg == 0) ? ("-INF") : (arg < 0) ? ("NAN") : Math.log(arg);
}
[/CODE]


Kevin van Zonneveld
20 Oct '08