title: Java double equality
body: <p>I'm having a problem in comparing doubles. The comparison is responsible for stopping a while cycle. The code runs fine, but then suddenly, the cycle never stops. The values from centroidList's dimVal are being compared to the temp variable which is calculated(the bold bit). The code always enters the if, it doesn't matter if I use "!=" or "==". Printed out the values, they are exactly the same. What is wrong?</p>
  
  <pre><code>    package clusters;
  
  import java.util.LinkedList;
  
  public class KMeansV2 {
  
      LinkedList&lt;Record&gt; table;
  LinkedList&lt;Centroid&gt; centroidList;
  LinkedList&lt;Double&gt; intervalList;
  boolean clusterStop;
  
  int meassureType;
  int prec=10000000;
  
  KMeansV2()
  {
      Read read=new Read(true,"BrCa_HD_full.xlsx");
      table=new LinkedList&lt;Record&gt;(read.table);
      centroidList=new LinkedList&lt;Centroid&gt;();
      CreateCentroids(2);
      SetMeassureType(1);
      while(clusterStop==false)
      {
          UpdateRecords();
          UpdateClusters();
      }
      Output();
  }
  
  public void SetMeassureType(int meassureType)
  {
      this.meassureType=meassureType;
  }
  
  public void CreateCentroids(int centroidCount)
  {
      if(centroidList.isEmpty())
      {
          for(int i=0;i&lt;centroidCount;i++)
          {
                  centroidList.add(new Centroid(table.get(0).values.size(),i));
          }
      }
      else
      {
          centroidList.clear();
          for(int i=0;i&lt;centroidCount;i++)
          {
                  centroidList.add(new Centroid(table.get(0).values.size(),i));
          }
      }
  
  }
  
  public void UpdateRecords()
  {
      for(int i=0;i&lt;table.size();i++)
      {
          table.get(i).Update(centroidList, meassureType);
      }
  }
  
  public void UpdateClusters()
  {
      clusterStop=true;
      for(int i=0;i&lt;centroidList.size();i++) //staiga pa centroidiem
      {
          for(int j=0;j&lt;table.get(0).values.size();j++) //staiga pa kolonnam
          {
              double sum=0;
              double count=0;
              for(int k=0;k&lt;table.size();k++) //staiga pa rindam
              {
                  if(centroidList.get(i).type==table.get(k).type)
                  {
                      sum+=table.get(k).values.get(j);
                      count++;
                  }
              }
              System.out.println(clusterStop);
  
              double temp=(1/count)*sum;
  
              **if(centroidList.get(i).dimVal.get(j)==temp);
              {
                  System.out.println(centroidList.get(i).dimVal.get(j)+" != "+(1/count)*sum);
                  clusterStop=false;
              }**
  
              centroidList.get(i).dimVal.set(j,temp);
          }
          System.out.println(clusterStop);
      }
  }
  
  public void Output()
  {
      LinkedList&lt;String&gt; types=new LinkedList&lt;String&gt;();
      for(int i=0;i&lt;table.size();i++)
      {
          if(!types.contains(table.get(i).realType))
          {
              types.add(table.get(i).realType);
          }   
      }   
  
      for(int i=0;i&lt;centroidList.size();i++) //staiga pa centroidiem
      {       
          for(int j=0;j&lt;types.size();j++) //staiga pa klasem
          {
              int count=0;
              for(int k=0;k&lt;table.size();k++) // staiga pa rindam
              {
                  if(table.get(k).type==i &amp;&amp; table.get(k).realType.equals(types.get(j)))
                  {
                          count++;    
                  }
              }
              System.out.println("Centroid "+(i+1)+" has "+count+" of type "+types.get(j));
              //kMeansUI.UpdateLog("Centroid "+(i+1)+" has "+count+" of type "+types.get(j));
          }
      }
  
      for(int i=0;i&lt;centroidList.size();i++)
      {
          int count=0;
          for(int j=0;j&lt;table.size();j++)
          {
              if(table.get(j).type==i)
              {
                  count++;
              }
          }
          System.out.println("Cluster "+i+" has "+count+" records.");
          //kMeansUI.UpdateLog("Cluster "+(i+1)+" has "+count+" records.");
      }
      //kMeansUI.UpdateLog("/-------------------------------------------------------------------------/");
  
  }
  
  public static void main(String[] args)
  {
      KMeansV2 test=new KMeansV2();
  }
  </code></pre>
  
  <p>}</p>