Python can't divide??!?!

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

    Python can't divide??!?!

    Ummmm...

    This is weird. Sorry if it's known about (how can it NOT be, I wonder?) but
    I haven't seen any reference to it anywhere.

    I'm running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.

    Fire up python or whatever.

    Do this: Result:

    3 / 5 0 Fair enough, int / int = int
    3 / 5.0 0.5999999999999 9998 eh?
    3.0 / 3 0.5999999999999 9998 ummmm...
    3.0 / 5.0 0.5999999999999 9998 how did I guess...

    That's just an example. Python cannot divide. Period. If you try 1.0 / 3 you
    get 0.3333333333333 3331, and from this, the above example, and a few tests,
    I speculate that *something* is subtracting 2 from the last digit *after*
    the division is done.

    Wacky.

    Now, is this just me, or can someone else duplicate it? I've tried it on two
    computers here, but they're both Win XP. I'm about to test it on Linux but I
    figured I'd write this first.

    Dan



  • Stephen Boulet

    #2
    Re: Python can't divide??!?!

    Works fine here:
    [color=blue][color=green][color=darkred]
    >>> a = 3/5
    >>> b = 3/5.0
    >>> c = 3.0/3
    >>> d = 3.0/5.0
    >>>
    >>> print "%f %f %f %f" % (a,b,c,d)[/color][/color][/color]
    0.000000 0.600000 1.000000 0.600000

    but
    [color=blue][color=green][color=darkred]
    >>> print "%.17f %.17f %.17f %.17f" % (a,b,c,d)[/color][/color][/color]
    0.0000000000000 0000 0.5999999999999 9998 1.0000000000000 0000
    0.5999999999999 9998

    ;)

    Stephen


    Dan Williams wrote:
    [color=blue]
    > Ummmm...
    >
    > This is weird. Sorry if it's known about (how can it NOT be, I wonder?) but
    > I haven't seen any reference to it anywhere.
    >
    > I'm running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.
    >
    > Fire up python or whatever.
    >
    > Do this: Result:
    >
    > 3 / 5 0 Fair enough, int / int = int
    > 3 / 5.0 0.5999999999999 9998 eh?
    > 3.0 / 3 0.5999999999999 9998 ummmm...
    > 3.0 / 5.0 0.5999999999999 9998 how did I guess...
    >
    > That's just an example. Python cannot divide. Period. If you try 1.0 / 3 you
    > get 0.3333333333333 3331, and from this, the above example, and a few tests,
    > I speculate that *something* is subtracting 2 from the last digit *after*
    > the division is done.
    >
    > Wacky.
    >
    > Now, is this just me, or can someone else duplicate it? I've tried it on two
    > computers here, but they're both Win XP. I'm about to test it on Linux but I
    > figured I'd write this first.
    >
    > Dan
    >
    >
    >[/color]

    Comment

    • Dan Bishop

      #3
      Re: Python can't divide??!?!

      "Dan Williams" <dan@ithium.net > wrote in message news:<mailman.1 252.1076003166. 12720.python-list@python.org >...[color=blue]
      > Ummmm...
      >
      > This is weird. Sorry if it's known about (how can it NOT be, I wonder?) but
      > I haven't seen any reference to it anywhere.[/color]

      Then you haven't been looking hard enough.
      [color=blue]
      > I'm running the latest Python (2.3.3) on Windows XP Pro, i386 architecture.
      >
      > Fire up python or whatever.
      >
      > Do this: Result:
      >
      > 3 / 5 0 Fair enough, int / int = int[/color]

      This is for bug-compatibility with old versions of Python. It is
      strongly recommended that all new Python scripts use "from __future__
      import division".
      [color=blue]
      > 3 / 5.0 0.5999999999999 9998 eh?
      > 3.0 / 3 0.5999999999999 9998 ummmm...
      > 3.0 / 5.0 0.5999999999999 9998 how did I guess...[/color]

      It makes perfect sense when you remember that computers do math in
      binary. In binary, 3/5 is equal to 0.1 0011 0011 0011 0011 0011
      0011..., which is rounded to (C99 notation) 0x1.33333333333 333333p-1.
      The exact decimal equivalent of this is
      0.5999999999999 999777955395074 968691915273666 3818359375. In the
      default 17 significant digits format, this rounds to
      0.5999999999999 9998.
      [color=blue]
      > That's just an example. Python cannot divide. Period.[/color]

      It's off by only 37 parts per quintillion.
      [color=blue]
      > If you try 1.0 / 3 you get 0.3333333333333 3331,[/color]

      More precisely, you get 0x1.55555555555 55p-2, or
      0.3333333333333 333148296162562 473909929394721 98486328125.
      [color=blue]
      > and from this, the above example, and a few tests,
      > I speculate that *something* is subtracting 2 from the last digit *after*
      > the division is done.[/color]

      *Nothing* is subtracting from the last digit because your floating
      part hardware has no concept of decimal digits, only bits.
      [color=blue]
      > Now, is this just me, or can someone else duplicate it?[/color]

      Everyone in the world who has binary floating part hardware can
      duplicate it, whether they're using Python or not.

      Comment

      • Erik Max Francis

        #4
        Re: Python can't divide??!?!

        Dan Williams wrote:
        [color=blue]
        > Fire up python or whatever.
        >
        > Do this: Result:
        >
        > 3 / 5 0 Fair enough, int / int = int
        > 3 / 5.0 0.5999999999999 9998 eh?
        > 3.0 / 3 0.5999999999999 9998 ummmm...
        > 3.0 / 5.0 0.5999999999999 9998 how did I guess...
        >
        > That's just an example. Python cannot divide. Period.[/color]

        Neither can C, C++, Java, Perl, or any of the other countless
        programming languages which use floating point.

        --
        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
        \__/ I thought I might never see another Saturday night.
        -- Robert S. MacNamara

        Comment

        • Kurt B. Kaiser

          #5
          Re: Python can't divide??!?!

          "Dan Williams" <dan@ithium.net > writes:
          [color=blue]
          > Fire up python or whatever.
          >
          > Do this: Result:
          >
          > 3 / 5 0 Fair enough, int / int = int
          > 3 / 5.0 0.5999999999999 9998 eh?
          > 3.0 / 3 0.5999999999999 9998 ummmm...
          > 3.0 / 5.0 0.5999999999999 9998 how did I guess...
          >[/color]
          [color=blue][color=green][color=darkred]
          >>> print 3.0 / 5[/color][/color][/color]
          0.6[color=blue][color=green][color=darkred]
          >>> print (3.0 / 5)[/color][/color][/color]
          0.6[color=blue][color=green][color=darkred]
          >>> print (3.0 / 5, 3.0 / 5)[/color][/color][/color]
          (0.599999999999 99998, 0.5999999999999 9998)[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          [...]
          [color=blue]
          > Wacky.[/color]

          :-)

          --
          KBK

          Comment

          • Josiah Carlson

            #6
            Re: Python can't divide??!?!

            >>Wacky.

            Not wacky, there is a difference between str() and repr():
            [color=blue][color=green][color=darkred]
            >>> str(3.0/5)[/color][/color][/color]
            '0.6'[color=blue][color=green][color=darkred]
            >>> repr(3.0/5)[/color][/color][/color]
            '0.599999999999 99998'


            - Josiah

            Comment

            • Ben Finney

              #7
              Re: Python can't divide??!?!

              On Thu, 5 Feb 2004 17:44:55 -0000, Dan Williams wrote:[color=blue]
              > Python cannot divide. Period.[/color]

              Your binary computer can't accurately represent decimal fractions.
              Python doesn't try to hide this from you.

              <http://www.python.org/doc/tut/node15.html>

              --
              \ "A celibate clergy is an especially good idea, because it tends |
              `\ to suppress any hereditary propensity toward fanaticism." -- |
              _o__) Carl Sagan |
              Ben Finney <http://bignose.squidly .org/>

              Comment

              • John Hunter

                #8
                Re: Python can't divide??!?!

                >>>>> "Kurt" == Kurt B Kaiser <kbk@shore.ne t> writes:


                This prints a float[color=blue][color=green][color=darkred]
                >>> print (3.0 / 5)[/color][/color][/color]
                0.6

                This prints a tuple of floats[color=blue][color=green][color=darkred]
                >>> print (3.0 / 5, 3.0 / 5)[/color][/color][/color]
                (0.599999999999 99998, 0.5999999999999 9998)

                This prints a (length 1) tuple of floats[color=blue][color=green][color=darkred]
                >>> print (3.0 / 5, )[/color][/color][/color]
                (0.599999999999 99998,)

                The thing that may be tripping you up is that (val) is simply the
                value, and (val,) is a tuple containing the value as the first
                element. As for printing a value versus a tuple of values, that's the
                __repr__ versus __str__ distinction previous posters have referred to.

                JDH



                Comment

                • Kurt B. Kaiser

                  #9
                  Re: Python can't divide??!?!

                  John Hunter <jdhunter@ace.b sd.uchicago.edu > writes:
                  [color=blue]
                  > The thing that may be tripping you up is that (val) is simply the
                  > value, and (val,) is a tuple containing the value as the first
                  > element. As for printing a value versus a tuple of values, that's the
                  > __repr__ versus __str__ distinction previous posters have referred to.[/color]

                  :-)

                  Tim enlightened me on this issue some years ago, when I suggested a
                  "solution" using sys.displayhook ;



                  Just try explaining this stuff to a 9 year old learning Python! It
                  tends to break the expository flow....

                  --
                  KBK

                  "Don't sit there with your face all screwed up. Swallow!
                  Cod liver oil is good for you"

                  Comment

                  • Axle

                    #10
                    Re: Python can't divide??!?!

                    Yes, python can divide... use the / operator

                    Kurt B. Kaiser wrote:
                    [color=blue]
                    > John Hunter <jdhunter@ace.b sd.uchicago.edu > writes:
                    >
                    >[color=green]
                    >>The thing that may be tripping you up is that (val) is simply the
                    >>value, and (val,) is a tuple containing the value as the first
                    >>element. As for printing a value versus a tuple of values, that's the
                    >>__repr__ versus __str__ distinction previous posters have referred to.[/color]
                    >
                    >
                    > :-)
                    >
                    > Tim enlightened me on this issue some years ago, when I suggested a
                    > "solution" using sys.displayhook ;
                    >
                    > http://www.google.com/groups?q=g:thl...t%40python.org
                    >
                    > Just try explaining this stuff to a 9 year old learning Python! It
                    > tends to break the expository flow....
                    >[/color]

                    Comment

                    • Josiah Carlson

                      #11
                      Re: Python can't divide??!?!

                      Axle wrote:[color=blue]
                      > Yes, python can divide... use the / operator[/color]

                      He was referring to the inaccuracy of binary vs. decimal
                      representations . Most Computer Science students don't learn about it
                      until sophomore year of college in their computer architectures class.


                      3.0 / 5.0 -> .6 in decimal.

                      When we're dealing with floating point arithmetic in Python (or any
                      other language that uses IEEE 754 double-precision floating point
                      representations ), 3.0 / 5.0 is internally represented as:
                      001111111110001 100110011001100 110011001100110 011001100110011 0011

                      If we then convert that back into decimal, we get .59999999999999 998.

                      Binary representation thanks to:


                      - Josiah

                      Comment

                      Working...