Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a modulus (not remainder) function / operation?

Tags:

modulo

rust

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!

like image 589
Kapichu Avatar asked Jul 03 '15 15:07

Kapichu


People also ask

Does modulus mean remainder?

The Modulus is the remainder of the euclidean division of one number by another. % is called the modulo operation. For instance, 9 divided by 4 equals 2 but it remains 1 .

What operations use modulus?

In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.

How do you find a remainder without a modulus operator?

First of all, read the number and divisor as entered by the user. Keep subtracting the divisor from the number and set the value to the number until the number becomes smaller than the divisor. If the number becomes smaller than the divisor, it should be the required remainder. Print out the result.

Is modulus a mathematical operation?

The modulo (or "modulus" or "mod") is the remainder after dividing one number by another. It is where we end up, not how many times around.


3 Answers

RFC 2196 adds a couple of integer methods related to euclidian division. Specifically, the rem_euclid method (example link for i32) is what you are searching for:

println!("{}", -1i32 % 4);                // -1
println!("{}", (-21i32).rem_euclid(4));   // 3

This method is available in rustc 1.38.0 (released on 2019-09-27) and above.

like image 78
Lukas Kalbertodt Avatar answered Oct 04 '22 21:10

Lukas Kalbertodt


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:

  • No modulo operator?
  • Remainder is not modulus, but int::rem() uses the mod operator. .
like image 25
JosEduSol Avatar answered Oct 04 '22 21:10

JosEduSol


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 }
like image 21
ideasman42 Avatar answered Oct 04 '22 20:10

ideasman42