|  | Posted by Tim Roberts on 06/15/30 11:30 
"Sander Peters" <sanderpeters@yahoo.com> wrote:>
 >I have the following situation:
 >
 >$a=5.6000;
 >$b=8.9000;
 >$c=($a+$b);
 >
 >echo $c;
 >
 >I will get the value '14.5' back from PHP.
 
 Maybe.  I'm sure you will shortly be coming up against the NEXT surprising
 truth about floating point values, and that is that 5.6 and 8.9 cannot be
 represented exactly in binary.  Just like trying to represent 1/3 in
 decimal, values like 5.6 and 8.9 requires an infinite number of bits.  So,
 the value you actually get is only an approximation.
 
 Because of that, 5.6 + 8.9 does not actually equal 14.5 (which CAN be
 represented exactly in binary).  It will PRINT as 14.5, because printing
 rounds it, but if you do this:
 
 if( $c == 14.5 )
 echo "yes"
 
 it will not actually print "yes".
 --
 - Tim Roberts, timr@probo.com
 Providenza & Boekelheide, Inc.
  Navigation: [Reply to this message] |