Use PHP functions in JavaScript

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);
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
20 Oct '08 Permalink

q  @ Onno Marsman & Philip Peterson: Thanks for both of your efforts. I've ran the script

Gravatar
Onno Marsman
16 Oct '08 Permalink

q  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.

Gravatar
Philip Peterson
15 Oct '08 Permalink

q  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?

Gravatar
Onno Marsman
15 Oct '08 Permalink

q  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.

Gravatar
Philip Peterson
15 Oct '08 Permalink

q  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.

Gravatar
Philip Peterson
15 Oct '08 Permalink

q  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]

Gravatar
Philip Peterson
14 Oct '08 Permalink

q  function log10(arg) {
// http://kevin.vanzonneveld.net
// * example 1: log(10);
// * returns 1: 1
// * example 1: log(1);
// * returns 1: 0
return Math.log(arg);
}


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
» log
log10
log1p
max
min
mt_getrandmax
mt_rand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !