FAQ
Hi,
I am new to python. I was messing around in the interperator checking
out the floating point handling and I believe I may have found a
rounding bug:
234 - 23234.2345
-23000.234499999999


This is not correct by my calculations.

I am using python 2.5.2 in ubuntu 8.04. I am wondering if this is a
known bug, or if I am just not understanding some feature of python.
Thanks for the help!

-Cooper Quintin
http://www.bitsamurai.net

Search Discussions

  • Casevh at Jun 18, 2008 at 5:54 am

    On Jun 17, 10:47?pm, coop... at gmail.com wrote:
    Hi,
    I am new to python. ?I was messing around in the interperator checking
    out the floating point handling and I believe I may have found a
    rounding bug:
    234 - 23234.2345
    -23000.234499999999

    This is not correct by my calculations.

    I am using python 2.5.2 in ubuntu 8.04. ?I am wondering if this is a
    known bug, or if I am just not understanding some feature of python.
    Thanks for the help!

    -Cooper Quintinhttp://www.bitsamurai.net
    It is a side-effect of binary floating-point.

    http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate

    casevh
  • Aidan at Jun 18, 2008 at 6:05 am

    cooperq at gmail.com wrote:
    Hi,
    I am new to python. I was messing around in the interperator checking
    out the floating point handling and I believe I may have found a
    rounding bug:
    234 - 23234.2345
    -23000.234499999999


    This is not correct by my calculations.

    I am using python 2.5.2 in ubuntu 8.04. I am wondering if this is a
    known bug, or if I am just not understanding some feature of python.
    Thanks for the help!

    -Cooper Quintin
    http://www.bitsamurai.net

    If you need to round before you display the result, I find the following
    works:
    '%.4f' % (234 - 23234.2345)
    '-23000.2345'

    The %.4f means that the interpolated value will be treated as a float,
    rounded to 4 digits after the decimal point.
  • Jerry Hill at Jun 18, 2008 at 3:02 pm

    On Wed, Jun 18, 2008 at 1:47 AM, wrote:
    234 - 23234.2345
    -23000.234499999999

    This is not correct by my calculations.
    Python floating point operations use the underlying C floating point
    libraries which, in turn, usually rely on the hardware's floating
    point implementations. This Wikipedia article talks about how those
    values are usually stored and manipulated:
    http://en.wikipedia.org/wiki/IEEE_floating-point_standard

    So, which IEEE double precision floating point value would you like
    instead? As far as I understand it, these are your two choices:

    'ba490c020f76d6c0' = -23000.234499999999
    'bb490c020f76d6c0' = -23000.234500000002

    Alternatively, investigate the Decimal module, but keep in mind that
    it can have the same sorts of issues, just on different numbers.
    Compare, for instance:
    (1.0/3.0) * 3.0
    1.0
    from decimal import Decimal
    ( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')
    Decimal("0.9999999999999999999999999999")
    >>>

    --
    Jerry
  • Cooperq at Jun 18, 2008 at 4:26 pm

    On Jun 18, 8:02?am, "Jerry Hill" wrote:
    On Wed, Jun 18, 2008 at 1:47 AM, ?wrote:
    234 - 23234.2345
    -23000.234499999999
    This is not correct by my calculations.
    Python floating point operations use the underlying C floating point
    libraries which, in turn, usually rely on the hardware's floating
    point implementations. ?This Wikipedia article talks about how those
    values are usually stored and manipulated:http://en.wikipedia.org/wiki/IEEE_floating-point_standard

    So, which IEEE double precision floating point value would you like
    instead? ?As far as I understand it, these are your two choices:

    'ba490c020f76d6c0' = -23000.234499999999
    'bb490c020f76d6c0' = -23000.234500000002

    Alternatively, investigate the Decimal module, but keep in mind that
    it can have the same sorts of issues, just on different numbers.
    Compare, for instance:
    (1.0/3.0) * 3.0
    1.0
    from decimal import Decimal
    ( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')
    Decimal("0.9999999999999999999999999999")



    --
    Jerry
    So it seems then that python might not be very good for doing
    precision floating point work, because there is a good chance its
    floating points will be off by a (very small) amount? Or is there a
    way to get around this and be guaranteed an accurate answer?
  • Casevh at Jun 18, 2008 at 4:57 pm

    So it seems then that python might not be very good for doing
    precision floating point work, because there is a good chance its
    floating points will be off by a (very small) amount? ?Or is there a
    way to get around this and be guaranteed an accurate answer?- Hide quoted text -
    It's not a Python problem. That is just the behavior for floating-
    point arithmetic.

    casevh

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 18, '08 at 5:47a
activeJun 18, '08 at 4:57p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase