In C++ optimizers you can specify the use of fast floating point arithmetic instead of precise floating point arithmetic. The difference is that in fast mode it will do optimizations that would apply to real world math, but may not produce the correct results when done on a computer.
An example of this can be shown using GodBolt using the following code and the -O2
optimization. If you are using clang or gcc you can put in the -ffast-math
parameter to view what the code looks like with these optimizations.
double mulBy6DivBy12(double x)
{ return 6 * x / 12; }
double divBy2(double x)
{ return x / 2; }
Without the -ffast-math
parameter it will generate a multiply and divide operation in the mulBy6DivBy12
function, with it on the two functions will look the same.
What I want to know is at runtime, when being processed by the JIT does it do these potentially unsafe optimizations?