JDK-4421899 : Multiplication of any integer multiple of 5 with 0.000001 faulty!
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2001-03-06
  • Updated: 2001-03-28
  • Resolved: 2001-03-28
Related Reports
Relates :  
Relates :  
Description

Name: yyT116575			Date: 03/05/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)


Test the following source code:

//----------------------------
import java.lang.*;

public class test2 {

  public static void main(String[] argv) {
    double counter;
    counter=Double.parseDouble(argv[0]);
    double factor=0.000001;
    
    System.out.println("test:"+Double.toString(counter*factor));
    
  }
}
// --------------------------------------


after compiling call this program with 'java test2 5'
the result should be: 5.0E-6 but it isn't. it is: 4.9999999999999996E-6.
Something like this happens for all values which are integer multiples of 5.
 5-> 4.9999999999999996E-6
10-> 9.9999999999999998E-6
15-> 1.4444444444444449E-5
20-> 1.9999999999999998E-5
...

Calling the program with other integers, for example 4, produces the expected
output: '4.0E-6'.
The problem stays the same with negative values. With zero (0) everything is
okay.

I tested the program on a Compaq Armada 1592DT running RedHat Linux 6.2 and on a
Windows 98 Pentium III 650 MHz with JRE1.3 running on both systems.
(Review ID: 118143) 
======================================================================

Comments
EVALUATION The intended decimal factor in question, 0.000001, cannot be exactly represented as a binary floating-point value. As a binary floating-point value, the string "0.000001" get converted to a floating-point value numerically equal to 0.000000999999999999999954748111825886258685613938723690807819366455078125, the binary floating-point value closest to the value in question. Therefore, the multiplications in question will not yield the expected exact values. Moreover, the binary -> decimal conversion process is also inexact. Closing as not a bug. joe.darcy@eng 2001-03-27
27-03-2001

PUBLIC COMMENTS The multiplication is not fautly; the decimal value in question cannot be exactly represented as a binary floating-point value, which leads to the surprising results. See evaluation for details. joe.darcy@eng 2001-03-27
27-03-2001