Versions Compared

Key

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

...

Denormalized numbers can severely impair the precision of floating point numbers and should not be used.

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. If calculations using float are producing denormalized numbers, use double instead.

...

Code Block
Original      : 0.3333333333333333
Denormalized? : 2.333333333333333E-45
Restored      : 0.3333333333333333

Risk Assessment

Floating point numbers are an approximation; using subnormal floating point number are a worse approximation.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FLP03-J

low

probable

high

P2

L3

Related Guidelines

CERT C Secure Coding Standard FLP05-C. Don't use denormalized numbers

Bibliography

Wiki Markup
\[[IEEE 754|AA. Bibliography#IEEE 754 2006]\]
\[[Bryant 2003|AA. Bibliography#Bryant 03]\] Computer Systems: A Programmer's Perspective. Section 2.4 Floating Point

...