Reference · Math
abs
signature
abs(num | dec) -> num | decWhat 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 7Edge cases
- The type is the argument's:
abs(-5)is the int5,abs(-5.0)the float5.0. - A decimal keeps its trailing zeros:
abs(-12.50m)is12.50m. NaNstaysNaN;-infbecomesinf;-0.0is0.0.- The smallest int has no positive twin:
absof it is an integer overflow. It is written-9223372036854775808; as a method,(-9223372036854775808).abs(), since-9223372036854775808.abs()reads as the minus of9223372036854775808.abs().
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.0Errors
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 strthis one fails
print(abs(-9223372036854775808))output
line 1: integer overflowChaining
print([-1, 2, -3] | map(abs))print(-7 | abs, abs(3 - 10) + 1)print([3, -4] | map(abs) | max)output
[1, 2, 3]7 84See also
Thread
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