subtraction of floating point numbers

Discussion in 'Python' started by Jaroslav Dobrek, Feb 24, 2012.

  1. 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
     
    Jaroslav Dobrek, Feb 24, 2012
    #1
    1. Advertising

  2. Jaroslav Dobrek <> 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...

    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.
     
    Alain Ketterlin, Feb 24, 2012
    #2
    1. Advertising

  3. Jaroslav Dobrek

    Chris Rebert Guest

    On Fri, Feb 24, 2012 at 12:41 AM, Jaroslav Dobrek
    <> 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/python-list/2012-February/1288344.html

    Regards,
    Chris
     
    Chris Rebert, Feb 24, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Motaz Saad
    Replies:
    7
    Views:
    6,558
  2. Replies:
    17
    Views:
    873
    CBFalconer
    Oct 27, 2007
  3. Keflavich
    Replies:
    13
    Views:
    733
    J. Robertson
    Dec 14, 2007
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,423
    Saraswati lakki
    Jan 6, 2012
  5. Nicholas Rahn

    floating point subtraction question

    Nicholas Rahn, Nov 27, 2005, in forum: Ruby
    Replies:
    4
    Views:
    193
    William James
    Nov 27, 2005
Loading...

Share This Page