Number object
Numbers are any number which is not included within quotes (if you add a number between quotes, will be interpreted as a String).
There are several types of numbers:
- Natural numbers - positive whole numbers (8, 15, 89, 1234).
- Integer numbers - whole numbers that might be either positive or negative (-78 , -3 , 0 , 5 , 78).
- Floating-point numbers (-6.4 , 0.8 , 12.5).
- Hexadecimal numbers (0x7d8 is number 2008).
- Exponential numbers - used for large numerical values ( 7.8e+8 = 780 000 000, is 7.8x108 ).
Everything in ActionScript 3.0 is an object, including numbers. In ActionScript there are special classes for working with numbers.
You can create a variable that contains numerical values by using any of the following syntaxes:
var var_name:NumberType = number;
var var_name:NumberType = new NumberType(number);
var var_name:NumberType = NumberType(number);
-
NumberType represents the ActionScript class for the type of numbers that are stored in the "var_name" variable, can be 3 types:
- Number - for any type of numbers: integers, decimal (floating-point), hexadecimal and exponentials.
- int - for integer numbers (positive or negative).
- uint - for positive integer numbers.
-
Usually the first syntax is used.
Example:
var num1:int = 78;
var num2:Number = new Number(68.8);
var num3:uint = uint(23);
NaN - Not a number
If you declare a Number type variable, without assign a value, the variable will have the defaul value,
NaN (which represents "
not a number").
Mathematical operations with non-real or undefined results also yield NaN, such as the square root of -1 or zero divided by zero.
Example:
var num1:Number;
var num2:Number = 0/0;
trace(num1); // NaN
trace(num2); // NaN
To check if a variable has the NaN value, use the
isNaN(num) function. This function returns
true if the value of "
num" parameter is NaN, otherwise, returns
false.
Example:
var num:Number;
trace(num); // NaN
trace(isNaN(num)); // true
// check if the value of "num" is NaN, and assign a numeric value
if(isNaN(num)) {
num = 78;
}
trace(num); // 78
Other things about numbers in ActionScript
-
var num:Number = .4; and
var nr:Number = 0.45; represent the same number (0.45).
- The default value for
int and
uint types is 0.
- To define a color in ActionScript, it uses the expression:
0xFFRRGGBB (F - alpha (transparency), R - red, G - green, B - blue), this is a 32-bits value which can be stored in
uint variable (e.g.,
var getcolor:uint = 0xafe8edfe; ).
Methods and Constants of the Number object
The Number object has no specific properties, but has a few methods and constants.
To perform arithmetic operations with numbers, use the arithmetic and relational operators (see the lesson
Constants and Operators).
Methods
toExponential(dg) - returns a string representation of the number in exponential notation. The "dg" parameter represents the number of digits before the decimal point.
var nr:Number = 123456789;
trace(nr.toExponential(2)); // 1.23e+8
toFixed(dg) - returns a string representation of the number in fixed-point notation. The "dg" parameter represents the number of digits before the decimal point.
var nr:Number = 12.3456789;
trace(nr.toFixed(3)); // 12.346
toPrecision(dg) - returns a string representation of the number either in exponential notation or in fixed-point notation. The string will contain the number of digits specified in the "dg" parameter.
var nr1:Number = 12.3456789;
trace(nr1.toPrecision(4)); // 12.35
var nr2:Number = 123400000;
trace(nr2.toPrecision(4)); // 1.234e+8
toString() - converts the object into string.
var nr:int = 7;
var str:String = '8';
trace(nr.toString()+ str); // 78 (or String(nr)+ str )
Number('str') - converts the 'str' string into number.
var nr:int = 7;
var str:String = '8';
trace(nr+ Number(str)); // 15
Constants
MAX_VALUE - the largest posible number
trace(int.MAX_VALUE); // 2147483647
trace(uint.MAX_VALUE); // 4294967295
trace(Number.MAX_VALUE); // 1.79769313486231e+308
MIN_VALUE - The smallest representable number
trace(int.MIN_VALUE); // -2147483648
trace(uint.MIN_VALUE); // 0
trace(Number.MIN_VALUE); // 4.9406564584124654e-324
NEGATIVE_INFINITY - represents the negative infinity. It can be used to check the negative infinite conditions.
var nr:Number = -7/0;
trace(); // -Infinity
trace(nr == Number.NEGATIVE_INFINITY); // true
POSITIVE_INFINITY - represents the positive infinity. It can be used to check the positive infinite conditions.
var nr:Number = 8/0;
trace(); // Infinity
trace(nr == Number.POSITIVE_INFINITY); // true
Math class
The
Math class contains methods and constants to perform complex mathematical operations.
To apply the methods of the Math class, use the following syntax:
Math.method(parameter)
- Example:
var nr:Number = Math.pow(3, 4); // 3 raised to power 4
trace(nr); // 81
var nr2:Number = Math.random(); // get a random value between 0 and 1
trace(nr2); // 0.32177373580634594
Math mehods for Arithmetic operations
- pow(a, b) - "a" raised to "b" power
- exp(a) - "e" raised to the "a" power
- floor(num) - "num" rounded down
- ceil(num) - "num" rounded up
- round(num) - "num" rounded to the nearest integer
- max(a, b, c, ...) - maximum of the set "a, b, c, ..."
- min(a, b, c, ...) - minimum of the set "a, b, c, ..."
- sqrt(num) - square root of "num"
- abs(num) - absolute value of "num"
- log(num) - logarithm (base 10) of "num"
- ln(num) - natural logarithm (base e) of "num"
- random() - returns a random value between 0 and 1
Math mehods for Trigonometric calculations
- sin(a) - Sine of an angle measuring "a" radians
- cos(a) - Cosine of an angle measuring "a" radians
- tan(a) - Tangent of an angle measuring "a" radians
- asin(a) - Angle in radians whose sine is "a" (arcsine of "a")
- acos(a) - Angle in radians whose cosine is "a" (arccosine of "a")
- atan(a) - Angle in radians whose tangent is "a" (arctangent of "a")
- atan2(x, y) - Angle which, drawn from the origin (0, 0), intersects the point (x, y) (arctangent of y/x)
- All trig functions operate on radians, an angular unit in which 2π radians measure a full revolution.
To get the radians value when you know the degrees, use the following formula:
radians = degrees * Math.PI/180;
- Example:
var degrees:Number = 45;
var radians:Number = degrees * Math.PI/180;
trace(radians); // 0.7853981633974483
To get the degrees when you know the radians value, use the following formula:
degrees = radians * 180/Math.PI;
- Example:
var radians:Number = 0.7854;
var degrees:Number = radians * 180/Math.PI;
trace(degrees); // 45
Math Constants
Constants contain a fixed value. To apply a constant, use the following syntax:
Math.CONSTANT
- Example:
trace(Math.PI); // 3.141592653589793
trace(Math.E); // 2.71828182845905
- E - the base of natural logarithms ( 2.71828182845905 )
- LN10 - natural logarithm of 10 ( 2.302585092994046 )
- LN2 - natural logarithm of 2 ( 0.6931471805599453 )
- LOG10E - the base-10 logarithm of the constant e (Math.E) ( 0.4342944819032518 )
- LOG2E - the base-2 logarithm of the constant e ( 1.442695040888963387 )
- PI - the ratio of the circumference of a circle to its diameter ( 3.141592653589793 )
- SQRT1_2 - square root of one-half ( 0.7071067811865476 )
- SQRT2 - square root of 2 ( 1.4142135623730951 )