rounding errors?

T

todd

Hi,
I've just started using Python, and am having an extraordinary experience.
One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers.. Maybe It's superficial, but here's what I'm
getting,using the interpreter..Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?
Thanks
Todd
 
E

Erik Max Francis

todd said:
Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?

It has to do with floating point, and nothing to do with Python. Any
other language using floating point has the same rounding problems.
 
T

Terry Reedy

todd said:
Hi,
I've just started using Python, and am having an extraordinary experience.
One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers..

No floating point numbers are funny ;-). There should be at least one FAQ
entry on the subject and at least 10 past threads available thru Google.
Plus some numerical analysis texts. It is possible that your work should
wait for the new decimal module in 2.4.

Terry J. Reedy
 
J

Jason Mobarak

http://docs.python.org/tut/node15.html
Hi,
I've just started using Python, and am having an extraordinary experience.
One thing worries me, however, I'm planning on doing some mathematical
research with Python, and it appears that it does funny thing with
floating point numbers.. Maybe It's superficial, but here's what I'm
getting,using the interpreter..


0.20999999999999999

Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?
Thanks
Todd
 
G

Grant Edwards

todd said:
I've just started using Python, and am having an extraordinary
experience.

Python's pretty cool, eh?
One thing worries me, however, I'm planning on doing some
mathematical research with Python, and it appears that it does
funny thing with floating point numbers.

No, it isn't. The problem is that you're expecting it to do
something funny with them and it isn't.
Maybe It's superficial,

No, on the contrary, it's quite fundamental to the way floating
point works in computers.
but here's what I'm getting,using the interpreter..

[...]

And you'd prefer that the intrepreter lied to you and printed
"0.32" when you tell it to print the number 0.3200..001 --
that's a common reaction from people who don't understand
floating point numbers. If you want, you can specify a format
when printing floating point numbers so that you only see as
many digits as you want to:
Now, I realize that this is really small errors.. Does anybody
have an explanation why Python picks up or loses these?

Somebody else has already posted a pointer to the Python FAQ
about floating point, but if you're planning on doing numerical
research using floating point on computers, you really ought to
take a class on numerical methods or numerical analysis using
floating point on computers.
 
A

Anthony Baxter

Note that Python 2.4 will include a 'decimal' module that exposes a
numerical type far better suited to this sort of calculation that
binary floating point.
 
H

Harald Massa

Now, I realize that this is really small errors.. Does anybody have an
explanation why Python picks up or loses these?

The Timbot explanation is standard to this.

But the solution is easier: google for decimal pyhton or look at python 2.4

Harald
 
D

Dan Bishop

Anthony Baxter said:
Note that Python 2.4 will include a 'decimal' module that exposes a
numerical type far better suited to this sort of calculation that
binary floating point.

Only if you're calculating inherently decimal things (like money).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top