title: Java: understanding double (and normalization)
body: <p>I'm trying to learn a bit on robotics and Java. In my attempt I have written a function that (should) take a uniformed array called q(0.2,0.2,0.2,0.2,0.2), multiply each value in this array by 0.6 or 0.2 based on value Z, compared to another array world (0,1,1,0,0) that represents hit or miss of value Z compared to array world and finally return a new uniformed array.</p>
  
  <p>So:
  1.) Loop through (0,1,1,0,0)
  2.) if i!=Z than i*0.2, if i=Z than i*0.6
  3.) sum all new values in q to a double called normalize
  4.) normalize each value in q (value / normalize)</p>
  
  <p>Below is the function:</p>
  
  <pre><code>    public static final double pHit = 0.6;
      public static final double pMiss = 0.2;
      public static int[] world = {0,1,1,0,0};
      public static List&lt;Double&gt; q = new ArrayList&lt;Double&gt;();
  
      public static List&lt;Double&gt; sense(int Z){
          double normalize = 0;
          for(int i=0;i &lt; q.size();i++){
              if(Z == world[i]){
                  q.set(i, (q.get(i) * pHit));
              }
              else{
                  q.set(i, (q.get(i) * pMiss));
              }
          }
          //Normalize
          for(int i=0;i &lt; q.size();i++){
              normalize += q.get(i);
          }
          for(int i=0;i&lt;q.size();i++){
              q.set(i, q.get(i)/normalize);
          }
          return q;   
      }
  </code></pre>
  
  <p>If I set world to (0,1,0,0,0) and Z to 1 I get following results:
  0.14285714285714288
  0.4285714285714285
  0.14285714285714288
  0.14285714285714288
  0.14285714285714288</p>
  
  <p>This normalizes nicely (sum = 1).</p>
  
  <p>But if I set world to (0,1,1,0,0) and Z to 1 I get a strange result:
  0.1111111111111111
  0.3333333333333332
  0.3333333333333332
  0.1111111111111111
  0.1111111111111111</p>
  
  <p>This "normalizes" to 0.9999999999999998 ?</p>
  
  <p>Many thanks for any input!</p>