Reference · Math

math.modf

signature
math.modf(num) -> list

What it does

The fractional and the integer part of a number, as a list of two floats, [fraction, whole], as Python's math.modf: math.modf(3.75) is [0.75, 3.0]. Both parts carry the number's sign, so math.modf(-3.75) is [-0.75, -3.0]; the whole part is truncated toward zero, as math.trunc does, but stays a float.

Examples

print(math.modf(3.75), math.modf(-3.75), math.modf(5))let [frac, whole] = math.modf(12.5)print(frac, whole)
output
[0.75, 3.0] [-0.75, -3.0] [0.0, 5.0]0.5 12.0

Edge cases

print(math.modf(-3.0), math.modf(float("inf")), math.modf(-0.0))print(math.modf(0.5), math.modf(-0.5))
output
[0.0, -3.0] [0.0, inf] [0.0, -0.0][0.5, 0.0] [-0.5, -0.0]

Errors

An argument that is not a number is a runtime error, not an error value. A decimal is refused with a hint to convert it with float.

this one fails
print(math.modf("3.75"))
output
line 1: `math.modf` expects a number as argument 1, got str

Chaining

In a pipeline the number comes first. first and last pick a part:

print(3.75 | math.modf | first)print(3.75 | math.modf | last)print([1.25, 2.5] | map(math.modf))
output
0.753.0[[0.25, 1.0], [0.5, 2.0]]

See also

math.trunc, math.frexp, math.fmod, int

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

Thread 0 posts

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