💾 Archived View for freeshell.de › gemlog › 2023-06-18_Division__remainder__mod.gmi captured on 2023-07-10 at 13:27:58. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Division, remainder, mod ➗%❓

siiky said "remainder is not modulo"

I'm not qualified to say if that's true (and I don't really care) but I have some relevant observations because I came across this while implementing a Whitespace interpreter.

Whitespace was originally implemented in Haskell, and I wrote an interpreter in Java. These two languages disagree on what the operator % means for negative inputs, so I read...

...what Wikipedia has to say on the differrence.

Oh, wow! What a mess.

First, integer division. What integer do you round to if you can't divide exactly? Wikipedia lists FIVE ways to decide on the result, with graphs. Ouch.

Once you've decided how integer division works, then you can decide what % means. Good luck. You won't please everyone.

I did notice that although Java didn't do what siiky expected with the % operator, it does understand that there's disagreement here, and has Math.floorMod() as an alternative, and I think that does siiky wanted.

My practical advice is that remainder and mod are the same, except for negative numbers, where language implementers live in a wild, Bohemian, devil-may-care world, and do what they like.

#Whitespace

#programming

back to gemlog