Javascript fmod() wanted

Discussion in 'Javascript' started by Aaron Gray, Jul 21, 2006.

  1. Aaron Gray

    Aaron Gray Guest

    Does anyone have a good fmod() function written in Javascript ?

    Many thanks in advance,

    Aaron
     
    Aaron Gray, Jul 21, 2006
    #1
    1. Advertising

  2. Aaron Gray

    Randy Webb Guest

    Randy Webb, Jul 21, 2006
    #2
    1. Advertising

  3. Aaron Gray

    Aaron Gray Guest

    "Randy Webb" <> wrote in message
    news:...
    > Aaron Gray said the following on 7/21/2006 6:24 PM:
    >> Does anyone have a good fmod() function written in Javascript ?

    >
    > That depends on what you want fmod() to do.


    A floating point modulo function.

    Aaron
     
    Aaron Gray, Jul 21, 2006
    #3
  4. Aaron Gray

    web.dev Guest

    Aaron Gray wrote:
    > Does anyone have a good fmod() function written in Javascript ?


    I'm assuming you're looking for a javascript port of the fmod function
    in php.

    Using the php example as a basis:

    $x = 5.7;
    $y = 1.3;
    $r = fmod( $x, $y);

    //$r equals 0.5, because 4 * 1.3 + 0.5 = 5.7

    I threw together this inefficient method, plus I did not take care of
    the intricacies behind javascript's floating point precision.

    function fmod(dividend, divisor)
    {
    var multiplier = 0;

    while(divisor * multiplier < dividend)
    {
    ++multiplier;
    }

    --multiplier;

    return dividend - (divisor * multiplier);
    }
     
    web.dev, Jul 22, 2006
    #4
  5. "Aaron Gray" <> writes:
    ....
    >> Aaron Gray said the following on 7/21/2006 6:24 PM:
    >>> Does anyone have a good fmod() function written in Javascript ?

    ....[which is]...
    > A floating point modulo function.


    Ah, I can do that:
    function fmod(a,b) {
    return a % b;
    }

    Or, do you need something else?

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 22, 2006
    #5
  6. Aaron Gray

    web.dev Guest

    Lasse Reichstein Nielsen wrote:
    > Ah, I can do that:
    > function fmod(a,b) {
    > return a % b;
    > }


    Oh would you look at that. For some reason unbeknownst to me, I've
    always thought javascript modulus only worked with whole numbers. :)
     
    web.dev, Jul 22, 2006
    #6
  7. Aaron Gray

    Aaron Gray Guest

    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > "Aaron Gray" <> writes:
    > ...
    >>> Aaron Gray said the following on 7/21/2006 6:24 PM:
    >>>> Does anyone have a good fmod() function written in Javascript ?

    > ...[which is]...
    >> A floating point modulo function.

    >
    > Ah, I can do that:
    > function fmod(a,b) {
    > return a % b;
    > }
    >
    > Or, do you need something else?


    No, thats the one, nice and integeral being in Javascript as an overloaded
    operator, great :)

    Many thanks,

    Aaron
     
    Aaron Gray, Jul 22, 2006
    #7
  8. Aaron Gray

    Aaron Gray Guest

    >> A floating point modulo function.
    >
    > Ah, I can do that:
    > function fmod(a,b) {
    > return a % b;
    > }


    Its got some horrible rounding errors :-

    1295999.1 % 1296000 = 1295999.1
    1296000.1 % 1296000 = 0.10000000009313226
    1296001.1 % 1296000 = 1.1000000000931322

    Same errors as :-

    function mods3600( x)
    {
    return x - 1.296e6 * Math.floor(x/1.296e6);
    }

    Although that only works for positive numbers.

    Aaron
     
    Aaron Gray, Jul 22, 2006
    #8
  9. Aaron Gray

    Csaba Gabor Guest

    Lasse Reichstein Nielsen wrote:
    > "Aaron Gray" <> writes:
    > ...
    > >> Aaron Gray said the following on 7/21/2006 6:24 PM:
    > >>> Does anyone have a good fmod() function written in Javascript ?

    > ...[which is]...
    > > A floating point modulo function.

    >
    > Ah, I can do that:
    > function fmod(a,b) {
    > return a % b;
    > }
    >
    > Or, do you need something else?


    Yes. I've ever hated fmod. A mod should always be non negative.
    That's what they were when I was a kid, and that's what they should be
    now, darnit. The mathematician's mod:

    function mod(num,modulus) {
    return ((num<0) ? Math.abs(modulus) : 0) + (num % modulus); }

    mod(num,modulus) above implements the classic notion of mod, returning
    NaN if modulus is 0. mod(...) satisfies the equation

    num=k*modulus+mod(num,modulus) for some integer k

    where mod(num,modulus) is non negative and less than |modulus|. Note
    that this differs from fmod(num,modulus), which will be negative iff
    num<0. mod(...) will be non negative regardless of whether either or
    both arguments are negative. There is no requirement that either a or
    b be an integer.

    Csaba Gabor from Vienna
     
    Csaba Gabor, Jul 22, 2006
    #9
  10. Aaron Gray

    RobG Guest

    web.dev wrote:
    > Aaron Gray wrote:
    > > Does anyone have a good fmod() function written in Javascript ?

    >
    > I'm assuming you're looking for a javascript port of the fmod function
    > in php.
    >
    > Using the php example as a basis:
    >
    > $x = 5.7;
    > $y = 1.3;
    > $r = fmod( $x, $y);
    >
    > //$r equals 0.5, because 4 * 1.3 + 0.5 = 5.7
    >
    > I threw together this inefficient method, plus I did not take care of
    > the intricacies behind javascript's floating point precision.
    >
    > function fmod(dividend, divisor)
    > {
    > var multiplier = 0;
    >
    > while(divisor * multiplier < dividend)
    > {
    > ++multiplier;
    > }
    >
    > --multiplier;
    >
    > return dividend - (divisor * multiplier);
    > }


    Given that JavaScript has a modulus operator, an fmod function isn't
    needed. For the sake of it, the above is overly complex - consider:

    function fmod(a, b){
    while ( b <= a) {a -= b}
    return a;
    }

    Which is shorter and likely faster, but is still very slow where
    dividend is large and divisor small. It will also vary from the result
    given by a%b quite often if either is a float because of the
    compounding error from the use of IEEE 64 bit numbers (that has been
    covered in great depth before). Consider:

    function fmod(a, b){
    var x = Math.floor(a/b);
    return a - b*x;
    }

    Which, while still not spot on, is fast and should be close enough if %
    is not available and intelligent rounding is used.


    --
    Rob
     
    RobG, Jul 22, 2006
    #10
  11. "Aaron Gray" <> writes:

    > Its got some horrible rounding errors :-
    >
    > 1295999.1 % 1296000 = 1295999.1
    > 1296000.1 % 1296000 = 0.10000000009313226


    The rounding error is not intrinsic to modulus computation, but comes
    from the fact that 1296000.1 cannot be represented exactly as a double
    precission floating point number.

    Try 1296000.1 - 1296000, which also gives 0.10000000009313226, because
    1296000.10000000009313226
    is the closest representable number to 1296000.1.

    The precission was lost the moment you wrote 1296000.1, it has nothing
    to do with the operation, which is actually precise in this case.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 22, 2006
    #11
  12. Aaron Gray

    RobG Guest

    Aaron Gray wrote:
    >>> A floating point modulo function.

    >> Ah, I can do that:
    >> function fmod(a,b) {
    >> return a % b;
    >> }

    >
    > Its got some horrible rounding errors :-
    >
    > 1295999.1 % 1296000 = 1295999.1
    > 1296000.1 % 1296000 = 0.10000000009313226
    > 1296001.1 % 1296000 = 1.1000000000931322


    Further to what Lasse said, you might like to try something that returns
    a value based on the number of places used in the input:

    function fmod(a, b){
    var placesA = (''+a).replace(/^[^.]*(\.)?/,'').length;
    var placesB = (''+b).replace(/^[^.]*(\.)?/,'').length;
    var places = (placesA > placesB)? placesA : placesB;
    return (a%b).toFixed(places);
    }

    alert( fmod(1296001.1, 1296000)
    + '\n' + (1296001.1 % 1296000));


    Notes:
    toFixed was introduced in JavaScript verison 1.5 and while it is part of
    the ECMAScript Ed 3 specification it may not be supported everywhere.

    It may be necessary to test the input for suitable values (NaN, zero,
    infinity, etc. - ECMAScript spec 11.5.3) and the size of places (it
    should be from 0 to 20 inclusive - ECMAScript spec 15.7.4.5).


    --
    Rob
     
    RobG, Jul 22, 2006
    #12
  13. JRS: In article <>, dated Fri, 21 Jul 2006
    23:24:58 remote, seen in news:comp.lang.javascript, Aaron Gray
    <> posted :

    >Does anyone have a good fmod() function written in Javascript ?


    function fmod(X, Y) { return X%Y }

    function fdiv(X, Y) { return (X/Y)|0 }
    Read the newsgroup FAQ.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Jul 22, 2006
    #13
  14. In article <>, Csaba
    Gabor <> writes

    <snip>
    >Yes. I've ever hated fmod. A mod should always be non negative.
    >That's what they were when I was a kid, and that's what they should be
    >now, darnit. The mathematician's mod:

    <snip>

    This argument has appeared many times in other news groups. The fact is
    that the "mathematician's" mod is defined for the Natural Numbers where
    the numbers involved cannot be negative.

    You can define mod, %, rem, whatever, for negative numbers but when you
    do, you find that different application areas need different sign
    conventions.

    It's simply not true that there is one sign convention that is more
    'correct' than all the others. It appears that most languages that are
    specific about the sign follow the Fortran rules whether you like it or
    not.

    John
    --
    John Harris
     
    John G Harris, Jul 22, 2006
    #14
  15. JRS: In article <>, dated Sat, 22 Jul 2006
    00:14:13 remote, seen in news:comp.lang.javascript, Aaron Gray
    <> posted :
    >"Lasse Reichstein Nielsen" <> wrote in message
    >news:...
    >> "Aaron Gray" <> writes:
    >> ...
    >>>> Aaron Gray said the following on 7/21/2006 6:24 PM:
    >>>>> Does anyone have a good fmod() function written in Javascript ?

    >> ...[which is]...
    >>> A floating point modulo function.

    >>
    >> Ah, I can do that:
    >> function fmod(a,b) {
    >> return a % b;
    >> }
    >>
    >> Or, do you need something else?

    >
    >No, thats the one, nice and integeral being in Javascript as an overloaded
    >operator, great :)



    It's not an overloaded operator; it only has a single manifestation.
    Barring future editions, Javascript has only one numeric type, Number,
    an IEEE Double; and % has no application except between two Numbers.

    One *can* write (new Date()>=0)%0.75 if one wants to generate 0
    before 1970-01-01 00:00:00.000 UTC and 0.25 thereafter; but the Boolean
    is there forced to Number by the % operator. Indeed, one can use % with
    other types; but the results are not useful.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Jul 22, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey
    Replies:
    0
    Views:
    772
    Harvey
    Jul 16, 2004
  2. Harvey
    Replies:
    1
    Views:
    888
    Daniel
    Jul 16, 2004
  3. Shewy du 80 !!

    fmod et Pause du MP3

    Shewy du 80 !!, Jun 14, 2004, in forum: C++
    Replies:
    1
    Views:
    668
    Pete C.
    Jun 14, 2004
  4. seia0106

    atan2() and fmod()

    seia0106, Jul 28, 2004, in forum: C++
    Replies:
    8
    Views:
    746
    John Harrison
    Jul 29, 2004
  5. stau

    fmod?

    stau, Dec 28, 2003, in forum: C Programming
    Replies:
    6
    Views:
    650
Loading...

Share This Page