Problem with floating point precision

Discussion in 'Python' started by j_mckitrick, Aug 17, 2004.

  1. j_mckitrick

    j_mckitrick Guest

    I checked the docs, but found nothing about floating point issues.

    I am working on an app that deals with currency values, and only does
    addition and subtraction (no division). But I am starting to see
    values that should be the same failing a compare! What is the
    workaround or correct way to compare 2 floats? It worked fine for
    months, then suddenly starting acting odd. I might have upgraded
    Python somewhere in between, I can't recall.

    jonathon
    j_mckitrick, Aug 17, 2004
    #1
    1. Advertising

  2. j_mckitrick

    John Roth Guest

    "j_mckitrick" <> wrote in message
    news:...
    > I checked the docs, but found nothing about floating point issues.
    >
    > I am working on an app that deals with currency values, and only does
    > addition and subtraction (no division). But I am starting to see
    > values that should be the same failing a compare! What is the
    > workaround or correct way to compare 2 floats? It worked fine for
    > months, then suddenly starting acting odd. I might have upgraded
    > Python somewhere in between, I can't recall.


    The basic issue here is that you shouldn't be using floats
    for currency. Granted, lots of people do, but it will eventually
    run into exactly the problem you're describing.

    The reason is that currency is best handled by some form of
    arithmetic that works with discrete quantities, while floats are
    intended to be used with continuous quantities (like scientific
    and engineering measurements).

    There's a floating decimal package in 2.4 that is intended
    to eventually (in 2.5 maybe?) be the base of a currency
    package. However, that doesn't help today.

    What I'd do is create a currency class that uses plain
    old, boring ints as the base, with the necessary decimal
    point handling present behind the scene.

    John Roth


    >
    > jonathon
    John Roth, Aug 17, 2004
    #2
    1. Advertising

  3. j_mckitrick

    Roy Smith Guest

    (j_mckitrick) wrote:

    > I checked the docs, but found nothing about floating point issues.
    >
    > I am working on an app that deals with currency values, and only does
    > addition and subtraction (no division). But I am starting to see
    > values that should be the same failing a compare! What is the
    > workaround or correct way to compare 2 floats?


    A full analysis of floating point math would fill a book. The details
    depend greatly on the underlying FP hardware in your system, although
    these days it's pretty hard to find a machine which doesn't use IEEE 754
    standard math, so you might start by googling for "ieee 754".

    Python 2.4 will have a new Decimal numeric type, which was invented to
    resolve many of the problems that happen when trying to use floats for
    currency calculations. Take a look at
    http://www.python.org/peps/pep-0327.html for more information.

    There is an alpha version of 2.4 that was released recently; you might
    want to download that and start playing around with the Decimal type to
    see if that solves your problem. I'm sure other people here could give
    a better estimate of when there will be a production release of 2.4, but
    my guess is that it's a few months away.
    Roy Smith, Aug 17, 2004
    #3
  4. j_mckitrick

    Brian Gough Guest

    (j_mckitrick) writes:

    > I checked the docs, but found nothing about floating point issues.


    See the appendix "Floating Point Arithmetic: Issues and Limitations"
    in the Python Tutorial for a discussion of this topic.

    --
    Brian Gough

    Network Theory Ltd,
    Publishing the Python Manuals --- http://www.network-theory.co.uk/
    Brian Gough, Aug 17, 2004
    #4
  5. j_mckitrick

    Tim Peters Guest

    [John Roth]
    > ...
    > There's a floating decimal package in 2.4 that is intended
    > to eventually (in 2.5 maybe?) be the base of a currency
    > package. However, that doesn't help today.


    It will help in 2.4, though. Fixed-point decimal is a subset of what
    2.4's decimal arithmetic can do, and it's dead easy to do exact
    "dollars and cents" calculations using it. I'm not sure what a
    currency package would add on top of it -- maybe add currency markers
    to string output, perhaps offer a simplified rounding discipline for
    multiplication, not really sure. The "hard parts" will already be in
    2.4.
    Tim Peters, Aug 18, 2004
    #5
  6. j_mckitrick

    Arthur Guest

    On Tue, 17 Aug 2004 12:19:50 -0400, "John Roth"
    <> wrote:

    >"j_mckitrick" <> wrote in message
    >news:...
    >> I checked the docs, but found nothing about floating point issues.
    >>
    >> I am working on an app that deals with currency values, and only does
    >> addition and subtraction (no division). But I am starting to see
    >> values that should be the same failing a compare! What is the
    >> workaround or correct way to compare 2 floats? It worked fine for
    >> months, then suddenly starting acting odd. I might have upgraded
    >> Python somewhere in between, I can't recall.

    >
    >The basic issue here is that you shouldn't be using floats
    >for currency. Granted, lots of people do, but it will eventually
    >run into exactly the problem you're describing.
    >
    >The reason is that currency is best handled by some form of
    >arithmetic that works with discrete quantities, while floats are
    >intended to be used with continuous quantities (like scientific
    >and engineering measurements).
    >
    >There's a floating decimal package in 2.4 that is intended
    >to eventually (in 2.5 maybe?) be the base of a currency
    >package. However, that doesn't help today.


    @$#,###.##
    import decimal

    I believe wil be the syntax for getting at it ;)

    Art
    Arthur, Aug 18, 2004
    #6
    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. Roger Leigh
    Replies:
    4
    Views:
    454
    Roger Leigh
    Nov 12, 2003
  2. BigMan
    Replies:
    16
    Views:
    589
    E. Robert Tisdale
    Apr 13, 2005
  3. syntax

    floating point precision problem

    syntax, Jan 30, 2004, in forum: C Programming
    Replies:
    21
    Views:
    877
    Dik T. Winter
    Feb 1, 2004
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,277
    Saraswati lakki
    Jan 6, 2012
  5. Todd S.
    Replies:
    10
    Views:
    183
    M. Edward (Ed) Borasky
    Feb 3, 2006
Loading...

Share This Page