Reference · Math

math.fabs

signature
math.fabs(num) -> float

What 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 int

Edge cases

print(math.fabs(-0.0), math.fabs(float("-inf")), math.fabs(float("nan")))print(math.fabs(-9007199254740993))
output
0.0 inf NaN9007199254740992.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; 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 str
this 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
7Useful? Like it, or add to its thread below.

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