Object | +-Math public class Math extends Object 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) Availability: ActionScript 1.0; 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. Property summary
Properties inherited from class Object
Method summary
Methods inherited from class Object
abs (Math.abs method)public static abs(x:Number) : Number Computes and returns an absolute value for the number specified by the parameter x. Availability: ActionScript 1.0; 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. Parameters x :Number - A number. Returns Number - A number. 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 acos (Math.acos method)public static acos(x:Number) : Number Computes and returns the arc cosine of the number specified in the parameter x, in radians. Availability: ActionScript 1.0; 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. Parameters x :Number - A number from -1.0 to 1.0. Returns Number - A number; the arc cosine of the parameter x. 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 See also asin (Math.asin method), atan (Math.atan method), atan2 (Math.atan2 method), cos (Math.cos method), sin (Math.sin method), tan (Math.tan method) asin (Math.asin method)public static asin(x:Number) : Number Computes and returns the arc sine for the number specified in the parameter x, in radians. Availability: ActionScript 1.0; 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. 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. 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 See also acos (Math.acos method), atan (Math.atan method), atan2 (Math.atan2 method), cos (Math.cos method), sin (Math.sin method), tan (Math.tan method) atan (Math.atan method)public static atan(tangent:Number) : Number 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. Availability: ActionScript 1.0; 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. 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. 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 See also acos (Math.acos method), asin (Math.asin method), atan2 (Math.atan2 method), cos (Math.cos method), sin (Math.sin method), tan (Math.tan method) atan2 (Math.atan2 method)public static atan2(y:Number, x:Number) : Number 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. Availability: ActionScript 1.0; 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. Parameters y :Number - A number specifying the y coordinate of the point. x :Number - A number specifying the x coordinate of the point. Returns Number - A number. 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 See also acos (Math.acos method), asin (Math.asin method), atan (Math.atan method), cos (Math.cos method), sin (Math.sin method), tan (Math.tan method) ceil (Math.ceil method)public static ceil(x:Number) : Number 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. Availability: ActionScript 1.0; 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. Parameters x :Number - A number or expression. Returns Number - An integer that is both closest to, and greater than or equal to, parameter x. Example The following code returns a value of 13: Math.ceil(12.5); See also floor (Math.floor method), round (Math.round method) cos (Math.cos method)public static cos(x:Number) : Number Computes and returns the cosine of the specified angle in radians. To calculate a radian, see the description of the Math class entry. Availability: ActionScript 1.0; 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. Parameters x :Number - A number that represents an angle measured in radians. Returns Number - A number from -1.0 to 1.0. 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. See also acos (Math.acos method), asin (Math.asin method), atan (Math.atan method), atan2 (Math.atan2 method), sin (Math.sin method), tan (Math.tan method) E (Math.E property)public static E : Number A mathematical constant for the base of natural logarithms, expressed as e. The approximate value of e is 2.71828182845905. Availability: ActionScript 1.0; 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. 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 exp (Math.exp method)public static exp(x:Number) : Number 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. Availability: ActionScript 1.0; 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. Parameters x :Number - The exponent; a number or expression. Returns Number - A number. 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 See also E (Math.E property) floor (Math.floor method)public static floor(x:Number) : Number 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. Availability: ActionScript 1.0; 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. 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: Math.floor(12.5); The following code returns a value of -7: Math.floor(-6.5); LN10 (Math.LN10 property)public static LN10 : Number A mathematical constant for the natural logarithm of 10, expressed as loge10, with an approximate value of 2.302585092994046. Availability: ActionScript 1.0; 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. Example This example traces the value of Math.LN10. trace(Math.LN10); // output: 2.30258509299405 LN2 (Math.LN2 property)public static LN2 : Number A mathematical constant for the natural logarithm of 2, expressed as loge2, with an approximate value of 0.6931471805599453. Availability: ActionScript 1.0; 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. log (Math.log method)public static log(x:Number) : Number Returns the natural logarithm of parameter x. Availability: ActionScript 1.0; 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. 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 LOG10E (Math.LOG10E property)public static LOG10E : Number 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. Availability: ActionScript 1.0; 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. Example This example shows how to obtain the base-10 logarithm of a number: trace(Math.log(1000) * Math.LOG10E); // Output: 3 LOG2E (Math.LOG2E property)public static LOG2E : Number 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. Availability: ActionScript 1.0; 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. Example This example shows how to obtain the base-2 logarithm of a number: trace(Math.log(16) * Math.LOG2E); // Output: 4 max (Math.max method)public static max(x:Number, y:Number) : Number Evaluates x and y and returns the larger value. Availability: ActionScript 1.0; 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. Parameters x :Number - A number or expression. y :Number - A number or expression. Returns Number - A number. 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()); See also min (Math.min method), Date min (Math.min method)public static min(x:Number, y:Number) : Number Evaluates x and y and returns the smaller value. Availability: ActionScript 1.0; 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. Parameters x :Number - A number or expression. y :Number - A number or expression. Returns Number - A number. 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()); See also max (Math.max method) PI (Math.PI property)public static PI : Number A mathematical constant for the ratio of the circumference of a circle to its diameter, expressed as pi, with a value of 3.141592653589793. Availability: ActionScript 1.0; 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. 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); } pow (Math.pow method)public static pow(x:Number, y:Number) : Number Computes and returns x to the power of y. Availability: ActionScript 1.0; 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. 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 Number - A number. 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. random (Math.random method)public static random() : Number Returns a pseudo-random number n, where 0 <= n < 1. The number returned is a pseudo-random number because it is not generated by a truly random natural phenomenon such as radioactive decay. Availability: ActionScript 1.0; 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 Number - A number. 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); } round (Math.round method)public static round(x:Number) : Number 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. Availability: ActionScript 1.0; 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. Parameters x :Number - A number. Returns Number - A number; an integer. 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)); } See also ceil (Math.ceil method), floor (Math.floor method) sin (Math.sin method)public static sin(x:Number) : Number Computes and returns the sine of the specified angle in radians. To calculate a radian, see the description of the Math class entry. Availability: ActionScript 1.0; 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. 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). 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); } See also acos (Math.acos method), asin (Math.asin method), atan (Math.atan method), atan2 (Math.atan2 method), cos (Math.cos method), tan (Math.tan method) sqrt (Math.sqrt method)public static sqrt(x:Number) : Number Computes and returns the square root of the specified number. Availability: ActionScript 1.0; 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. 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); SQRT1_2 (Math.SQRT1_2 property)public static SQRT1_2 : Number A mathematical constant for the square root of one-half, with an approximate value of 0.7071067811865476. Availability: ActionScript 1.0; 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. Example This example traces the value of Math.SQRT1_2. trace(Math.SQRT1_2); // Output: 0.707106781186548 SQRT2 (Math.SQRT2 property)public static SQRT2 : Number A mathematical constant for the square root of 2, with an approximate value of 1.4142135623730951. Availability: ActionScript 1.0; 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. Example This example traces the value of Math.SQRT2. trace(Math.SQRT2); // Output: 1.4142135623731 tan (Math.tan method)public static tan(x:Number) : Number Computes and returns the tangent of the specified angle. To calculate a radian, use the information outlined in the introduction to the Math class. Availability: ActionScript 1.0; 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. Parameters x :Number - A number that represents an angle measured in radians. Returns Number - A number; tangent of parameter x. 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); } See also acos (Math.acos method), asin (Math.asin method), atan (Math.atan method), atan2 (Math.atan2 method), cos (Math.cos method), sin (Math.sin method) |