subtraction of floating point numbers

J

Jaroslav Dobrek

Hello,

when I have Python subtract floating point numbers it yields weird
results. Example:

4822.40 - 4785.52 = 36.8799999999992

Why doesn't Python simply yield the correct result? It doesn't have a
problem with this:

482240 - 478552 = 3688

Can I tell Python in some way to do this differently?

Jaroslav
 
A

Alain Ketterlin

Jaroslav Dobrek said:
when I have Python subtract floating point numbers it yields weird
results. Example:

4822.40 - 4785.52 = 36.8799999999992

We've had this discussion here one or two days ago...

The usual answer is: please read "What Every Computer Scientist Should
Know About Floating Point Arithmetic", at:

http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768

and check the answers posted these last days. In brief: you're working
with floating point numbers, not reals (i.e., real "reals"). That's
life. Deal with it, or move to specialized packages, like decimal.

-- Alain.
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top