Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Care must be taken Be careful when rearranging floating-point expressions to ensure the greatest accuracy of the result.

Wiki MarkupAccording to C99, Section Subclause 5.1.2.3, "Program execution" \[[, paragraph 14, of the C Standard [ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]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.

Code Block

double x, y, z;
/* ... */
x = (x * y) * z; /* not equivalent to x *= y * z; */
z = (x - y) + y ; /* not equivalent to z = x; */
z = x + x * y; /* not equivalent to z = x * (1.0 + y); */
y = x / 5.0; /* not equivalent to y = x * 0.2; */

...

Failure to understand the limitations in precision of floating-point-represented numbers and the their implications of this on the arrangement of expressions , can cause unexpected arithmetic results.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

FLP01-C

low Low

probable Probable

high High

P2

L3

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule recommendation on the CERT website.

Other Languages

...

Related Guidelines

...

...

...

References

...

ISO/IEC TR 24772:2013Floating-point Arithmetic [PLF]

Bibliography

...

...

2011]Subclause 5.1.2.3,

...

"Program Execution"

 

...

Image Added Image Added Image Added execution" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "PLF Floating Point Arithmetic"FLP00-C. Understand the limitations of floating point numbers      05. Floating Point (FLP)       FLP02-C. Consider avoiding floating point numbers when precise computation is needed