...
Using denormalized numbers can severely impair the precision of floating-point calculations; as a result, denormalized numbers must not be used.
Detecting Denormalized Numbers
The following code tests whether a float
value is denormalized in strictfp mode, or for platforms that lack extended range support. Testing for denormalized numbers in the presence of extended range support is platform dependent; see rule "NUM09-J. Use the strictfp modifier for floating point calculation consistency across platforms" for additional information.
...
Testing whether values of type double
are denormalized is exactly analogous.
Print Representation of Denormalized Numbers
Denormalized numbers can also be troublesome because their printed representation is unusual. Floats and normalized doubles, when formatted with the %a
specifier, begin with a leading nonzero digit. Denormalized doubles can begin with a leading zero to the left of the decimal point in the mantissa.
...
Code Block |
---|
normalized float with %e : 2.350989e-38 normalized float with %a : 0x1.0p-125 denormalized float with %e : 7.174648e-43 denormalized float with %a : 0x1.0p-140 normalized double with %e : 8.900295e-308 normalized double with %a : 0x1.0p-1020 denormalized double with %e : 8.289046e-317 denormalized double with %a : 0x0.0000001p-1022 |
Noncompliant Code Example
This code attempts to reduce a floating-point number to a denormalized value and then restore the value.
...
Code Block |
---|
Original : 0.33333334 Denormalized? : 2.8E-45 Restored : 0.4 |
Compliant Solution
Do not use code that could use denormalized numbers. When calculations using float
produce denormalized numbers, use of double
can provide sufficient precision.
...
Code Block |
---|
Original : 0.3333333333333333 Denormalized? : 2.333333333333333E-45 Restored : 0.3333333333333333 |
Exceptions
NUM08-EX1: Denormalized numbers are acceptable when competent numerical analysis demonstrates that the computed values will meet all accuracy and behavioral requirements that are appropriate to the application. Note that "competent numerical analysis" generally requires a specialized professional numerical analyst; lesser levels of rigor fail to qualify for this exception.
Risk Assessment
Floating-point numbers are an approximation; denormalized floating-point numbers are a less precise approximation. Use of denormalized numbers can cause unexpected loss of precision, possibly leading to incorrect or unexpected results. Although the severity stated below for violations of this rule is low, applications that require accurate results should consider the severity of this violation to be high.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
NUM08-J | low | probable | high | P2 | L3 |
Related Guidelines
CERT C Secure Coding Standard |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f982b78e0f95077b-00af43bd-48bf4249-95a79112-f32a416b0c3e3ba2a9144779"><ac:plain-text-body><![CDATA[ | [[IEEE 754 | AA. Bibliography#IEEE 754 2006]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7ee5810d9acf5a90-dbd8a201-49914fc6-8f3a864e-fb9adcad4f5154b47e0e296a"><ac:plain-text-body><![CDATA[ | [[Bryant 2003 | AA. Bibliography#Bryant 03]] | Computer Systems: A Programmer's Perspective. Section 2.4 Floating Point | ]]></ac:plain-text-body></ac:structured-macro> |
...