Hrounding error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cooperq@gmail.com

    Hrounding error

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


    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

  • casevh

    #2
    Re: Hrounding error

    On Jun 17, 10:47 pm, coop...@gmail.c om 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.234499999 999
    >
    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.

    Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...


    casevh

    Comment

    • Aidan

      #3
      Re: Hrounding error

      cooperq@gmail.c om 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.234499999 999
      >
      >
      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.

      Comment

      • Jerry Hill

        #4
        Re: Hrounding error

        On Wed, Jun 18, 2008 at 1:47 AM, <cooperq@gmail. comwrote:
        >>>234 - 23234.2345
        -23000.234499999 999
        >
        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:


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

        'ba490c020f76d6 c0' = -23000.234499999 999
        'bb490c020f76d6 c0' = -23000.234500000 002

        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.9999 999999999999999 999999999")
        >>>
        --
        Jerry

        Comment

        • cooperq@gmail.com

          #5
          Re: Hrounding error

          On Jun 18, 8:02 am, "Jerry Hill" <malaclyp...@gm ail.comwrote:
          On Wed, Jun 18, 2008 at 1:47 AM,  <coop...@gmail. comwrote:
          >>234 - 23234.2345
          -23000.234499999 999
          >
          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_fl...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:
          >
          'ba490c020f76d6 c0' = -23000.234499999 999
          'bb490c020f76d6 c0' = -23000.234500000 002
          >
          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.9999 999999999999999 999999999")
          >
          >
          >
          --
          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?

          Comment

          • casevh

            #6
            Re: Hrounding error

            >
            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 quotedtext -
            >
            It's not a Python problem. That is just the behavior for floating-
            point arithmetic.

            casevh

            Comment

            Working...