Use PHP functions in JavaScript

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

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.

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
lord_t
Feb 8th Permalink

q  

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;
    }
}

Gravatar
lord_t
Feb 8th Permalink

q  

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;
    }
}

Gravatar
Lord_t
Feb 8th Permalink

q  It doesn't work!

Gravatar
Kutsy
9 Mar '11 Permalink

q  

php: fmod(5,10) => 5
js: fmod(5,10) => 10  !!! Must be "5"!


Gravatar
Kevin van Zonneveld
6 Oct '08 Permalink

q  @ Onno Marsman: Sharp, thanks again Onno.

Gravatar
Onno Marsman
5 Oct '08 Permalink

q  The following comments should be removed, they are not true:

// % note: Examples in PHP &amp; JS return: 0.8, but according
// % note: the PHP-manual's it should be 0.5. PHP manual seems to be incorrect?

Gravatar
Kevin van Zonneveld
1 Oct '08 Permalink

q  @ Enrique González: Whoops, my bad.

Gravatar
Enrique González
1 Oct '08 Permalink

q  The example 1 at rad2deg is wrong. It should be something like:

rad2deg (3.141592653589793)
returns 180

Gravatar
Kevin van Zonneveld
1 Oct '08 Permalink

q  @ Enrique González: Thank you so much!

Gravatar
Enrique González
1 Oct '08 Permalink

q  Two simple functions that translate deg to rad and viceversa:

[CODE=&quot;javascript&quot;]
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]


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 !