Skip to content

abs()

Calculates the absolute value of a number.

A number's absolute value is its distance from zero on the number line. -5 and 5 are both five units away from zero, so calling abs(-5) and abs(5) both return 5. The absolute value of a number is always positive.

Examples

abs example 1

function setup()
  size(100, 100)

  describe('A gray square with a vertical black line that divides it in half. A white rectangle gets taller when the user moves the mouse away from the line.')
end

function draw()
  background(200)

  -- Divide the canvas.
  line(50, 0, 50, 100)

  -- Calculate the mouse's distance from the middle.
  local h = abs(mouseX - 50)

  -- Draw a rectangle based on the mouse's distance
  -- from the middle.
  rect(0, 100 - h, 100, h)
end

Syntax

abs(n)

Parameters

Parameter
n Number: number to compute.

Returns

Number: absolute value of given number