Stream: ideas

Topic: A "true" floating point modulus method and/or operator?


view this post on Zulip Stuart Hungerford (Nov 25 2026 at 22:05):

When working with angles I sometimes miss the Python "true" modulus operator for floating point values:

>>> -10.0 % 360.0
350.0
>>>

The Roc equivalent I think returns a remainder:

»  -30.0 % 360.0
-30.0

I realize I can always do:

»  -10.0 - 360.0 * -10.0.div_floor_by(360.0)
350.0

But a method or operator would be nice to have.

view this post on Zulip Steve Howell (Nov 25 2026 at 23:13):

I feel like I've seen a programming language out there that has two operators for mod (with both semantics for negatives). It's definitely a bug-prone thing.

view this post on Zulip Stuart Hungerford (Nov 25 2026 at 23:13):

A true_mod() function could also work

view this post on Zulip Aurélien Geron (Nov 26 2026 at 20:07):

FWIW, Odin and CoffeeScript both use % for the remainder (-10 % 360 = -10) and %% for the modulo (-10 %% 360 = 350).

view this post on Zulip Aurélien Geron (Nov 26 2026 at 20:11):

Note: almost all languages that distinguish the two call them remainder and modulo, with the results shown above. The only exception I know of is Odin which oddly reverses this convention (it might just be a mistake in their docs).


Last updated: Nov 26 2026 at 20:35 UTC