UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

126 lines (98 loc) 3.56 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">????? ????? ???</a> </strong> on 2012-03-22 13:52:35 <br /> New in the world of news and news of new articles added <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-20 18:38:04 <br /> @ Onno Marsman &amp;amp; Philip Peterson: Thanks for both of your efforts. I've ran the script <hr /> <strong> Onno Marsman </strong> on 2008-10-16 09:35:25 <br /> 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. <hr /> <strong> Philip Peterson </strong> on 2008-10-15 23:00:51 <br /> 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: <pre><code> function log(arg, base) { if (base === undefined) { return Math.log(arg); } else { return (arg == undefined || arg == 0) ? (-Infinity) : (arg &amp;lt; 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 &amp;lt; 0) ? (NaN) : Math.log(arg)/Math.log(10); } </code></pre> how's it look? <hr /> <strong> Onno Marsman </strong> on 2008-10-15 09:37:09 <br /> 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. - &amp;quot;-INF&amp;quot; and &amp;quot;NaN&amp;quot; are not valid javascript representations of these values. -Infinity and NaN (without quotes) should be used. <hr /> <strong> Philip Peterson </strong> on 2008-10-15 00:50:28 <br /> In fact, log() has a similar discrepancy: <pre><code> function log(arg, base) { if (base === undefined) { return Math.log(arg); } else { return (arg == undefined || arg == 0) ? (&amp;quot;-INF&amp;quot;) : (arg &amp;lt; 0) ? (&amp;quot;NAN&amp;quot;) : (Math.log(arg)/Math.log(base)); } } </code></pre> 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. <hr /> <strong> Philip Peterson </strong> on 2008-10-15 00:46:21 <br /> Actually, a small fix: <pre><code> 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) ? (&amp;quot;-INF&amp;quot;) : (arg &amp;lt; 0) ? (&amp;quot;NAN&amp;quot;) : Math.log(arg); } </code></pre> <hr /> <strong> Philip Peterson </strong> on 2008-10-14 23:31:45 <br /> 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); } <hr />