lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 24/01/17 09:47 PM, Milo Christiansen wrote:
Hmmm... So why does my programmers calculator, Go, JavaScript, etc, etc all return -3 in this case?

Personally I think it's a bug, as "2" is not the correct result for that expression (in any language but Lua it seems).

Don't top post. And read this:
http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/
(That's from the LuaRocks developer, I think. So they kinda know what they're saying.)

And check mailing list archives. This isn't the first time this has come up.


On Tue, Jan 24, 2017 at 6:40 PM, Soni L. <fakedme@gmail.com <mailto:fakedme@gmail.com>> wrote:

    On 24/01/17 09:21 PM, Milo Christiansen wrote:

        In Lua 5.3 "-3%5 == 2", according to my programmers calculator
        and several other programing languages I tried this expression
        in, the real result is -3.

        On the other hand "5%-3 == 2", so maybe the result is correct,
        and the bug is simply reversed operands if one is negative?

        I discovered this because I am adding tests (based on the
        official Lua tests) to my Go Lua VM, and one of the tests
        failed because the incorrect result is coded into the test.


    First of all your modulo is not my modulo (or at least this was a
    thing back in C89... looks like it isn't in C99+).

    Secondly, -3%5 == 2 because floor(-3/5) = -1, and (-1)*5+2 = -5+2
    = -3.

    See also:

    https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/
    <https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/>

    http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/
    <http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/>

    https://github.com/sp614x/optifine/issues/288
    <https://github.com/sp614x/optifine/issues/288>

    etc. This is an endless argument.

    Thirdly, in case you're still reading this for some unknown
    reason, RTFM always applies:

    http://www.lua.org/manual/5.3/manual.html#3.4.1
    <http://www.lua.org/manual/5.3/manual.html#3.4.1>

    "Modulo is defined as the remainder of a division that rounds the
    quotient towards minus infinity (floor division)."

-- Disclaimer: these emails may be made public at any given time,
    with or without reason. If you don't agree with this, DO NOT REPLY.




--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.