Fused multiply-add instructions can be counter-intuitive
Example: 0.1 * 10.0 - 1.0
Normally, there are 2 operations done here and there are 2 roundings that occur. FMA instructions do multiply and add operations with a single rounding operation and results can be counter-intuitive.
We know:
-
mathematically, the result is exactly 0.0
-
in floating-point, 0.1 cannot be represented exactly (it is 0.10000000149011612)
Done normally as 2 operations:
First, 0.10000000149011612 * 10.0 is 1.0000000149011612 and is rounded to 1.0 ( 1.0 is the closest float that can represent the result 1.0000000149011612 ).
Second, 1.0 - 1.0 is 0.0.
0.0 is the exact float value of the result.
Done as single FMA operation:
0.10000000149011612 * 10.0 - 1.0 is 0.00000001490116119384765625.
0.00000001490116119384765625 is the exact float value of the result.
So!
It can be the case that doing 2 operations (rounding twice) is actually closer to the mathematically exact answer than using FMA (rounding once).