math.remainder

Full name
math.remainder
Library
math
Syntax

math.remainder(x, y)

Description

The math.remainder function returns the remainder of the division of "x" and "y" according to the criteria imposed by the IEEE 754 standard. This remainder is the difference of x-(n*y) for the integer value n closest to the integer part of the x/y quotient (be it greater or less than this quotient). If the integer part of x/y is at the same distance from two integers, the integer with even value is considered (see example below).

Note that this criterion does not match the one followed in the math.fmod function.

Some special cases are considered. Thus math.remainder(x, math.inf) returns x (if x is a finite number) and math.remainder(x, 0) and math.remainder(math.inf, x) return ValueError.

Result

The result of the math.remainder function is a real number.

Examples

In this first example, the remainder of the division of the numbers 6 and 5. In this case, the quotient 6/5 is 1.2 and the nearest integer is 1, so the remainder to be returned is 6-(1*5) = 1:

math.remainder(6, 5)

1.0

The math.fmod function returns the same result:

math.fmod(6, 5)

1.0

If we pass the values 6 and 4 as arguments to the function, the result is no longer exactly what we might expect. In this case, the 6/4 quotient is 1.5, so there are two integers at an equal distance from said result: the number 1 and the number 2. As mentioned, in these scenarios the even number is considered, 2, so the result of the function is 6-(2*4) = -2:

math.remainder(6, 4)

-2

This result, in effect, is not the one returned by the math.fmod function:

math.fmod(6, 4)

2

Submitted by admin on Tue, 01/26/2021 - 13:52