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

Proxy Information
Original URL
gemini://freeshell.de/gemlog/2023-06-18_Division__remainder__mod.gmi
Status Code
Success (20)
Meta
text/gemini;lang=en-GB
Capsule Response Time
105.232281 milliseconds
Gemini-to-HTML Time
0.413496 milliseconds

This content has been proxied by September (ba2dc).