Reference · Math
math.modf
math.modf(num) -> listWhat 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)[0.75, 3.0] [-0.75, -3.0] [0.0, 5.0]0.5 12.0Edge cases
- The fraction comes first, the whole part second, as in Python.
- An int gives a fraction of
0.0and the int as a float. - A whole negative float gives a fraction of
0.0, not-0.0as Python gives; the whole part keeps the sign. - Infinity gives
[0.0, inf], and-0.0gives[0.0, -0.0]. - A float so large that it has no fraction gives
0.0and the number itself.
print(math.modf(-3.0), math.modf(float("inf")), math.modf(-0.0))print(math.modf(0.5), math.modf(-0.5))[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.
print(math.modf("3.75"))line 1: `math.modf` expects a number as argument 1, got strChaining
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))0.753.0[[0.25, 1.0], [0.5, 2.0]]See also
math.trunc, math.frexp, math.fmod, int
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