Reference · Vectors

vec.abs

signature
vec.abs(list) -> list

What it does

Each item's absolute value: negatives turned positive, the rest as they are. A list of only ints gives ints; a list with a float in it gives floats throughout.

Examples

print(vec.abs([-1, 2, -3]))print(vec.abs([-1.5, 2.5]))
output
[1, 2, 3][1.5, 2.5]

Edge cases

print(vec.abs([-1, 2.5]))print(vec.abs([]), vec.abs(null))print(vec.abs([-0.0, -math.inf]))
output
[1.0, 2.5][] [][0.0, inf]

Errors

An item that is not an int or a float, or an argument that is not a list, is a runtime error, not an error value.

this one fails
print(vec.abs([1, "a"]))
output
line 1: `vec.abs` expects a list of numbers, but item 1 is str

Chaining

The size of each change, whichever way it went, and the mean absolute deviation:

let sales = [3, 5, 4, 6, 8]print(sales | vec.diff | vec.abs)print(sales | vec.sub(stats.mean(sales)) | vec.abs | stats.mean)
output
[2, 1, 2, 2]1.44

See also

abs, vec.sub, vec.diff, vec.norm, math.fabs

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

Thread 0 posts

On vec.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