Dark Mode On/Off

math.remainder() Function in Python with Examples

Posted in Programming   LAST UPDATED: SEPTEMBER 2, 2021

Before you think this is a simple remainder function that finds the remainder when a number is divided by the other, stop and read ahead. This isn't what you think.

Finding the remainder when a number is divided by another is trivial. It can be done using the modulus(modulo) operator (`%`) but a modulus operator always returns a positive number(true modulus value) which makes it unsuitable if we have to use this operator with negative numbers.

Python 3.7 has introduced a new remainder method which can be found in the math library.

Syntax of `remainder()` method

Following is the syntax of the remainder method:

``math.remainder(number_one, number_two)``

Here `number_one `refers to the divisor and `number_two `refers to the dividend.

Input:

The remainder method takes 2 inputs (they can be of integer or float type).

Output:

It returns the remainder of the division operation when the first number is divided by the second number. The return type is a floating-point value, which means it can be a fraction too.

When both the first and second parameters are finite and non-zero, this function calculates `(number_one - n*number_two)` as the output instead of just diving two numbers and giving the remainder as output. Here `n` is the closest integer value to the actual quotient for `(number_one/number_two)`.

Time for an Example!

Suppose the first number is 17 and the second number is 6.

One would expect the output to be 5. But surprisingly, with the remainder function, the output is -1.

This is because 17 can be divided by 6 twice leaving remainder aS 5, but instead of 17, if the dividend was 18, it can be completely divided by 6. This means 17 is much closer to 18 than it is to 12 (6 multiplied by 2). Hence the output would be -1 indicating that the dividend is just one number far from the number which is completely divided by the divisor.

Also, `math.remainder` function always fulfills the condition the below condition:

abs(reminder) <= 0.5 * abs(number_two)

Let's take an example and see,

``````a = 10.5
b = 3.1
print(math.remainder(a,b))

m = abs(math.remainder(a,b))
n = 0.5*abs(b)
print(m <= n)``````

Output:

```1.1999999999999997
True```

Let's take an example to see the `math.remainder()` function in action:

``````a = 17
b = 6
print(math.remainder(a,b))``````

Output:

`-1.0`

Special cases with the `math.remainder()` method

#1. When the second parameter (divisor) is an infinite value(in the math library, it can be accessed using `math.inf`), and the first parameter is any finite, non-zero number, it returns the first parameter as output.

``````import math

print(math.remainder(5, math.inf))``````

Output:

`5.0`

#2. When the second parameter is 0 and the first parameter is any finite, non-zero number, it raises a `ValueError`.

``````import math

print(math.remainder(5, 0))``````

Output:

`ValueError: math domain error`

#3. Similarly, when the first parameter is infinite and the second parameter is a finite, non-zero value, it raises a `ValueError`.

``````import math

print(math.remainder(math.inf, 5))``````

Output:

`ValueError: math domain error`

On platforms that have binary floating-point values, the result of the remainder operation is always exactly displayed, no rounding error occurs.

Conclusion:

This is a new introduction in the Python 3.7 version's math library and is quite different than the traditional modulus operator that was used for finding remainder until now. This function expands the scope of the remainder operation.

You may also like:

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.
I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
Tags:PythonPython Math LibraryProgramming