24 October 2011

Thanks for your feedback.

You should be familiar with ActionScript 3 syntax, variables, data types and operators.

Beginning

- Flash Builder (Download trial)
- Adobe Animate CC

ActionScript 3 can manipulate different types of numbers. Understanding the different types of numbers and how they work is advantageous for a programmer. You can select the appropriate numerical data types when programming, know the limitations of each type within the language, and find solutions to problems that can occur. In this article you will learn about the numerical data types available in ActionScript 3 and be introduced to the Math class.

Natural numbers are the numbers you are most familiar with. You use them to count the things around you. A natural number is zero and any number obtained by repeatedly adding 1—in other words, 0 and any positive, whole number. Examples of natural numbers are 100, 0, 45,645, 32.

Integers include all natural numbers plus all negative whole numbers. Examples of integers are –24, –1, 100, 0, 45,645.

Any rational or irrational number is a real number. A rational number is any number that can be expressed as the quotient or fraction of two integers or with a decimal. Examples of rational numbers are –249, –1, 0, 3/7, –2/5, and .342. Irrational numbers are numbers that can't accurately be expressed as a fraction or with a decimal. The best-known irrational numbers are π, e, and the √2. Floating-point numbers represent real numbers in computers.

In ActionScript 3, the Number data type can represent all types of numbers—integers, unsigned integers, and floating-point numbers. It is the most flexible of numeric data types in ActionScript 3. However, to maximize performance, use the `Number`

data type only for floating-point numbers or for integer values larger than the 32-bit `int`

and `uint`

types can store.

The `Number`

data type uses the 64-bit double-precision format as specified by the IEEE Standard for Binary Floating-Point Arithmetic (IEEE-754). This standard dictates how floating-point numbers are stored using the 64 available bits. One bit is used to designate whether the number is positive or negative. Eleven bits are used for the exponent, which is stored as base 2. The remaining 52 bits are used to store the *significand* (also called *mantissa*), the number that is raised to the power indicated by the exponent.

By using some of its bits to store an exponent, the `Number`

data type can store floating-point numbers significantly larger than if it used all of its bits for the significand. For example, if the `Number`

data type used all 64 bits to store the significand, it could store a number as large as 2^{65 }– 1. By using 11 bits to store an exponent, the `Number`

data type can raise its significand to a power of 2^{1023}.

Although this range of numbers is enormous, it comes at the cost of precision. Because the `Number`

data type uses 52 bits to store the significand, numbers that require more than 52 bits for accurate representation, such as the fraction 1/3, are only approximations. If your application requires absolute precision with decimal numbers, use software that implements decimal floating-point arithmetic as opposed to binary floating-point arithmetic.

To store a floating-point number, include a decimal point in the number. If you omit a decimal point, the number is stored as an integer.

The default value of an unassigned `Number`

variable is `NaN`

. The `NaN`

value is also the result of any operation that should return a number but does not. For example, if you attempt to calculate the square root of a negative number or divide 0 by 0, the result is `NaN`

. Other special `Number`

values include positive infinity and negative infinity.

**Note: **The result of division by 0 is only `NaN`

if the divisor is also 0. Division by 0 produces infinity when the dividend is positive or –infinity when the dividend is negative.

The int data type includes all positive and negative whole numbers from –2,147,483,648 (–2^{31}) through 2,147,483,647 (2^{31} – 1) inclusive. It is stored as a 32-bit integer. If you do not need floating-point numbers, use the `int`

type instead of `Number`

because it is faster and more efficient. For values outside the range of `int`

, use the `Number`

data type. The `int`

type is commonly used in counters and loops. The default value for unassigned `int`

variables is 0.

If you try to assign a floating-point value to an `int`

variable, ActionScript ignores the fractional (decimal) part.

The uint data type includes all positive whole numbers from 0 through 4,294,967,295 (2^{32} – 1) inclusive. It is stored as a 32—bit integer. The default value for unassigned `uint`

variables is 0. As with `int`

, if you try to assign a floating-point value to a `uint`

variable, ActionScript ignores the fractional (decimal) part.

A `uint`

value is always a simple binary translation of the value. Use a `uint`

for all bit math and when working with hexadecimal values for color.

ActionScript 3.0 includes a core class dedicated to doing mathematical functions. The Math class contains constants and methods that are used to perform arithmetic and trigonometric calculations. All the properties and methods of the Math class are static. Call them using the syntax `Math.method(parameter)`

or `Math.constant`

. All constants are defined with the maximum precision of double-precision IEEE-754 floating-point numbers. Below are some examples of using the Math class.

```
var randomNumber:Number = Math.random();
var squareRoot:Number = Math.sqrt(16);
var sineOf90Degrees:Number = Math.sin(π/2); //Degrees must be
// converted to radians for
// most trigonometric functions
```

A full understanding of the number types available to you as an ActionScript developer along with the methods provided by the Math class provide essential tools in ensuring your application functions as intended. When combined with operators and functions, these types form the essential logic of your application. To learn more about each of these read ActionScript 3 fundamentals: Operators and ActionScript 3 fundamentals: Functions.

The content in this article is based on material originally published in the Learning ActionScript 3 user guide created by Adobe Community Help and Learning.

- ActionScript 3 fundamentals: Operators
- ActionScript 3 fundamentals: Packages
- ActionScript 3 fundamentals: Arrays
- ActionScript 3 fundamentals: Loops
- ActionScript 3 fundamentals: Syntax
- ActionScript 3 fundamentals: Variables
- ActionScript 3 fundamentals: Data types
- Garbage collection internals for Flash Player and Adobe AIR
- AS3 fundamentals: Conditionals
- ActionScript 3 fundamentals: Functions