Bug: Windows target, incorrect float maths results

I’ve come across a very specific Maths bug which is only occuring on Windows targets. Here’s a verbal description, and example code - the maths in this example work fine for flash, html, neko. simplest form code below to demonstrate the error.

Verbally, a dynamic array, contains a two dimensional array of Floats, The error occurs when multiplying a float from the array with another number. in most cases the maths will be correct, however whenever the Float within the array is smaller in magnitude tham one, (i.e. between -1 and 1) , the result of the multiplication will incorrectly be given as 0 !

var a:Array<Float> = new Array();
a[0] = 0.5;    // incorrect maths results when -1 < a[0] < 1 

var b:Array<Array<Float>> = new Array();
b.push(a);

var c:Array<Dynamic> = new Array();
c.push(b);

var m:Float = 0.5;  // any number
trace(c[0][0][0] + " * " + m + " = " + (c[0][0][0] * m));

Does it work differently if you type everything as Float rather than Dynamic?

Instead of:

var a:Array<Float> = new Array();

Can you try

var a:Array<Float> = new Array<Float>();

I cannot type the “c” array as Float, as it contains arrays not Floats !!

the maths still fails when defining var a using

var a:Array<Float> = new Array<Float>();

If i define c using

var c:Array<Array<Array<Float>>> = new Array();

the maths will then work in this simplified example … but the maths still fail when c is a dynamic array. (all this means is i’ve over simplified the problem when creating this example, my c array is a display list with various members, rather than c just being a 3 dimensional array)

Can you type it as anything? DisplayObject?

Don’t use Dynamic When you use Dynamic the compiler can’t check types or apply optimizations, is error prone and the runtime behavior will be unspecified. http://haxe.org/manual/types-dynamic.html

You have a few options:

  • Hint the compiler what the expected type of the value will be.
  • Let the compiler infer the type.
  • Correctly type the variable with var c:Array<Array<Array<Float>>> ...

Example: http://try.haxe.org/#9428A