rounding errors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • todd

    rounding errors?

    Hi,
    I've just started using Python, and am having an extraordinary experience.
    One thing worries me, however, I'm planning on doing some mathematical
    research with Python, and it appears that it does funny thing with
    floating point numbers.. Maybe It's superficial, but here's what I'm
    getting,using the interpreter..[color=blue][color=green][color=darkred]
    >>> .31[/color][/color][/color]
    0.31[color=blue][color=green][color=darkred]
    >>> .32[/color][/color][/color]
    0.3200000000000 0001[color=blue][color=green][color=darkred]
    >>> .33[/color][/color][/color]
    0.3300000000000 0002[color=blue][color=green][color=darkred]
    >>> .34[/color][/color][/color]
    0.3400000000000 0002[color=blue][color=green][color=darkred]
    >>> .35[/color][/color][/color]
    0.3499999999999 9998[color=blue][color=green][color=darkred]
    >>> .21[/color][/color][/color]
    0.2099999999999 9999[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    Now, I realize that this is really small errors.. Does anybody have an
    explanation why Python picks up or loses these?
    Thanks
    Todd
  • Erik Max Francis

    #2
    Re: rounding errors?

    todd wrote:
    [color=blue]
    > Now, I realize that this is really small errors.. Does anybody have an
    > explanation why Python picks up or loses these?[/color]

    It has to do with floating point, and nothing to do with Python. Any
    other language using floating point has the same rounding problems.

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ There's really nothing like it.
    -- Sade Adu

    Comment

    • Terry Reedy

      #3
      Re: rounding errors?


      "todd" <toddkeeler@yah oo.ca> wrote in message
      news:pan.2004.0 7.01.16.18.54.8 64375@yahoo.ca. ..[color=blue]
      > Hi,
      > I've just started using Python, and am having an extraordinary[/color]
      experience.[color=blue]
      > One thing worries me, however, I'm planning on doing some mathematical
      > research with Python, and it appears that it does funny thing with
      > floating point numbers..[/color]

      No floating point numbers are funny ;-). There should be at least one FAQ
      entry on the subject and at least 10 past threads available thru Google.
      Plus some numerical analysis texts. It is possible that your work should
      wait for the new decimal module in 2.4.

      Terry J. Reedy





      Comment

      • Jason Mobarak

        #4
        Re: rounding errors?



        todd wrote:[color=blue]
        > Hi,
        > I've just started using Python, and am having an extraordinary experience.
        > One thing worries me, however, I'm planning on doing some mathematical
        > research with Python, and it appears that it does funny thing with
        > floating point numbers.. Maybe It's superficial, but here's what I'm
        > getting,using the interpreter..
        >[color=green][color=darkred]
        >>>>.31[/color][/color]
        >
        > 0.31
        >[color=green][color=darkred]
        >>>>.32[/color][/color]
        >
        > 0.3200000000000 0001
        >[color=green][color=darkred]
        >>>>.33[/color][/color]
        >
        > 0.3300000000000 0002
        >[color=green][color=darkred]
        >>>>.34[/color][/color]
        >
        > 0.3400000000000 0002
        >[color=green][color=darkred]
        >>>>.35[/color][/color]
        >
        > 0.3499999999999 9998
        >[color=green][color=darkred]
        >>>>.21[/color][/color]
        >
        > 0.2099999999999 9999
        >
        > Now, I realize that this is really small errors.. Does anybody have an
        > explanation why Python picks up or loses these?
        > Thanks
        > Todd[/color]

        Comment

        • Tim Peters

          #5
          Re: rounding errors?

          [todd]
          ....[color=blue][color=green][color=darkred]
          > >>> .21[/color][/color]
          > 0.2099999999999 9999[color=green][color=darkred]
          > >>>[/color][/color]
          >
          > Now, I realize that this is really small errors.. Does anybody have an
          > explanation why Python picks up or loses these?[/color]

          Many people do, and a few of them are even correct <wink>. If you
          start here, you can stop here too:



          Comment

          • Grant Edwards

            #6
            Re: rounding errors?

            todd wrote:
            [color=blue]
            > I've just started using Python, and am having an extraordinary
            > experience.[/color]

            Python's pretty cool, eh?
            [color=blue]
            > One thing worries me, however, I'm planning on doing some
            > mathematical research with Python, and it appears that it does
            > funny thing with floating point numbers.[/color]

            No, it isn't. The problem is that you're expecting it to do
            something funny with them and it isn't.
            [color=blue]
            > Maybe It's superficial,[/color]

            No, on the contrary, it's quite fundamental to the way floating
            point works in computers.
            [color=blue]
            > but here's what I'm getting,using the interpreter..
            >[color=green][color=darkred]
            >>>>>>.31
            >>>
            >>> 0.31
            >>>
            >>>>>>.32
            >>>
            >>> 0.3200000000000 0001[/color][/color][/color]

            [...]

            And you'd prefer that the intrepreter lied to you and printed
            "0.32" when you tell it to print the number 0.3200..001 --
            that's a common reaction from people who don't understand
            floating point numbers. If you want, you can specify a format
            when printing floating point numbers so that you only see as
            many digits as you want to:
            [color=blue][color=green][color=darkred]
            >>> "%0.2f" % 0.32[/color][/color][/color]
            '0.32'[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]
            [color=blue]
            > Now, I realize that this is really small errors.. Does anybody
            > have an explanation why Python picks up or loses these?[/color]

            Somebody else has already posted a pointer to the Python FAQ
            about floating point, but if you're planning on doing numerical
            research using floating point on computers, you really ought to
            take a class on numerical methods or numerical analysis using
            floating point on computers.

            --
            Grant Edwards grante Yow! PIZZA!!
            at
            visi.com

            Comment

            • Anthony Baxter

              #7
              Re: rounding errors?

              Note that Python 2.4 will include a 'decimal' module that exposes a
              numerical type far better suited to this sort of calculation that
              binary floating point.

              Comment

              • Harald Massa

                #8
                Re: rounding errors?

                > Now, I realize that this is really small errors.. Does anybody have an[color=blue]
                > explanation why Python picks up or loses these?[/color]

                The Timbot explanation is standard to this.

                But the solution is easier: google for decimal pyhton or look at python 2.4

                Harald

                Comment

                • Dan Bishop

                  #9
                  Re: rounding errors?

                  Anthony Baxter <anthonybaxter@ gmail.com> wrote in message news:<mailman.3 77.1088840914.2 7577.python-list@python.org >...[color=blue]
                  > Note that Python 2.4 will include a 'decimal' module that exposes a
                  > numerical type far better suited to this sort of calculation that
                  > binary floating point.[/color]

                  Only if you're calculating inherently decimal things (like money).

                  Comment

                  • Anthony Baxter

                    #10
                    Re: rounding errors?

                    On 3 Jul 2004 15:17:35 -0700, Dan Bishop <danb_83@yahoo. com> wrote:[color=blue][color=green]
                    > > Note that Python 2.4 will include a 'decimal' module that exposes a
                    > > numerical type far better suited to this sort of calculation that
                    > > binary floating point.[/color]
                    >
                    > Only if you're calculating inherently decimal things (like money).
                    >[/color]

                    Or anything where you really care about having controllable
                    precision.

                    Comment

                    Working...