I am trying to build a deterministic simulation, where the floating point numbers are truncated off via the following function:
(I found it here: http://joshblog.net/2007/01/30/flash-floating-point-number-errors )
return Math.round(10000 * float) / 10000;
My question is: wouldn’t the fact that I am dividing it by 10000, be in itself a source of floatpoint errors?
IE every time a dividision takes place, it will result in a new float with further possible nondeterministic results.
EDIT:
how about this? using only powers of 2
return Math.round(float* 1024) / 1024;
My goal was to achieve greater consistency across platforms(C# / AS3 and 32/64 bits), while I accept that 100% consistency is not possible.
(due to AS3 not capable of real integer math, as all internal operations are performed via floats)
What I have gathered so far( Thanks to Eric Postpischil and Jeffrey Sax):
Out of the above, the “Math.round(1024 * float)” operation may NOT produce identical results on all platforms, if the “errors have accumulated to more than half of the quantum” which is possible even “within a single operation“.
.
Where as for the “/ 1024” part, as 1024 is a power of 2, that is a straight bit shift, it will NOT introduce extra errors, where as if I divided by 1000 that would introduce a small chance of an extra error, as 1000 cannot be perfectly represented. So a division with 1000 could introduce another error after the rounding which the division by 1024 could not.
.
CONCLUSION: Math.round(1024 * float) / 1024; is better than Math.round(1000 * float) / 1000; although neither of them is perfect.
Is this an accurate statement?