Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
The Math class is a top-level class whose methods and properties you can use without using a constructor.
Use the methods and properties of this class to access and manipulate mathematical constants and functions. All the properties and methods of the Math class are static and must be called using the syntax Math.method(
parameter
)
or Math.constant
. In ActionScript, constants are defined with the maximum precision of double-precision IEEE-754 floating-point numbers.
Several Math class methods use the measure of an angle in radians as a parameter.You can use the following equation to calculate radian values before calling the method and then provide the calculated value as the parameter, or you can provide the entire right side of the equation (with the angle's measure in degrees in place of degrees
) as the radian parameter.
To calculate a radian value, use the following formula:
radians = degrees * Math.PI/180
The following is an example of passing the equation as a parameter to calculate the sine of a 45° angle:
Math.sin(45 * Math.PI/180)
is the same as Math.sin(.7854)
public static function abs(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns an absolute value for the number specified by the parameter x
.
Parameters
Returns
Example
The following example shows how
Math.abs()
returns the absolute value of a number and does not affect the value of the
x
parameter (called
num
in this example):
var num:Number = -12;
var numAbsolute:Number = Math.abs(num);
trace(num); // output: -12
trace(numAbsolute); // output: 12
public static function acos(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the arc cosine of the number specified in the parameter x
, in radians.
Parameters
| x:Number — A number from -1.0 to 1.0. |
Returns
| Number —
A number; the arc cosine of the parameter x .
|
See also
Example
The following example displays the arc cosine for several values.
trace(Math.acos(-1)); // output: 3.14159265358979
trace(Math.acos(0)); // output: 1.5707963267949
trace(Math.acos(1)); // output: 0
public static function asin(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the arc sine for the number specified in the parameter x
, in radians.
Parameters
| x:Number — A number from -1.0 to 1.0. |
Returns
| Number —
A number between negative pi divided by 2 and positive pi divided by 2.
|
See also
Example
The following example displays the arc sine for several values.
trace(Math.asin(-1)); // output: -1.5707963267949
trace(Math.asin(0)); // output: 0
trace(Math.asin(1)); // output: 1.5707963267949
public static function atan(tangent:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the value, in radians, of the angle whose tangent is specified in the parameter tangent
. The return value is between negative pi divided by 2 and positive pi divided by 2.
Parameters
| tangent:Number — A number that represents the tangent of an angle. |
Returns
| Number —
A number between negative pi divided by 2 and positive pi divided by 2.
|
See also
Example
The following example displays the angle value for several tangents.
trace(Math.atan(-1)); // output: -0.785398163397448
trace(Math.atan(0)); // output: 0
trace(Math.atan(1)); // output: 0.785398163397448
public static function atan2(y:Number, x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the angle of the point y
/x
in radians, when measured counterclockwise from a circle's x axis (where 0,0 represents the center of the circle). The return value is between positive pi and negative pi.
Parameters
| y:Number — A number specifying the y coordinate of the point. |
|
| x:Number — A number specifying the x coordinate of the point. |
Returns
See also
Example
The following example returns the angle, in radians, of the point specified by the coordinates (0, 10), such that x = 0 and y = 10. Note that the first parameter to atan2 is always the y coordinate.
trace(Math.atan2(10, 0)); // output: 1.5707963267949
public static function ceil(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Returns the ceiling of the specified number or expression. The ceiling of a number is the closest integer that is greater than or equal to the number.
Parameters
| x:Number — A number or expression. |
Returns
| Number —
An integer that is both closest to, and greater than or equal to, parameter x .
|
See also
Example
The following code returns a value of 13:
public static function cos(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the cosine of the specified angle in radians. To calculate a radian, see the description of the Math class entry.
Parameters
| x:Number — A number that represents an angle measured in radians. |
Returns
| Number —
A number from -1.0 to 1.0.
|
See also
Example
The following example displays the cosine for several different angles.
trace (Math.cos(0)); // 0 degree angle. Output: 1
trace (Math.cos(Math.PI/2)); // 90 degree angle. Output: 6.12303176911189e-17
trace (Math.cos(Math.PI)); // 180 degree angle. Output: -1
trace (Math.cos(Math.PI*2)); // 360 degree angle. Output: 1
Note: The cosine of a 90 degree angle is zero, but because of the inherent inaccuracy of decimal calculations using binary numbers, Flash Player will report a number extremely close to, but not exactly equal to, zero.
public static function exp(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Returns the value of the base of the natural logarithm (e), to the power of the exponent specified in the parameter x
. The constant Math.E
can provide the value of e.
Parameters
| x:Number — The exponent; a number or expression. |
Returns
See also
Example
The following example displays the logarithm for two number values.
trace(Math.exp(1)); // output: 2.71828182845905
trace(Math.exp(2)); // output: 7.38905609893065
public static function floor(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Returns the floor of the number or expression specified in the parameter x
. The floor is the closest integer that is less than or equal to the specified number or expression.
Parameters
| x:Number — A number or expression. |
Returns
| Number —
The integer that is both closest to, and less than or equal to, parameter x .
|
Example
The following code returns a value of 12:
The following code returns a value of -7:
public static function log(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Returns the natural logarithm of parameter x
.
Parameters
| x:Number — A number or expression with a value greater than 0. |
Returns
| Number —
The natural logarithm of parameter x .
|
Example
The following example displays the logarithm for three numerical values.
trace(Math.log(0)); // output: -Infinity
trace(Math.log(1)); // output: 0
trace(Math.log(2)); // output: 0.693147180559945
trace(Math.log(Math.E)); // output: 1
public static function max(x:Number, y:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Evaluates x
and y
and returns the larger value.
Parameters
| x:Number — A number or expression. |
|
| y:Number — A number or expression. |
Returns
See also
Example
The following example displays
Thu Dec 30 00:00:00 GMT-0700 2004
, which is the larger of the evaluated expressions.
var date1:Date = new Date(2004, 11, 25);
var date2:Date = new Date(2004, 11, 30);
var maxDate:Number = Math.max(date1.getTime(), date2.getTime());
trace(new Date(maxDate).toString());
public static function min(x:Number, y:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Evaluates x
and y
and returns the smaller value.
Parameters
| x:Number — A number or expression. |
|
| y:Number — A number or expression. |
Returns
See also
Example
The following example displays
Sat Dec 25 00:00:00 GMT-0700 2004
, which is the smaller of the evaluated expressions.
var date1:Date = new Date(2004, 11, 25);
var date2:Date = new Date(2004, 11, 30);
var minDate:Number = Math.min(date1.getTime(), date2.getTime());
trace(new Date(minDate).toString());
public static function pow(x:Number, y:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns x
to the power of y
.
Parameters
| x:Number — A number to be raised to a power. |
|
| y:Number — A number specifying a power the parameter x is raised to. |
Returns
Example
The following example uses
Math.pow
and
Math.sqrt
to calculate the length of a line.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
this.origX = _xmouse;
this.origY = _ymouse;
};
mouseListener.onMouseUp = function() {
this.newX = _xmouse;
this.newY = _ymouse;
var minY = Math.min(this.origY, this.newY);
var nextDepth:Number = canvas_mc.getNextHighestDepth();
var line_mc:MovieClip = canvas_mc.createEmptyMovieClip("line"+nextDepth+"_mc", nextDepth);
line_mc.moveTo(this.origX, this.origY);
line_mc.lineStyle(2, 0x000000, 100);
line_mc.lineTo(this.newX, this.newY);
var hypLen:Number = Math.sqrt(Math.pow(line_mc._width, 2)+Math.pow(line_mc._height, 2));
line_mc.createTextField("length"+nextDepth+"_txt", canvas_mc.getNextHighestDepth(), this.origX, this.origY-22, 100, 22);
line_mc['length'+nextDepth+'_txt'].text = Math.round(hypLen) +" pixels";
};
Mouse.addListener(mouseListener);
The MovieClip.getNextHighestDepth()
method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth()
method.
public static function random():Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Returns a pseudo-random number n, where 0 <= n < 1. The number returned is called a "pseudo-random" number because it is, technically, calculated in an undisclosed manner.
Returns
Example
The following example outputs 100 random integers between 4 and 11 (inclusively):
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
for (var i = 0; i < 100; i++) {
var n:Number = randRange(4, 11)
trace(n);
}
public static function round(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Rounds the value of the parameter x
up or down to the nearest integer and returns the value. If parameter x
is equidistant from its two nearest integers (that is, the number ends in .5), the value is rounded up to the next higher integer.
Parameters
Returns
| Number —
A number; an integer.
|
See also
Example
The following example returns a random number between two specified integers.
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.round(Math.random() * (max-min+1) + (min-.5));
return randomNum;
}
for (var i = 0; i<25; i++) {
trace(randRange(4, 11));
}
public static function sin(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the sine of the specified angle in radians. To calculate a radian, see the description of the Math class entry.
Parameters
| x:Number — A number that represents an angle measured in radians. |
Returns
| Number —
A number; the sine of the specified angle (between -1.0 and 1.0).
|
See also
Example
The following example draws a circle using the mathematical constant pi, the sine of an angle, and the Drawing API.
drawCircle(this, 100, 100, 50);
//
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
mc.lineStyle(2, 0xFF0000, 100);
mc.moveTo(x+r, y);
mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}
public static function sqrt(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the square root of the specified number.
Parameters
| x:Number — A number or expression greater than or equal to 0. |
Returns
| Number —
A number if parameter x is greater than or equal to zero; NaN (not a number) otherwise.
|
Example
The following example uses
Math.pow
and
Math.sqrt
to calculate the length of a line.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
this.origX = _xmouse;
this.origY = _ymouse;
};
mouseListener.onMouseUp = function() {
this.newX = _xmouse;
this.newY = _ymouse;
var minY = Math.min(this.origY, this.newY);
var nextDepth:Number = canvas_mc.getNextHighestDepth();
var line_mc:MovieClip = canvas_mc.createEmptyMovieClip("line"+nextDepth+"_mc", nextDepth);
line_mc.moveTo(this.origX, this.origY);
line_mc.lineStyle(2, 0x000000, 100);
line_mc.lineTo(this.newX, this.newY);
var hypLen:Number = Math.sqrt(Math.pow(line_mc._width, 2)+Math.pow(line_mc._height, 2));
line_mc.createTextField("length"+nextDepth+"_txt", canvas_mc.getNextHighestDepth(), this.origX, this.origY-22, 100, 22);
line_mc['length'+nextDepth+'_txt'].text = Math.round(hypLen) +" pixels";
};
Mouse.addListener(mouseListener);
public static function tan(x:Number):Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
Computes and returns the tangent of the specified angle. To calculate a radian, use the information outlined in the introduction to the Math class.
Parameters
| x:Number — A number that represents an angle measured in radians. |
Returns
| Number —
A number; tangent of parameter x .
|
See also
Example
The following example draws a circle using the mathematical constant pi, the tangent of an angle, and the Drawing API.
drawCircle(this, 100, 100, 50);
//
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
mc.lineStyle(2, 0xFF0000, 100);
mc.moveTo(x+r, y);
mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}
public static var E:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the base of natural logarithms, expressed as e. The approximate value of e
is 2.71828182845905.
Example
This example shows how
Math.E
is used to compute continuously compounded interest for a simple case of 100 percent interest over a one-year period.
var principal:Number = 100;
var simpleInterest:Number = 100;
var continuouslyCompoundedInterest:Number = (100 * Math.E) - principal;
trace ("Beginning principal: $" + principal);
trace ("Simple interest after one year: $" + simpleInterest);
trace ("Continuously compounded interest after one year: $" + continuouslyCompoundedInterest);
//
Output:
Beginning principal: $100
Simple interest after one year: $100
Continuously compounded interest after one year: $171.828182845905
public static var LN10:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the natural logarithm of 10, expressed as loge10, with an approximate value of 2.302585092994046.
Example
This example traces the value of
Math.LN10
.
trace(Math.LN10);
// output: 2.30258509299405
public static var LN2:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the natural logarithm of 2, expressed as loge2, with an approximate value of 0.6931471805599453.
public static var LOG10E:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the base-10 logarithm of the constant e (Math.E
), expressed as log10e, with an approximate value of 0.4342944819032518.
The Math.log()
method computes the natural logarithm of a number. Multiply the result of Math.log()
by Math.LOG10E
obtain the base-10 logarithm.
Example
This example shows how to obtain the base-10 logarithm of a number:
trace(Math.log(1000) * Math.LOG10E);
// Output: 3
public static var LOG2E:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the base-2 logarithm of the constant e (Math.E
), expressed as log2e, with an approximate value of 1.442695040888963387.
The Math.log
method computes the natural logarithm of a number. Multiply the result of Math.log()
by Math.LOG2E
obtain the base-2 logarithm.
Example
This example shows how to obtain the base-2 logarithm of a number:
trace(Math.log(16) * Math.LOG2E);
// Output: 4
public static var PI:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the ratio of the circumference of a circle to its diameter, expressed as pi, with a value of 3.141592653589793.
Example
The following example draws a circle using the mathematical constant pi and the Drawing API.
drawCircle(this, 100, 100, 50);
//
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
mc.lineStyle(2, 0xFF0000, 100);
mc.moveTo(x+r, y);
mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}
public static var SQRT1_2:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the square root of one-half, with an approximate value of 0.7071067811865476.
Example
This example traces the value of
Math.SQRT1_2.
trace(Math.SQRT1_2);
// Output: 0.707106781186548
public static var SQRT2:Number
Player version: | Flash Player 5 — In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports. |
A mathematical constant for the square root of 2, with an approximate value of 1.4142135623730951.
Example
This example traces the value of
Math.SQRT2.
trace(Math.SQRT2);
// Output: 1.4142135623731
© 2004-2010 Adobe Systems Incorporated. All rights reserved.
Wed Apr 7 2010, 4:41 PM GMT-07:00