title: are floating point numbers changed when sorted in perl?
body: <p>I'm running a statistical bootstrap at 10k permutations, which I'm trying to compare against an observed value. The observed is supposed to be identical to the max of the 10k permutations. The way I am measuring this is by attempting to find its percentile.</p>
  
  <p>All results of the 10k permutations (10,000 random numbers) are stored in an array, which I sort using:</p>
  
  <pre><code>my @sorted = sort {$a &lt;=&gt; $b} @permutednumbers;
  </code></pre>
  
  <p>When I then compare the observed value <code>$truevalue</code>, I'm getting an inaccurate comparison. These are stored as floating point numbers. The bootstrapping procedure uses the same formula for generating the random number so it should be absolutely identical, but when comparing the same value, it becomes inaccurate. I'm testing this with:</p>
  
  <pre><code>if ($sorted[$#sorted] == $truevalue) {
    print "sorted: $sorted[$#sorted] is eq truevalue:$truevalue\n";
  } elsif ($sorted[$#sorted] &gt; $truevalue) {
    print "sorted: $sorted[$#sorted] is gt truevalue:$truevalue\n";
  } elsif ($sorted[$#sorted] &lt; $truevalue) {
     print "sorted: $sorted[$#sorted] is lt truevalue:$truevalue, totalpermvalues; $totalpermvalues\n";
  }
  </code></pre>
  
  <p>output: </p>
  
  <pre><code>sorted: 0.937864522389543 is gt truevalue:0.937864522389543
  </code></pre>
  
  <p>So I get that floating point numbers aren't printed in complete accuracy, but I always assumed internally the computer stores the correct numbers. Is that not a correct assumption? Of course I can fix this quickly by changing them into integers of some sort, but is this something that I should be doing automatically all the time? Are floating point numbers just dangerous to use? Those exact values should be identical given that they are outputs of identical inputs, which is what is confusing me...</p>
  
  <p>If this matters, the values are individually calculated using the linear_interpolate function in Math::Interpolate package, but the inputs are identical.</p>