Versions Compared

Key

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

...

The following code tests whether a float value is denormalized in FP-strict 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 NUM06-J. Use the strictfp modifier for floating-point calculation consistency across platforms for additional information.

Code Block

strictfp public static boolean isDenormalized(float val) {
  if (val == 0) {
    return false;
  }
  if ((val > -Float.MIN_NORMAL) && (val < Float.MIN_NORMAL)) {
    return true;
  }
  return false;
}

...

Here is a small program, along with its output, that demonstrates the print representation of denormalized numbers.

Code Block

strictfp class FloatingPointFormats {
  public static void main(String[] args) {
    float x = 0x1p-125f;
    double y = 0x1p-1020;
    System.out.format("normalized float with %%e    : %e\n", x);
    System.out.format("normalized float with %%a    : %a\n", x);
    x = 0x1p-140f;
    System.out.format("denormalized float with %%e  : %e\n", x);
    System.out.format("denormalized float with %%a  : %a\n", x);
    System.out.format("normalized double with %%e   : %e\n", y);
    System.out.format("normalized double with %%a   : %a\n", y);
    y = 0x1p-1050;
    System.out.format("denormalized double with %%e : %e\n", y);
    System.out.format("denormalized double with %%a : %a\n", y);
  }
}
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

...

This noncompliant code example attempts to reduce a floating-point number to a denormalized value and then restore the value.

Code Block
bgColor#FFCCCC

float x = 1/3.0f;
System.out.println("Original    : " + x);
x = x * 7e-45f;
System.out.println("DenormalizedNormalized: " + x);
x = x / 7e-45f;
System.out.println("Restored    : " + x);

Because this operation is imprecise, this code produces the following output when run in FP-strict mode:

Code Block

Original      : 0.33333334
Denormalized  : 2.8E-45
Restored      : 0.4

...

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
bgColor#ccccff

double x = 1/3.0;
System.out.println("Original    : " + x);
x = x * 7e-45;
System.out.println("Denormalized: " + x);
x = x / 7e-45;
System.out.println("Restored    : " + x);

This code produces the following output in FP-strict mode:

Code Block

Original      : 0.3333333333333333
Denormalized  : 2.333333333333333E-45
Restored      : 0.3333333333333333

...

[Bryant 2003]

Computer Systems: A Programmer's Perspective, Section 2.4, Floating Point

[CVE 2008 ]

CVE-2010-4476

[IEEE 754]

 

 

      03. Numeric Types and Operations (NUM)      NUM06-J. Use the strictfp modifier for floating-point calculation consistency across platforms