UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

184 lines (155 loc) 4.62 kB
<!-- Generated by Rakefile:build --> <strong> kirilloid </strong> on 2012-03-06 12:32:42 <br /> Hm-hm. Why not use this? <pre><code>function fmod (x, y) { return x % y; }</code></pre> <hr /> <strong> <a href="http://hrabstwo.net" rel="nofollow">lord_t</a> </strong> on 2012-02-08 22:16:42 <br /> <pre><code> Math.fmod = function(x, y) { // Returns the remainder of dividing x by y as a float // // version: 1109.2015 // discuss at: http://phpjs.org/functions/fmod // + original by: Onno Marsman // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Pawel 'lord_t' Maruszczyk (http://hrabstwo.net) // * example 1: fmod(5.7, 1.3); // * returns 1: 0.5 var tmp, tmp2, p = 0, pY = 0, l = 0.0, l2 = 0.0; tmp = x.toExponential().match(/^.\.?(.*)e(.+)$/); p = parseInt(tmp[2], 10) - (tmp[1] + '').length; tmp = y.toExponential().match(/^.\.?(.*)e(.+)$/); pY = parseInt(tmp[2], 10) - (tmp[1] + '').length; if (pY &gt; p) { p = pY; } tmp2 = (x % y); if (p &lt; -100 || p &gt; 20) { // toFixed will give an out of bound error so we fix it like this: l = Math.round(Math.log(tmp2) / Math.log(10)); l2 = Math.pow(10, l); return (tmp2 / l2).toFixed(l - p) * l2; } else { return tmp2; } } </code></pre> <hr /> <strong> <a href="hrabstwo.net" rel="nofollow">lord_t</a> </strong> on 2012-02-08 22:14:48 <br /> <pre><code> Math.fmod = function(x, y) { // Returns the remainder of dividing x by y as a float // // version: 1109.2015 // discuss at: http://phpjs.org/functions/fmod // + original by: Onno Marsman // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Pawel 'lord_t' Maruszczyk // * example 1: fmod(5.7, 1.3); // * returns 1: 0.5 var tmp, tmp2, p = 0, pY = 0, l = 0.0, l2 = 0.0; tmp = x.toExponential().match(/^.\.?(.*)e(.+)$/); p = parseInt(tmp[2], 10) - (tmp[1] + '').length; tmp = y.toExponential().match(/^.\.?(.*)e(.+)$/); pY = parseInt(tmp[2], 10) - (tmp[1] + '').length; if (pY &gt; p) { p = pY; } tmp2 = (x % y); if (p &lt; -100 || p &gt; 20) { // toFixed will give an out of bound error so we fix it like this: l = Math.round(Math.log(tmp2) / Math.log(10)); l2 = Math.pow(10, l); return (tmp2 / l2).toFixed(l - p) * l2; } else { return tmp2; } } </code></pre> <hr /> <strong> <a href="hrabstwo.net" rel="nofollow">Lord_t</a> </strong> on 2012-02-08 21:38:57 <br /> It doesn't work! <hr /> <strong> Kutsy </strong> on 2011-03-09 08:42:04 <br /> <pre><code> php: fmod(5,10) =&gt; 5 js: fmod(5,10) =&gt; 10 !!! Must be &quot;5&quot;! </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-06 12:25:55 <br /> @ Onno Marsman: Sharp, thanks again Onno. <hr /> <strong> Onno Marsman </strong> on 2008-10-05 17:50:26 <br /> The following comments should be removed, they are not true: // % note: Examples in PHP &amp;amp; JS return: 0.8, but according // % note: the PHP-manual's it should be 0.5. PHP manual seems to be incorrect? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-01 15:18:10 <br /> @ Enrique González: Whoops, my bad. <hr /> <strong> <a href="http://www.maciaspajas.com" rel="nofollow">Enrique González</a> </strong> on 2008-10-01 15:00:56 <br /> The example 1 at rad2deg is wrong. It should be something like: rad2deg (3.141592653589793) returns 180 <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-01 14:24:56 <br /> @ Enrique González: Thank you so much! <hr /> <strong> <a href="http://www.maciaspajas.com" rel="nofollow">Enrique González</a> </strong> on 2008-10-01 13:52:45 <br /> Two simple functions that translate deg to rad and viceversa: <pre><code> function deg2rad(angle) { //* example 1 : deg2rad (180) //* returns 1 : 3.141592653589793 return (angle/180)*Math.PI; } function rad2deg(angle) { //* example 1 : deg2rad (pi()) //* returns 1 : 3.141592653589793 return (angle/Math.PI)*180; } </code></pre> <hr />