35

In Rust (like most programming languages), the % operator performs the remainder operation, not the modulus operation. These operations have different results for negative numbers:

-21 modulus 4 => 3
-21 remainder 4 => -1
println!("{}", -21 % 4); // -1

However, I want the modulus.

I found a workaround ((a % b) + b) % b, but I don't want to reinvent the wheel if there's already a function for that!

  • 1
    Any reason to use the term modulus instead of modulo (which is more common AFAICS). – ideasman42 Jan 2 '17 at 8:33
  • They might have studied somewhere where the term modulus is used, not knowing that different institutions tend to differ in vocabulary. – OliverUv Dec 24 '17 at 19:32
18

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.

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:

  • 1
    I, as a C/C++ programmer, am embarrassed that I didn't know that % works in C that way, too... – Kapichu Jul 3 '15 at 16:08
  • 1
    Isn't this a gap in Rust? – Kapichu Jul 3 '15 at 16:09
  • 1
    Well, as you expect, this is not something mathematicians enjoy. And i would not say it is a gap, for all this family of languages % is just the remainder. – JosEduSol Jul 3 '15 at 16:20
  • 1
    I'll go with % being the remainder, but not having support for modulus sucks... – Kapichu Jul 3 '15 at 16:24
  • 1
    @JosEduSol As I write this, the answer above shows (a % b) + b as a way to calculate the modulus, but I'm pretty sure what you meant to write is this: ((a % b) + b) % b. – David J. Nov 21 '18 at 1:58
5

No, Rust doesn't have a built in modulus, see this discussion for some reasons why.

Here's an example that might be handy:

///
/// Modulo that handles negative numbers, works the same as Python's `%`.
///
/// eg: `(a + b).modulo(c)`
///
pub trait ModuloSignedExt {
    fn modulo(&self, n: Self) -> Self;
}
macro_rules! modulo_signed_ext_impl {
    ($($t:ty)*) => ($(
        impl ModuloSignedExt for $t {
            #[inline]
            fn modulo(&self, n: Self) -> Self {
                (self % n + n) % n
            }
        }
    )*)
}
modulo_signed_ext_impl! { i8 i16 i32 i64 }

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.