Be careful when rearranging floating-point expressions to ensure the greatest accuracy of the result.
Subclause 184.108.40.206, paragraph 14, of the C Standard [ISO/IEC 9899:2011], states:
Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, even in the absence of overflow and underflow. Likewise, implementations cannot generally replace decimal constants to rearrange expressions. In the following fragment, rearrangements suggested by mathematical rules for real numbers are often not valid.
Failure to understand the limitations in precision of floating-point-represented numbers and their implications on the arrangement of expressions can cause unexpected arithmetic results.
|SEI CERT C++ Coding Standard||FLP01-CPP. Take care in rearranging floating-point expressions|
|ISO/IEC TR 24772:2013||Floating-point Arithmetic [PLF]|
|[ISO/IEC 9899:2011]||Subclause 220.127.116.11, "Program Execution"|