Reference · Math
math.remainder
math.remainder(num, num) -> floatWhat it does
The IEEE remainder: the first number minus the multiple of the second that is closest
to it, as a float, as Python's math.remainder. The result lies between -|y| / 2 and
|y| / 2, so it can be negative for positive arguments: math.remainder(8, 3) is
-1.0, since 8 is closer to 9 than to 6. For the remainder with the dividend's sign
use math.fmod or %.
Examples
print(math.remainder(7, 3), math.remainder(8, 3), math.remainder(7.5, 2))print(math.fmod(8, 3), 8 % 3)1.0 -1.0 -0.52.0 2Edge cases
- A tie goes to the even multiple:
math.remainder(7, 2)is-1.0(7 is as close to 8 as to 6, and 8 is 4 × 2) whilemath.remainder(9, 2)is1.0. - The sign of the result is not the dividend's:
math.remainder(-5.5, 1)is0.5. - An infinite divisor leaves the number as it is.
-0.0keeps its sign, and NaN in either place gives NaN.
print(math.remainder(7, 2), math.remainder(9, 2), math.remainder(-5.5, 1))print(math.remainder(7, float("inf")), math.remainder(-0.0, 3), math.remainder(float("nan"), 2))-1.0 1.0 0.57.0 -0.0 NaNErrors
A divisor of zero, or an infinite dividend, is a runtime error, math domain error,
not an error value. An argument that is not a number is a runtime error as well.
print(math.remainder(7, 0))line 1: `math.remainder`: math domain error (7, 0)print(math.remainder(float("inf"), 3))line 1: `math.remainder`: math domain error (inf, 3)Chaining
In a pipeline the dividend comes first. The distance from the nearest multiple is its absolute value:
print(8 | math.remainder(3))print([7, 8, 9] | map((x) => math.remainder(x, 3)) | map(math.fabs))-1.0[1.0, 1.0, 0.0]See also
Thread 0 posts
On math.remainder(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo