Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7094238

summation of two Double variables has wrong result

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u26
    • other-libs
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      C:\jdk1.6.0_23\bin>javac -version
      javac 1.6.0_23

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      OS Name Microsoft Windows 7 Home Premium
      Version 6.1.7600 Build 7600
      Other OS Description Not Available
      OS Manufacturer Microsoft Corporation
      System Manufacturer Dell Inc.
      System Model Inspiron N5030
      System Type x64-based PC
      Processor Pentium(R) Dual-Core CPU T4500 @ 2.30GHz, 2300 Mhz, 2 Core(s), 2 Logical Processor(s)
      BIOS Version/Date Dell Inc. A01, 7/23/2010
      SMBIOS Version 2.4
      Windows Directory C:\Windows
      System Directory C:\Windows\system32
      Boot Device \Device\HarddiskVolume2
      Hardware Abstraction Layer Version = "6.1.7600.16385"
      Installed Physical Memory (RAM) 3.00 GB
        Total Physical Memory 2.96 GB
      Available Physical Memory 860 MB
        Total Virtual Memory 5.92 GB
      Available Virtual Memory 2.84 GB
      Page File Space 2.96 GB
      Page File C:\pagefile.sys

      A DESCRIPTION OF THE PROBLEM :
      ...
      } else {
        Double d = num / divisor;
        sum += d; // sum is 6.6 and d is 0.06, result is 6.659999999999999
        divisor = divisor * 10.0;
      }
      ...
      i use this block of code in my Calculator utility which performs basic math operation on a string of values and operators (like "1+2-3.7+0.0085")

      REGRESSION. Last worked in version 6u26

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the code supplied below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      6.6 + 0.06 = 6.66
      ACTUAL -
      6.6 + 0.06 = 6.659999999999999

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      no error messages

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package cmdutils;

      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;

      /**
       * Created by IntelliJ IDEA.
       * User: ilja
       * Date: 9/22/11
       * Time: 2:05 PM
       * To change this template use File | Settings | File Templates.
       */
      public class Calc {
          private static char[] expr;
          private static int i = 0;

          private static double parseVal(int index) {
              Double num = 0.0;
              Double sum = 0.0;
              Double divisor = 10.0;
              boolean afterDot = false;

              while (index < expr.length) {

                  if ((expr[index] >= '0' && expr[index] <= '9') || expr[index] == '.') {
                      if (expr[index] == '.') {
                          afterDot = true;
                      } else {

                          num = (double) ((int) (expr[index] - '0'));

                          if (!afterDot) {
                              sum = sum * 10.0 + num;
                          } else {
                              Double d = num / divisor;
                              sum += d;
                              divisor = divisor * 10.0;
                          }
                      }
                  } else {
                      break;
                  }
                  index++;

              }
              i = index;
              return sum;

          }

          public static void main(String[] args) throws Exception {
              expr = args[0].toCharArray();
              List<Double> realNumbers = new ArrayList<Double>();
              List<Character> operators = new ArrayList<Character>();
              boolean outOfRange = false;

              // get 2 lists: numbers and operators
              while (i < expr.length) {
                  if (expr[i] >= '0' && expr[i] <= '9') {
                      realNumbers.add(parseVal(i));
                  }
                  outOfRange = i >= expr.length;
                  if(outOfRange)
                      break;
                  if (expr[i] == '+' || expr[i] == '-') {
                      operators.add(expr[i]);
                  }

                  i++;
              }
              // calculate result using the two lists
              Iterator iVal = realNumbers.iterator();
              Iterator iOper = operators.iterator();

              Double num = Double.parseDouble(iVal.next().toString());

              while (iVal.hasNext() && iOper.hasNext()) {
                  String op = iOper.next().toString();
                  String sval = iVal.next().toString();
                  Double val = Double.parseDouble(sval);
                  if (op.equals("+")) {
                      num += val;
                  }
                  if (op.equals("-")) {
                      num -= val;
                  }
              }
              System.out.print(num);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      i do not know how to workaround this

      SUPPORT :
      YES

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: