Reference · Math

abs

signature
abs(num | dec) -> num | dec

What it does

The absolute value: the number without its sign. The type is kept: an int gives an int, a float a float, a decimal a decimal with its digits as they were.

Examples

print(abs(-5), abs(5), abs(-2.5), abs(0))print(abs(-12.50m), abs(3 - 10))
output
5 5 2.5 012.50 7

Edge cases

print(type_of(abs(-5)), type_of(abs(-2.5)), type_of(abs(-1m)))print(abs(-12.50m), abs(float("nan")), abs(float("-inf")), abs(-0.0))
output
int float dec12.50 NaN inf 0.0

Errors

A string, null or a boolean is a runtime error, not an error value; abs of the smallest int is a runtime error too (integer overflow).

this one fails
print(abs("x"))
output
line 1: `abs` expects a number as argument 1, got str
this one fails
print(abs(-9223372036854775808))
output
line 1: integer overflow

Chaining

print([-1, 2, -3] | map(abs))print(-7 | abs, abs(3 - 10) + 1)print([3, -4] | map(abs) | max)
output
[1, 2, 3]7 84

See also

round, floor, ceil, sqrt, pow

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

Thread 0 posts

On abs(): 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