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

Two Doubles are not equal when compared using '=='

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u211
    • core-libs
    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      double says 1.0 = 1.0
      but Double says 1.0 does not equal 1.0





      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      double test1 = 1.0;
      double test2 = 1.0;
      System.out.println(test1 == test2); //This prints true
              
      Double test3 = 1.0;
      Double test4 = 1.0;
      System.out.println(test3 == test4); //This prints false

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Please have Double recognize that 1.0 equals 1.0

      CUSTOMER SUBMITTED WORKAROUND :
      If Double's == doesn't work, can the code for Double's == be replaced with something like this:

      public static void main(String[] args){
              Double test3 = 1.0;
              Double test4 = 1.0;
              System.out.println(test3 == test4); //This prints false (when true is expected)
              
              System.out.println(isCloseEnough(test3, test4)); //This prints true (preferred if == gives this result rather then relying on method call)
      }

      //feel free to adjust the number of zeros
      public static Boolean isCloseEnough(Double double1, Double double2){
              Double subtract = double1 - double2;
              if(subtract < .000000000000001 && subtract > -0.000000000000001){
                  return true;
              }else{
                  return false;
              }
          }

      FREQUENCY : always


            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: