[Bug] Modulo of negative values

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
zocka
Posts: 205
Joined: 15 Jun 2010, 17:56

[Bug] Modulo of negative values

Post by zocka »

It works fine for positive values, but for negative values it kind of doesn't:

Code: Select all

log((2-3) % 4);  // -1 (at first I came across the bug with something like this)
log(-1 % 4);      // not just for calculations: still -1
log(-10 % 4);    // well it _works_ - kind of: -2
log(-12 % 4);    // 0 (Y)
log(-11 % 4);    // -3
manialink minigame shatter
my manialink: zockaml
my maniaflash: maniaflash?zocka
User avatar
Gugli
Nadeo
Nadeo
Posts: 536
Joined: 14 Jun 2010, 17:35
Location: Mon PC est à Paris, mais mon coeur rode dans les forêts de Lozère

Re: [Bug] Modulo of negative values

Post by Gugli »

These results are the "traditionnal" way to handle modulo of negative values (that's the most effective way for processors to compute them, and that's the way C and C++ work for exemple).

If you have a negative value A but still want a positive modulo, you can replace

Code: Select all

A % B
by

Code: Select all

 (A % B) + B
And if A can be positive or negative, you can use

Code: Select all

 ((A % B) + B) % B
--
(>~_~)> ═╦═ ╔╦╗ <(~_~<)
zocka
Posts: 205
Joined: 15 Jun 2010, 17:56

Re: [Bug] Modulo of negative values

Post by zocka »

Oh okay I'm sorry then. It's something you don't necessarily come across that often and apparently checking the python and haskell cli wasn't a representative reference :oops:
manialink minigame shatter
my manialink: zockaml
my maniaflash: maniaflash?zocka
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 3 guests