Representation of floats



On 05/16/2012 09:05 AM, Evgeniy Maevskiy wrote:
> (%i1) x:makelist(1/5^k,k,1,7);
> (%o1) [0.2,0.04,0.008,0.0016,3.2000000000000003E-4,6.3999999999999997E-5,
> 1.2799999999999999E-5]
>
> but Firefox-javascript (for example) returns precise values:
>
> 0.2
> 0.04
> 0.008
> 0.0016
> 0.00032
>
> (Windows XP, Maxima 5.27.0, Firefox 11.0)
In Ubuntu, Firefox 11.10 javascript does not show the results rounded. 
Consider for instance the page:

<html><body>

<script type="text/javascript">

document.write(0.2+0.2+0.2);

if (0.2+0.2+0.2 == 0.6)

   {

   document.write("<p>Yes</p>");

   }

else

   {

   document.write("<p>No</p>");

   }

</script></body></html>


It gives me the result

0.6000000000000001

No

Languages that show you the results rounded might lead you into 
confusion. Consider for instance PHP 5 (in Ubuntu):

print 0.2+0.2+0.2."\n";
if (0.2+0.2+0.2 == 0.6) {
     print "Yes\n";
} else {
print "No\n";
}

The result is:

0.6
No

which will make you think that the two results contradict each other. 
Python and Maxima
also show you the result without rounding.

Python 2.7.2:

>>>  0.2+0.2+0.2
0.6000000000000001
>>>  if (0.2+0.2+0.2) == 0.6:
...     print 'yes'
... else:
...     print 'no'
...
no

Maxima 5.25:

(%i1) 0.2+0.2+0.2;
(%o1) .6000000000000001
(%i2) if (0.2+0.2+0.2 = 0.6) then "yes" else "no";
(%o2) no


Regards,
Jaime