Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Actionscript 3, why is the dynamic type of large int values Number?

I have noticed that if I create an int with a large value, the dynamic type of the object seems to be Number.

Example:

var int1:int = 0x8000000;
var type1:String = flash.utils.getQualifiedClassName(int1); // Returns "int"

var int2:int = 0x10000000;
var type2:String = flash.utils.getQualifiedClassName(int2); // Returns "Number"

What is going on here? Both of the values are well below the maximum value of an int, which is 2,147,483,647 (2^31-1).

like image 790
user829876 Avatar asked Feb 25 '12 19:02

user829876


1 Answers

What's going on is that the AS3 virtual machine uses 32 bit "atoms" to store the values of primitive types.

3 of those 32 bits are used for describing the type, meaning there are 29 bits left.

Since int is a signed type - using 1 bit for the sign - that leaves 28 bits for a positive integer. Making the highest number you can write in the remaining 28 bits:

0x0FFFFFFF = 268435455

As soon as the integer needs more than 29 bits, the atom is changed by the VM into a Number type (which is really represented as a 29 bit pointer to the actual 64 bit double precision float).

So, the value defined as "maximum value of an int" doesn't really make all that much sense (since Number can store integral values up to 53 bits, but int will limit it to 32) - other than in terms of:

  • communicating with other languages/technologies (which mostly use 32 bits for their integers)

  • making the results (mostly) predictable to programmers used to 32 bit integers; and

  • future-proofing (in case the internal representation changes in the future).

As a sidenote, there are quite a few peculiarities with ints and Numbers - including that this:

var i:Number = 2.0;

... is stored as int until the decimals are actually needed.

like image 63
JimmiTh Avatar answered Nov 15 '22 21:11

JimmiTh