Log inSkip to content

May 3rd, 2008

Are there real int and uint types?

In Actionscript 3 all types, even the primitive types are considered as classes, so String, Number, Boolean, int are such ones, too. By invoking getQualifiedClassName() we can get the fully qualified class name of an object:

1
2
3
4
5
var n_num: Number;
trace(getQualifiedClassName(n_num)); // Number
 
var n_int: int;
trace(getQualifiedClassName(n_int)); // int

This outputs the result as expected: Number and int. The uint however seems to be somewhat different. If a value of uint type passed to getClassByName(), one would except that the result will be “uint”, but actually this is not true.

1
2
var n_uint: uint;
trace(getQualifiedClassName(n_uint)); // int

The result is “int” instead of “uint”. Well, that’s not a big surprise, I can imagine that unsigned integers are internally handled by the same class as signed ones. But the situation is more complex, not only uint behaves weird:

1
2
3
4
5
6
7
8
var n_num: Number = 12;
trace(getQualifiedClassName(n_num)); // int
 
var n_int: int = 12;
trace(getQualifiedClassName(n_int)); // int
 
var n_uint: uint = 12;
trace(getQualifiedClassName(n_uint)); // int

If I overwrite the default values and assign the same integer value to all three variables even n_num (of type Number) will be considered as of class int! If I assign a non-integer value to n_num, the result will be Number as expected:

1
2
var n_num: Number = 12.3;
trace(getQualifiedClassName(n_num)); // Number

Does it mean that the class depends on the actual value of the variable? At the first look it seems to be possible, but when I checked whether the class definition (class object) of int and Number are really different, I found that they are not!

1
2
3
4
5
6
7
8
9
10
var n_num: Number = 12.3;
trace(n_num["constructor"]); // [class Number]
 
var n_int: int = 12;
trace(n_int["constructor"]); // [class Number]
 
var n_uint: uint = 12;
trace(n_uint["constructor"]); // [class Number]
 
trace(n_num["constructor"] == n_int["constructor"]); // true

All three numeric types are handled by the same Number class. It doesn’t mean however that both integer and real numbers are handled similarly by the Number class, but their classes are actually the same.

Earlier there was another weird issue with uint, namely that sometimes its type was changed from int to Number and sometimes back from Number to int. This occured with certain values assigned to the variable. I met this issue some times ago, but it seems to be not reproductible now, probably a recent player update corrected the problem. At last I provide here some links where the actual explanation for this issue can be found (see the comments, too) and some general information related to numbers in Actionscript 3 are also collected there:

Ryan Christensen: AS3 Interesting Numeric Storage Behavior
Andre Michelle: Weird behavior of numbers in as3
Michael Baczynski: Bitwise gems – fast integer math
Michael Baczynski: Int, uint and number data type conversion
Sho Kuwamoto: Avoid ints in ActionScript
Grant Skinner: Types in AS3: ints not so fast, uints slow!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">



Calendar

May 2008
S M T W T F S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Categories