in reply to [OT: JavaScript] JS remainder operation ('%')

According to 2 online JS calculators (https://playcode.io/new and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder) the result is 859064762.882.

I get 859064762.875 from both of those when using FireFox, and from the FireFox console. (It's not surprising they all give the same result since they all use the JS engine in my browser.)

I get 859064762.875 from both of those when using Chrome, and from the Chrome console. (Again, the consistency is to be expected.)

What kind of machine are you using?


Note that they are are two common modulo operators in CS ("floored" and "truncated"), and it varies a lot by language.

Perl%Floored
POSIX::fmodTruncated
JavaScript%Truncated

This only matters if an operands is negative, though.

  • Comment on Re: [OT: JavaScript] JS remainder operation ('%')

Replies are listed 'Best First'.
Re^2: [OT: JavaScript] JS remainder operation ('%')
by NERDVANA (Priest) on Jan 17, 2024 at 18:42 UTC
    Keep in mind that there is a IEEE rounding setting that on x86 usually defaults to "round toward even", but could also be set to round down or round up. This could be relevant since the implementation of fmod involves a float-to-int conversion.
Re^2: [OT: JavaScript] JS remainder operation ('%')
by LanX (Saint) on Jan 17, 2024 at 21:51 UTC
    Copying this into the address bar
    • javascript:document.writeln(900719925474099.7 % 2147483647.83)
    is producing 859064762.882 on my android's browsers

    • Chrome
    • Duckduckgo
    • Opera
    I suppose you are on windows?

    update

    it's also producing 859064762.882 on my Ubuntu18's

    • Chromium
    • Firefox

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re^2: [OT: JavaScript] JS remainder operation ('%')
by syphilis (Archbishop) on Jan 18, 2024 at 01:57 UTC
    What kind of machine are you using?

    Windows 11, with Google Chrome.
    What kind are you using ?
    Apparently yours uses the sub FMOD { return $_[0] - int($_[0] / $_[1]) * $_[1]; } that harangzsolt33 posted, not the fmod() implementation that most(?) of us are seeing.

    Cheers,
    Rob

      Nevermind, I just noticed that you changed the code before running it in Perl.