

The expression a Mod b is equivalent to either of the following formulas:Ī - (b * Fix(a / b)) Floating-point imprecision A floating-point division returns Double.NaN.An integral division throws a DivideByZeroException exception if number2 cannot be determined in compile-time and generates a compile-time error BC30542 Division by zero occurred while evaluating this expression if number2 is evaluated to zero at compile-time.If number2 evaluates to zero, the behavior of the Mod operator depends on the data type of the operands: For example, the expression 14 / 4 evaluates to 3.5.

The / Operator (Visual Basic) returns the full quotient, including the remainder, as a floating-point number. For example, the expression 14 \ 4 evaluates to 3. The \ Operator (Visual Basic) returns the integer quotient of a division. If number1 or number2 evaluates to Nothing, it is treated as zero. The data type of the result is the smallest data type that can hold all possible values that result from division with the data types of number1 and number2. If either number1 or number2 is a floating-point value, the floating-point remainder of the division is returned. ' The example displays the following output: For example: Public Module ExampleĬonsole.WriteLine($" 8 Mod 3 = ") The result is always in the range (- number2, number2), exclusive. The result of a Mod operation retains the sign of the dividend, number1, and so it may be positive or negative. NET Framework op_Modulus operator, and the underlying rem IL instruction all perform a remainder operation. Modular arithmetic is extremely important in the field of cryptography, which encodes information using modulo operations with a very large modulus.There is a difference between remainder and modulus in mathematics, with different results for negative numbers. Modular arithmetic is the field of mathematics which concerns these types of operations, where values "wrap around" (reset to zero) when they reach a modulus value. Examplesġ1 mod 4 = 3, because 11 divides by 4 (twice), with 3 remaining.Ģ5 mod 5 = 0, because 25 divides by 5 (five times), with 0 remaining.ģ mod 2 = 1, because 3 divides by 2 (once), with 1 remaining.ĥ mod 2 = 1, because all odd numbers yield a remainder of 1 when divided by 2. Where a is the dividend, b is the divisor (or modulus), and r is the remainder. In writing, it is frequently abbreviated as mod, or represented by the symbol %. Modulo is a math operation that finds the remainder when one integer is divided by another.
