Is there a modulus (not remainder!) function / operation in Rust?
As far as I can tell, there is no modular arithmetic function.
This also happens in C, where it is common to use the workaround you mentioned: ((a % b) + b) % b
.
In C, C++, D, C#, F# and Java, %
is in fact the remainder. In Perl, Python or Ruby, %
is the modulus.
Language developers don't always go the "correct mathematical way", so computer languages might seem weird from the strict mathematician view. The thing is that both modulus and remainder, are correct for different uses.
Modulus is more mathematical if you like, while the remainder (in the C-family) is consistent with common integer division satisfying: (a / b) * b + a % b = a
; this is adopted from old Fortran. So %
is better called the remainder, and I suppose Rust is being consistent with C.
You are not the first to note this:
modulus
instead ofmodulo
(which is more common AFAICS). – Alexandria