I’m testing a photo application for Facebook. I’m getting object IDs from the Facebook API, but I received some incorrect ones, which doesn’t make sense – why would Facebook send wrong IDs? I investigated a bit and found out that numbers with 17 and more digits are automatically turning into even numbers!
For example, let’s say the ID I’m supposed to receive from Facebook is 12345678912345679. In the debugger, I’ve noticed that Flash Player automatically turns it into 12345678912345678. And I even tried to manually set it back to an odd number, but it keeps changing back to even.
Is there any way to stop Flash Player from rounding the numbers? BTW the variable is defined as Object, I receive it like that from Facebook.
This is related to the implementation of data types:
intis a 32-bit number, with an even distribution of positive andnegative values, including 0. So the maximum value is
(2^32 / 2 ) - 1 == 2,147,483,647.uintis also a 32-bit number, but it doesn’t have negative values. So themaximum value is
2^32 - 1 == 4,294,967,295.When you use a numerical value greater than the maximum value of
intoruint, it is automatically cast toNumber. From the Adobe Doc:53 bits have a maximum value of:
2^53 - 1 == 9,007,199,254,740,989=> 16 digitsSo when you use any value greater than that, the inner workings of floating point numbers apply.
You can read about floating point numbers here, but in short, for any floating point value, the first couple of bits are used to specify a multiplication factor, which determines the location of the point. This allows for a greater range of values than are actually possible to represent with the number of bits available – at the cost of reduced precision.
When you have a value greater than the maximum possible integer value a Number could have, the least significant bit (the one representing 0 and 1) is cut off to allow for a more significant bit (the one representing 2^54) to exist => hence, you lose the odd numbers.
There is a simple way to get around this: Keep all your IDs as Strings – they can have as many digits as your system has available bytes 😉 It’s unlikely you’re going to do any calculations with them, anyway.
By the way, if you had a value greater than
2^54-(1+2), your numbers would be rounded down to the next multiple of 4; if you had a value greater than2^55-(1+2+4), they would be rounded down to the next multiple of 8, etc.