racket / zuo

A tiny Racket for scripting
Other
263 stars 27 forks source link

Fix modulo #18

Closed soegaard closed 10 months ago

soegaard commented 10 months ago

This PR makes zuo_modulo return a result that has the same sign as the second argument.

mflatt commented 10 months ago

Is the current modulo actually more like remainder? My goal was just to have the simpler operator (i.e., simpler to implement in C), because Zuo doesn't really need division with negative numbers, and probably I just got confused and used the wrong name.

Unless the operation is also broken as remainder, I would be inclined to either document the mismatch with Racket/Scheme or rename modulo to remainder. The more important goal for Zuo is fewer lines of C code.

soegaard commented 10 months ago

Is the current modulo actually more like remainder? Yes, the current Zuo modulo just call the C operator % which computes the remainder.

The C spec says:

If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. (and as far as I can see doesn't mention anything about signs).

Unless the operation is also broken as remainder, I would be inclined to either document the mismatch with Racket/Scheme or rename modulo to remainder. The more important goal for Zuo is fewer lines of C code.

I sort of had a hunch that the current behaviour was intended, when I saw the tests ;-)

I'll close this PR and make an issue that says to update the docs.