Versions Compared

Key

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

...

The Java language provides two primitive floating-point types, float and double, which are associated with the single-precision 32-bit and double-precision 64-bit format values and operations specified by IEEE 754 [IEEE 754]. Each of the floating-point types has a fixed, limited number of mantissa bits. Consequently, it is impossible to precisely represent any irrational number (for example, pi). Further, because these types use a binary mantissa, they cannot precisely represent many finite decimal numbers, such as 0.1, because these numbers have repeating binary representations.

...

When precise computation is unnecessary, floating-point representations may be used. In these cases, you must carefully and methodically estimate the maximum cumulative error of the computations to ensure that the resulting error is within acceptable tolerances. Consider using numerical analysis to properly understand the problem. See Goldberg's work for an introduction to this topic [Goldberg 1991].

Noncompliant Code Example

...

The use of floating-point is not recommended for performance reasons on Android.

Bibliography

[Bloch 2008]

Item 48. Avoid float and double if exact answers are required

[Bloch 2005]

Puzzle 2. Time for a change

[Goldberg 1991]

 

[IEEE 754]

 

[JLS 2005]

§4.2.3, Floating-Point Types, Formats, and Values

 

...

      Rule 03: Numeric Types and Operations (NUM)Image Added      NUM05-J. Do not use denormalized numbers