Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / ActionScript Technology Center / Learning ActionScript 3 /

ActionScript 3 fundamentals: Numbers and Math

by Michelle Yaiser

Michelle Yaiser
  • Adobe
  • michelleyaiser.com

Content

  • Types of numbers
  • Numerical data types in ActionScript 3
  • Math class
  • Where to go from here
  • Acknowledgement

Created

24 October 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Flash Builder Flash Professional

Requirements

Prerequisite knowledge

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

User level

Beginning

Required products

  • Flash Builder (Download trial)
  • Flash Professional (Download trial)

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.

Types of numbers

Natural numbers

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

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

Real numbers

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.

Numerical data types in ActionScript 3

Number data type

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.

Storage of floating-point numbers

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 265 – 1. By using 11 bits to store an exponent, the Number data type can raise its significand to a power of 21023.

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.

NaN and other special Number values

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.

int data type

The int data type includes all positive and negative whole numbers from –2,147,483,648 (–231) through 2,147,483,647 (231 – 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.

uint data type

The uint data type includes all positive whole numbers from 0 through 4,294,967,295 (232 – 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.

Math class

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

Where to go from here

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.

Acknowledgement

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.

More Like This

  • ActionScript 3 fundamentals: Data types
  • ActionScript 3 fundamentals: Syntax
  • ActionScript 3 fundamentals: Variables
  • ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries
  • AS3 fundamentals: Conditionals
  • ActionScript 3 fundamentals: Arrays
  • Garbage collection internals for Flash Player and Adobe AIR
  • ActionScript 3 fundamentals: Functions
  • ActionScript 3 fundamentals: Packages
  • ActionScript 3 fundamentals: Operators

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement