Inconsistent Math results between AS3 and Haxe->AS3

80 views
Skip to first unread message

Andreas Renberg (IQAndreas)

unread,
May 7, 2014, 12:51:18 AM5/7/14
to haxe...@googlegroups.com
I'm getting two different results running (what looks to me as) the same math code, consisting only of bitwise operators, and some division in the result. All numbers should stay below Haxe's maximum of 2^31 (i.e. 0x7FFFFFFF).

Here is the source I am using: https://gist.github.com/IQAndreas/1e2422b1f0ed95e41936 (in the Haxe version, I am compiling to target Flash 9+). Testing that code with the Flex SDK version and Haxe version 3.1.3, I got these results:


As you can see, they start out the same, then less similar, and by the fourth iteration they have already diverged beyond recognition.

Am I doing something wrong in the code, or is this expected behavior?

Mike Welsh

unread,
May 7, 2014, 2:57:48 AM5/7/14
to haxe...@googlegroups.com
Hi Andreas,

The issue is that the AS3 language promotes integer arithmetic to Number. That is, your multiplication has a type of Int->Int->Float (in Haxe terms).
On the other hand, Haxe defines the multiplication as Int->Int->Int.
This causes a difference when you multiply two large ints to produce a result bigger than 2^16, in Flash this will be a large Float that represents the result, but Haxe overflows and truncates the result to 32-bits.

If you change your AS3 version to this, you will see consistent results:
 return _seed = ( int(_seed * MULTIPLIER) % MODULUS) & MODULUS;
(You'd also get consistent results if you changed the Haxe version to use Float multiplication instead of Int, but for an RNG, you probably want the 32-bit overflow behavior.)

Mike Welsh

unread,
May 7, 2014, 3:07:39 AM5/7/14
to haxe...@googlegroups.com
BTW, there is also another subtle problem -- the result of 32-bit Int * 32-bit Int requires at most 64 bits, but a Float/Number only holds 52 bits of precision! So promoting this multiplication to Float can lose some precision in AS3. Fortunately your multiplier is small enough that this never becomes an issue.
Reply all
Reply to author
Forward
0 new messages