ActionScript 3 Number data type problem with long integer valuesPosted in ActionScript 3 by Branislav Abadjimarinov on 6/23/2010 4:22:00 PM - CST
Lately I've been doing some front-end flex programming and in a series of posts starting with this one I'll share my view of the Adobe's programming framework. First I'll start with ActionScript 3 - the programming language used in flex. It is an EcmaScript derived language. From what I've seen for a month or so programming there are two basic rules in the ActionScript world:
- The owls are not what they seem and
- There are a lot of owls
Maybe it is a good choice in flash with its simple syntax and
agility but when it comes to enterprise programming I've found that
it fails too often to accomplish simple tasks. As a .net refugee I
expect at least the basic language features to be working
Consider a case in which you need a really big integer number to be passed to the UI of your flex application. Let's say that it is bigger than 32bits. There is a special data type Number in ActionScript to preserve 64bit values and it seems to do it well ... except when your integer number is bigger than 16 digits. In that case god knows what happens to your number.
var reallyBigNumber:Number = Number(2885118563511697402); trace(reallyBigNumber); // traced value is 2885118563511697400 var asString:String = reallyBigNumber.toPrecision(21); trace(asString); // traced value is 2885118563511697408.00
Not only your number is "rounded" to a different value but when you call toPrecission function (which is expected to give you more precise representation of the number) you get a whole new third value. To bring even more confusion the Flash builder's watch shows that reallyBigNumber's value is 2885118563511697408 which is not what trace prints to the console. All the provided numbers are far from the max value of the Number data type. Go figure.
Maybe the Number data type is perfect for flash animations where you have two trees and five owls flying around but when you build an enterprise framework you got to have strong foundations.
For an year or so about 2 or 3 of my work collegues also hit the
same issue in several different areas. Also, as Art commented on my
previous post, this is not a bug but rather a tech limitation of
actionscript 3 and hence flex limitation also.
In a nutshell the actionscript Number data type is implemented by the IEEE-754 standard. The IEEE-754 is a standard for doble precission floating point numbers. So the equivalent of actionscript 3 Number type is java and .net's double. This is the reason for loosing precission when transfering java long values to actionscript Number.