Reference · Math

math.fmod

signature
math.fmod(num, num) -> float

What it does

The remainder of dividing the first number by the second, with the sign of the first, as a float: C's fmod and Python's math.fmod. math.fmod(-7, 3) is -1.0. The % operator follows the same sign rule and keeps ints as ints; math.fmod always gives a float.

Examples

print(math.fmod(7, 3), math.fmod(-7, 3), math.fmod(7, -3))print(math.fmod(7.5, 2), math.fmod(5.5, 1), math.fmod(-5.5, 1))print(-7 % 3, type_of(math.fmod(7, 3)))
output
1.0 -1.0 1.01.5 0.5 -0.5-1 float

Edge cases

print(math.fmod(7, float("inf")), math.fmod(1e300, 3), math.fmod(-0.0, 3))print(math.fmod(float("nan"), 3))
output
7.0 0.0 -0.0NaN

Errors

A divisor of zero, or an infinite dividend, is a runtime error, math domain error, not an error value (Python raises ValueError there too). An argument that is not a number is a runtime error as well.

this one fails
print(math.fmod(7, 0))
output
line 1: `math.fmod`: math domain error (7, 0)
this one fails
print(math.fmod(float("inf"), 3))
output
line 1: `math.fmod`: math domain error (inf, 3)

Chaining

In a pipeline the dividend comes first, so x | math.fmod(y) is x modulo y:

print(7 | math.fmod(2))print([1.5, 2.5, 3.5] | map((x) => math.fmod(x, 1)))
output
1.0[0.5, 0.5, 0.5]

See also

math.remainder, math.modf, math.trunc

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On math.fmod(): 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