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
function fmod (x, y) {
    // Returns the remainder of dividing x by y as a float  
    // 
    // version: 909.322
    // 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
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 & 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:

1
2
3
4
56
7
8
9
1011
12
13
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;
  }


Contribute a New function

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

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

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 83.7%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats