JavaScript fmod
Returns the remainder of dividing x by y as a float
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 | function fmod (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) // * 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 > p) { p = pY; } tmp2 = (x % y); if (p < -100 || p > 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 parseFloat(tmp2.toFixed(-p)); }} |
Examples
Running
1 | fmod(5.7, 1.3); |
Should return
1 | 0.5 |
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 fmod goodness in JavaScript.
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 > p) {
p = pY;
}
tmp2 = (x % y);
if (p < -100 || p > 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;
}
}
The following comments should be removed, they are not true:
// % note: Examples in PHP & JS return: 0.8, but according
// % note: the PHP-manual's it should be 0.5. PHP manual seems to be incorrect?
The example 1 at rad2deg is wrong. It should be something like:
rad2deg (3.141592653589793)
returns 180
Two simple functions that translate deg to rad and viceversa:
[CODE="javascript"]
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]


lord_t
Feb 8th
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 > p) { p = pY; } tmp2 = (x % y); if (p < -100 || p > 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; } }