API Reference › Math › Math.abs
method

Math.abs

Math.abs(x) -> int

Returns the absolute value of x — its distance from zero, always non-negative. It preserves the type of its argument, so Math.abs of a fixed stays fixed. Reach for it when you care about how far apart two things are regardless of direction, such as the gap between a target and current position, or the speed behind a signed velocity. The bare form abs(x) is kept as a convenience alias.

Parameters

xthe value whose magnitude you want

Example

program ChaseDistance {
  var target: int = 40
  var here: int = 12

  handler Report phase Update {
    let gap = Math.abs(target - here)   # 28, regardless of which is larger
  }
}
← All symbols