-- Leo's gemini proxy

-- Connecting to freeshell.de:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en-GB

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

-- Response ended

-- Page fetched on Fri May 3 19:09:41 2024