I'm making a bit of a physics engine for fun. I'm trying to make it reliable even at low tickrates, so I'm doing a dangerous dance with float arithmetic and precision.
While debugging, I ended up running this code:
#define A 0.2063387632369995100000000000000f
#define B 0.7307806611061096200000000000000f
float a = A;
float b = B;
float floatie1 = A + (B * ((A)/(-B)));
float floatie2 = a + (b * ((a)/(-b)));
printf("%.40f\n", floatie1);
printf("%.40f\n", floatie2);
The output is:
0.0000000149011611938476560000000000000000
0.0000000000000000000000000000000000000000
And the bits of each(respectively):
00110010100000000000000000000000
00000000000000000000000000000000
I get that that expression is supposed to evaluate to 0.
I do not want it to evaluate to zero though, because I might have some other unrelated arithmetic somewhere in code that'll give me a value that's imprecise in the exact same way, and if I then subtract the 2 it'll be 0.
I don't want my expressions being simplified or optimized in arbitrary situations.
I've tried making the floats volatile and turning off optimizations to no avail.
I've also tried -f-associative-math
, -ftrapping-math
and -funsafe-math-optimizations
, along with the combinations of their no-
variants.
If I break up the expression, put stuff into different variables and do one thing at a time it works, but I don't wanna have to be looking behind my back every time I write a piece of code.
MSVC++ gives me the correct result with this code out of the box.
Version: gcc (MinGW.org GCC-6.3.0-1) 6.3.0
windows 8.1.
How do I turn this off?