# The Number Data Type

### The Number Data Type

The next data type we'll discuss is the Number data type. Numbers come in all sorts of forms and are used for lots of different reasons, ranging from counting, to mathematical properties of movie clips, to expressions. Let's look at a few examples:

` 1                  //legal number 4.998              //legal number 3+4                //legal number _x                 //legal number representing a horizontal position string.length      //legal number representing the length of a string 0123               //legal number representing an octal number 10e2               //legal number using exponents 0x000000           //legal hexadecimal number "1234"             //not a legal number, but a string literal `

The two basic types of numbers supported by Flash are integers and floating-point numbers. Integers are whole numbers (positive or negative). Floating-point numbers are also positive or negative, but they include decimal points as well as fractional values (which are converted to decimal values).

Integers have two basic rules:

• They cannot contain decimals or fractional values.

• They cannot go below Number.MIN_VALUE or above Number.MAX_VALUE.

Some of the basic integers are raw numbers, such as 15 and 4400. These numbers are plain and simple. However, another example of an integer is a hexadecimal number, which is often used in color-coding (e.g., 0x6F9AB1). Yet another form of integer is an octal number, such as 0123, which translates to the following:

` (1*64) + (2*8) + (3*1) `

Floating-point numbers include decimal values, fractional values, and exponents. Exponents are defined by using the letter e followed by a number. This number represents the number of zeros to be added to the preceding number. Here's an example:

` trace (10e2); // output: 1000 `

#### Creating a Number

One way to create a number is to simply type it:

` 4 `

You can also use the Number data type in conjunction with the new constructor to create a number:

` new Number(4); `

Now you can set it equal to a variable:

` var myNumber:Number = new Number(4); trace (myNumber); // output: 4 `

You can also create numbers without the new constructor like this:

` var myNumber:Number = 4; trace(myNumber); //output:   4 `

#### Solving the Problem of Repeating Decimal Points

Because computers have difficulties with defining repeating decimal places and can sometimes misrepresent a number with multiple decimal places, it's a good idea to round or drop the decimal places with the built-in methods Math.round and Math.floor.

When using the Math.round method, simply place the number or variable holding the number in parentheses, and the method will round it to its nearest whole value, thus creating an integer:

` trace (Math.round(1.23333)); trace (Math.round(1.566666)); // output: 1 //         2 `

The Math.floor method, on the other hand, completely drops the decimal places from the number and returns an integer. Its use is the same as the Math.round method:

` trace (Math.floor(1.23333)); trace (Math.floor(1.566666)); // output: 1 //         1 `

#### Predefined Values for Numbers

Even though you can create almost any number manually, Flash has a few values for numbers built into it. Ironically, the first predefined value for a number is Not a Number (NaN).

##### NaN

Rarely would you set a number equal to NaN, but occasionally you might see this value in the Output panel when the number you are trying to use is not a number. A NaN value can be the result of placing text inside a Number data type or trying to divide zero by zero. Here's an example:

` var seatsAvailable:Number = new Number("lots"); trace (seatsAvailable); // output: NaN `

Because NaN is not a number, variables with this value cannot be equal to each other:

` //create our variables var seatsAvailable:Number = new Number("lots"); var seatsTaken:Number = new Number ("a few"); //create the if statement to see if it is not equal if (seatsAvailable != seatsTaken) {        trace("These two are not equal"); } `

You can also test to see if a number is NaN by using the isNaN function as in the following example:

` var myNum:Number = new Number("not a number"); if(isNaN(myNum)){     trace("the number is NaN"); } //output: the number is NaN `

But this does not mean that NaN is a string. For instance, you cannot do this:

` var myNum:Number = new Number("not a number"); if(myNum == "NaN"){     trace("the number is NaN"); } //output: (nothing) `

##### MAX_VALUE and MIN_VALUE

Flash has limitations as to what a number can be. Two of these limitations are MAX_VALUE and MIN_VALUE. Currently, the maximum allowable value for a number is 1.79769313486231e+308, and the minimum allowable value is 4.94065645841247e-324.

This doesn't mean a number has to be between these two values. For example, a number can be lower than MIN_VALUE, as shown here:

` //create our variable var myNumber:Number = -1; //create an if statement to see if myNumber //is lower than the MIN_VALUE if (myNumber < Number.MIN_VALUE) {       trace ("myNumber is lower than MIN_VALUE"); } // output: myNumber is lower than MIN_VALUE `

This is because MIN_VALUE is the minimum value a number can be in Flash, not the largest negative number. To see the largest negative number, set MAX_VALUE, the largest number Flash can handle, to negative and run the same code:

` //create our variable var myNumber:Number = -1; //create an if statement to see if myNumber //is lower than the -MAX_VALUE if (myNumber < -Number.MAX_VALUE) {        trace ("myNumber is lower than -MAX_VALUE"); } // output: (nothing because 1 is not smaller than MAX_VALUE) `

##### POSITIVE_INFINITY and NEGATIVE_INFINITY

If, by some chance, you create a number greater than Number.MAX_VALUE, the value will be Infinity. Likewise, if you create a negative number larger than -Number.MAX_VALUE, the value will be -Infinity.

Predefined values are built into Flash that represent Infinity and -Infinity. They are Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.

Using these predefined values, we can test whether a number is infinite in the code:

` //create our variable var myNumber:Number = Number.MAX_VALUE * Number.MAX_VALUE; //create the if statement if (myNumber == Number.POSITIVE_INFINITY){        trace ("myNumber is infinite"); } // output: myNumber is infinite `

#### Bonus Numbers

Here's a list of more predefined Math constants:

• Math.E. The natural base for a logarithm. The approximate value is 2.71828.

• Math.LN2. The natural logarithm of 2. The approximate value is 0.69314718055994528623.

• Math.LN10. The natural logarithm of 10. The approximate value is 2.3025850929940459011.

• Math.LOG2E. The base-2 logarithm of MATH.E. The approximate value is 1.442695040888963387.

• Math.LOG10E. The base-10 logarithm of MATH.E. The approximate value is 0.43429448190325181667.

• Math.PI. The ratio of the circumference of a circle to its diameter, expressed as pi. The approximate value is 3.14159265358979.

• Math.SQRT1_2. The reciprocal of the square root of one half. The approximate value is 0.707106781186.

• Math.SQRT2. The square root of 2. The approximate value is 1.414213562373.

Numbers are the basis of almost all object-oriented programming. In Chapter 13, you'll see a lot of ActionScript that involves using numbers.

Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319