52 of 151 menu

The remainder method of the math module

The remainder method of the math module returns the remainder of dividing one number by a second. In the first parameter of the method, we specify the number to be divided (the dividend), in the second parameter - the number by which we divide the first (the divisor). Unlike the % operator, the method always returns a real value.

Syntax

import math math.remainder(dividend, divisor)

Example

Let's find the remainder when dividing the number 25 by 5:

import math print(math.remainder(25, 5))

Result of code execution:

0.0

Example

Now let's get the remainder of dividing 30 by 4:

import math print(math.remainder(30, 4))

Result of code execution:

-2.0

Example

Let's find the remainder from dividing the numbers 10 and 3:

import math print(math.remainder(10, 3))

Result of code execution:

1.0

Example

Now let's try to find out the remainder from dividing the numbers 10 by 0:

import math print(math.remainder(10, 0))

After executing the code, the function will return us an error:

ValueError: math domain error

See also

  • method fmod of module math,
    which returns the remainder of a division of floating point numbers
  • method modf of module math,
    which returns the fractional and integer parts of a number
  • function divmod,
    which returns a tuple of the quotient and the remainder when dividing
byenru