Name

Math.abs( ) Method — compute the absolute value of a number

Availability

Flash 5; may be used when exporting Flash 4 movies

Synopsis

Math.abs(x)

Arguments

x

A positive or negative number.

Returns

The absolute value of x (a positive number of magnitude x).

Description

The abs( ) method calculates the distance between x and (also known as the absolute value of x). It leaves positive numbers unchanged and converts negative numbers into positive numbers of the same magnitude. It is useful for calculating the difference between two numbers without regard to which is larger than the other. For example, it is useful when calculating the distance between two points because distances are always positive.

Example

Math.abs(-5);  // Returns 5

// Calculate the difference between two numbers
function diff (num1, num2) { 
  return Math.abs(num1-num2);
}

diff(-5, 5);  // Returns 10

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.