Discussion:
Representation of floats
Evgeniy Maevskiy
2012-05-16 08:05:31 UTC
Permalink
In floats (i.e. numer:true) we have:

(%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

etc. (at least up to k=20)

Limits of the FLOAT in Maxima and Firefox are the same: 1e-324 - 1e309

(Windows XP, Maxima 5.27.0, Firefox 11.0)
Richard Fateman
2012-05-16 14:20:03 UTC
Permalink
On 5/16/2012 1:05 AM, Evgeniy Maevskiy wrote:
> In floats (i.e. numer:true) we have:
>
> (%i1)
> (%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
>
>
No, it is not representing values precisely. It is just printing them
with fewer digits.

try fpprintprec:5; numer:true;

x:makelist(1/5^k,k,1,7);
[0.2,0.04,0.008,0.0016,3.2*10^-4,6.4*10^-5,1.28*10^-5]
Stavros Macrakis
2012-05-16 14:27:07 UTC
Permalink
1/5^k cannot be represented exactly as a binary floating-point number for
k>0. For example, 0.2 is actually represented as 3602879701896397/2^54.

Javascript floating point is not exact, either, it is simply rounding to
fewer digits when printing.

To round to fewer digits when printing in Maxima, use fpprintprec;
e.g. fpprintprec:14
to round to 14 digits.

-s

On Wed, May 16, 2012 at 4:05 AM, Evgeniy Maevskiy <***@e-math.ru>wrote:

> In floats (i.e. numer:true) we have:
>
> (%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
>
> etc. (at least up to k=20)
>
> Limits of the FLOAT in Maxima and Firefox are the same: 1e-324 - 1e309
>
> (Windows XP, Maxima 5.27.0, Firefox 11.0)
>
>
>
>
> _______________________________________________
> Maxima mailing list
> ***@math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
Jaime Villate
2012-05-16 19:54:00 UTC
Permalink
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
Volker van Nek
2012-05-17 10:16:57 UTC
Permalink
For the record: With the recent debian version of Maxima on xubuntu12.04:

Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (a.k.a. GCL)
(%i1) 0.2+0.2+0.2;
(%o1) 0.6
(%i2) if (0.2+0.2+0.2 = 0.6) then "yes" else "no";
(%o2) no
(%i3) fpprintprec;
(%o3) 0

Same with
Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
on Windows.

Volker van Nek


2012/5/16 Jaime Villate <***@fe.up.pt>

> 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
>
>
> ______________________________**_________________
> Maxima mailing list
> ***@math.utexas.edu
> http://www.math.utexas.edu/**mailman/listinfo/maxima<http://www.math.utexas.edu/mailman/listinfo/maxima>
>
Raymond Toy
2012-05-17 13:53:39 UTC
Permalink
On 5/17/12 3:16 AM, Volker van Nek wrote:
> For the record: With the recent debian version of Maxima on xubuntu12.04:
>
> Maxima 5.27.0 http://maxima.sourceforge.net
> using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (a.k.a. GCL)
> (%i1) 0.2+0.2+0.2;
> (%o1) 0.6
> (%i2) if (0.2+0.2+0.2 = 0.6) then "yes" else "no";
> (%o2) no

That would be an issue with gcl's float printer. Maxima with cmucl
prints out .6000000000000001. Clisp and ecl give this answer too. Ccl
produces 0.6.

Ray
Evgeniy Maevskiy
2012-05-16 16:58:53 UTC
Permalink
I'm sorry, but I have to try again. Let us compare.

Maxima 5.27.0
----------------

numer:true$
for k:1 thru 29 do print("k=",k,": ",1/5^k)$

k= 1 : 0.2
k= 2 : 0.04
k= 3 : 0.008
k= 4 : 0.0016
k= 5 : 3.2000000000000003E-4
k= 6 : 6.3999999999999997E-5
k= 7 : 1.2799999999999999E-5
k= 8 : 2.5600000000000001E-6
k= 9 : 5.1200000000000003E-7
k= 10 : 1.024E-7
k= 11 : 2.0479999999999999E-8
k= 12 : 4.0959999999999999E-9
k= 13 : 8.1920000000000002E-10
k= 14 : 1.6384E-10
k= 15 : 3.2768000000000003E-11
k= 16 : 6.5535999999999999E-12
k= 17 : 1.3107200000000001E-12
k= 18 : 2.6214400000000002E-13
k= 19 : 5.2428799999999999E-14
k= 20 : 1.0485759999999999E-14
k= 21 : 2.0971519999999998E-15
k= 22 : 4.1943040000000002E-16
k= 23 : 8.3886080000000009E-17
k= 24 : 1.6777216000000002E-17
k= 25 : 3.3554432000000005E-18
k= 26 : 6.7108864000000012E-19
k= 27 : 1.3421772800000001E-19
k= 28 : 2.6843545600000002E-20
k= 29 : 5.3687091200000004E-21


Firefox 11.0
------------

<html>
<body>
<p id="p"></p>
<script>
for(k=1;k<30;k++)
document.getElementById("p").innerHTML+="k="+k+": "+1/Math.pow(5,k)+"<br>";
</script>
</body>
</html>

k=1: 0.2
k=2: 0.04
k=3: 0.008
k=4: 0.0016
k=5: 0.00032
k=6: 0.000064
k=7: 0.0000128
k=8: 0.00000256
k=9: 5.12e-7
k=10: 1.024e-7
k=11: 2.048e-8
k=12: 4.096e-9
k=13: 8.192e-10
k=14: 1.6384e-10
k=15: 3.2768e-11
k=16: 6.5536e-12
k=17: 1.31072e-12
k=18: 2.62144e-13
k=19: 5.24288e-14
k=20: 1.048576e-14
k=21: 2.097152e-15
k=22: 4.194304e-16
k=23: 8.388608000000001e-17
k=24: 1.6777216000000002e-17
k=25: 3.3554431999999997e-18
k=26: 6.710886399999999e-19
k=27: 1.34217728e-19
k=28: 2.6843545600000002e-20
k=29: 5.3687091200000004e-21

=================================

I don't see: where is rounding? Firefox up to k=22 returns exact values
Stavros Macrakis
2012-05-17 13:27:39 UTC
Permalink
It is not "returning exact values". It is *printing *a rounded value.

To demonstrate this to yourself, try:

document.getElementById("p").innerHTML+=6.3999999999999997E-5;

-s


2012/5/16 Evgeniy Maevskiy <***@e-math.ru>

> I'm sorry, but I have to try again. Let us compare.
>
> Maxima 5.27.0
> ----------------
>
> numer:true$
> for k:1 thru 29 do print("k=",k,": ",1/5^k)$
>
> k= 1 : 0.2
> k= 2 : 0.04
> k= 3 : 0.008
> k= 4 : 0.0016
> k= 5 : 3.2000000000000003E-4
> k= 6 : 6.3999999999999997E-5
> k= 7 : 1.2799999999999999E-5
> k= 8 : 2.5600000000000001E-6
> k= 9 : 5.1200000000000003E-7
> k= 10 : 1.024E-7
> k= 11 : 2.0479999999999999E-8
> k= 12 : 4.0959999999999999E-9
> k= 13 : 8.1920000000000002E-10
> k= 14 : 1.6384E-10
> k= 15 : 3.2768000000000003E-11
> k= 16 : 6.5535999999999999E-12
> k= 17 : 1.3107200000000001E-12
> k= 18 : 2.6214400000000002E-13
> k= 19 : 5.2428799999999999E-14
> k= 20 : 1.0485759999999999E-14
> k= 21 : 2.0971519999999998E-15
> k= 22 : 4.1943040000000002E-16
> k= 23 : 8.3886080000000009E-17
> k= 24 : 1.6777216000000002E-17
> k= 25 : 3.3554432000000005E-18
> k= 26 : 6.7108864000000012E-19
> k= 27 : 1.3421772800000001E-19
> k= 28 : 2.6843545600000002E-20
> k= 29 : 5.3687091200000004E-21
>
>
> Firefox 11.0
> ------------
>
> <html>
> <body>
> <p id="p"></p>
> <script>
> for(k=1;k<30;k++)
> document.getElementById("p").innerHTML+="k="+k+":
> "+1/Math.pow(5,k)+"<br>";
> </script>
> </body>
> </html>
>
> k=1: 0.2
> k=2: 0.04
> k=3: 0.008
> k=4: 0.0016
> k=5: 0.00032
> k=6: 0.000064
> k=7: 0.0000128
> k=8: 0.00000256
> k=9: 5.12e-7
> k=10: 1.024e-7
> k=11: 2.048e-8
> k=12: 4.096e-9
> k=13: 8.192e-10
> k=14: 1.6384e-10
> k=15: 3.2768e-11
> k=16: 6.5536e-12
> k=17: 1.31072e-12
> k=18: 2.62144e-13
> k=19: 5.24288e-14
> k=20: 1.048576e-14
> k=21: 2.097152e-15
> k=22: 4.194304e-16
> k=23: 8.388608000000001e-17
> k=24: 1.6777216000000002e-17
> k=25: 3.3554431999999997e-18
> k=26: 6.710886399999999e-19
> k=27: 1.34217728e-19
> k=28: 2.6843545600000002e-20
> k=29: 5.3687091200000004e-21
>
> =================================
>
> I don't see: where is rounding? Firefox up to k=22 returns exact values
>
>
> _______________________________________________
> Maxima mailing list
> ***@math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
Steve Haflich
2012-05-17 14:32:00 UTC
Permalink
I don't see: where is rounding? Firefox up to k=22 returns exact values

Stavros already made this point, but perhaps not sufficiently: You must
keep firmly in mind the difference between "return" and "print". They
are not the same.(*)

Volker commented:

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

A more direct illustration would be the following, which you can try in
your favorite Common Lisp:

(= 0.6 3/5) => NIL

I believe this result is _required_ by the ANS, at least if
:ieee-floating-point is present on the *features* list.

(*) See also http://en.wikipedia.org/wiki/The_Treachery_of_Images
Evgeniy Maevskiy
2012-05-17 17:23:05 UTC
Permalink
Thanks to all. Yes, it is the fact:

numer:false$
is(0.2=1/5); /* false - it's clear */

numer:true$
is(0.2=1/5); /* true - and it's clear too */

Now I'd like to compute the error:

numer:false$
0.2-1/5;

returns 0.0

I think that the error must be about 1e-16. Can I compute it explicitly?
Barton Willis
2012-05-17 19:12:24 UTC
Permalink
(%i1) rationalize(0.2)-1/5;
(%o1) 1/90071992547409920

(%i2) float(%);
(%o2) 1.1102230246251566*10^-17

--Barton

________________________________________
From: maxima-***@math.utexas.edu [maxima-***@math.utexas.edu] on behalf of Evgeniy Maevskiy [***@e-math.ru]
Sent: Thursday, May 17, 2012 12:23
To: ***@math.utexas.edu
Subject: Re: [Maxima] Representation of floats

Thanks to all. Yes, it is the fact:

numer:false$
is(0.2=1/5); /* false - it's clear */

numer:true$
is(0.2=1/5); /* true - and it's clear too */

Now I'd like to compute the error:

numer:false$
0.2-1/5;

returns 0.0

I think that the error must be about 1e-16. Can I compute it explicitly?
Stavros Macrakis
2012-05-17 19:16:50 UTC
Permalink
rationalize(0.2) - 1/5 => 1/90071992547409920 -- exact error

factor(rationalize(0.2)-1/5) => 1/(2^54*5) -- exact error,
perhaps more insight

float(rationalize(0.2)-1/5) => 1.11...e-17 -- good
approximation

bfloat(0.2) - 1/5, fpprec=20 => 1.11...b-17 -- good
approximation (bfloat is contagious)


On Thu, May 17, 2012 at 1:23 PM, Evgeniy Maevskiy <***@e-math.ru>wrote:

> Thanks to all. Yes, it is the fact:
>
> numer:false$
> is(0.2=1/5); /* false - it's clear */
>
> numer:true$
> is(0.2=1/5); /* true - and it's clear too */
>
> Now I'd like to compute the error:
>
> numer:false$
> 0.2-1/5;
>
> returns 0.0
>
> I think that the error must be about 1e-16. Can I compute it explicitly?
>
>
>
>
> _______________________________________________
> Maxima mailing list
> ***@math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
Loading...