Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   subtraction of floating point numbers (http://www.velocityreviews.com/forums/t868688-subtraction-of-floating-point-numbers.html)

 Jaroslav Dobrek 02-24-2012 08:41 AM

subtraction of floating point numbers

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

 Alain Ketterlin 02-24-2012 08:49 AM

Re: subtraction of floating point numbers

Jaroslav Dobrek <jaroslav.dobrek@gmail.com> writes:

> 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...

Know About Floating Point Arithmetic", at:

http://citeseerx.ist.psu.edu/viewdoc...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.

 Chris Rebert 02-24-2012 10:16 AM

Re: subtraction of floating point numbers

On Fri, Feb 24, 2012 at 12:41 AM, Jaroslav Dobrek
<jaroslav.dobrek@gmail.com> wrote:
> 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?

Refer to this thread from 2 days ago:
http://mail.python.org/pipermail/pyt...y/1288344.html

Regards,
Chris

 All times are GMT. The time now is 05:29 PM.