Quirrel4.41.0

math.abs

Defined in module "math"
from "math" import abs
pure fastcall abs(x: number): number

Returns the absolute value of x

Parameters

xnumberthe number to take the absolute value of

Return value

x with its sign removed. The result keeps the type of x: an integer argument gives an integer back, a float argument gives a float back. Use fabs when a float result is wanted regardless of the argument's type.

Notes

The smallest representable integer has no positive counterpart at the same width, so taking its absolute value overflows and returns that same negative value unchanged.

Example

examples/math/abs.nut
from "math" import abs

println("abs(-3) =", abs(-3))
println("type(abs(-3)) =", type(abs(-3)))

// the result keeps the argument's type: a float argument gives a float back
println("abs(-3.0) =", abs(-3.0))
println("type(abs(-3.0)) =", type(abs(-3.0)))
Output:
abs(-3) = 3
type(abs(-3)) = integer
abs(-3.0) = 3
type(abs(-3.0)) = float

See also

fabsReturns the absolute value of float(x)
floorRounds x downward to the nearest integer
ceilRounds x upward to the nearest integer
mathmodule index