Reference · Math
math.fabs
signature
math.fabs(num) -> floatWhat it does
The absolute value, always as a float, as Python's math.fabs: math.fabs(-3) is
3.0. abs keeps an int an int and takes a decimal too; this
one is for float arithmetic.
Examples
print(math.fabs(-3), math.fabs(3.5), math.fabs(-2.25))print(type_of(math.fabs(-3)), type_of(abs(-3)))output
3.0 3.5 2.25float intEdge cases
-0.0gives0.0.- Infinity gives infinity, and NaN stays NaN; neither is an error.
- An int beyond 2⁵³ loses precision on the way to a float, as any int to float conversion does.
print(math.fabs(-0.0), math.fabs(float("-inf")), math.fabs(float("nan")))print(math.fabs(-9007199254740993))output
0.0 inf NaN9007199254740992.0Errors
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; abs takes a
decimal as it is.
this one fails
print(math.fabs("x"))output
line 1: `math.fabs` expects a number as argument 1, got strthis one fails
print(math.fabs(dec("-1")))output
line 1: `math.fabs` expects a number as argument 1, got dec (this works on floats: convert a dec with float(x))Chaining
In a pipeline the number comes first. Differences become distances:
print(-2.5 | math.fabs)print([3, -1, 2] | map((x) => x - 2) | map(math.fabs))output
2.5[1.0, 3.0, 0.0]See also
abs, math.copysign, vec.abs, float
Thread
Thread 0 posts
On math.fabs(): 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