# Python can't divide??!?!

Discussion in 'Python' started by Dan Williams, Feb 5, 2004.

1. ### Dan WilliamsGuest

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.59999999999999998 eh?
3.0 / 3 0.59999999999999998 ummmm...
3.0 / 5.0 0.59999999999999998 how did I guess...

That's just an example. Python cannot divide. Period. If you try 1.0 / 3 you
get 0.33333333333333331, 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

Dan Williams, Feb 5, 2004

2. ### Stephen BouletGuest

Works fine here:

>>> 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)

0.000000 0.600000 1.000000 0.600000

but

>>> print "%.17f %.17f %.17f %.17f" % (a,b,c,d)

0.00000000000000000 0.59999999999999998 1.00000000000000000
0.59999999999999998

Stephen

Dan Williams wrote:

> 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.59999999999999998 eh?
> 3.0 / 3 0.59999999999999998 ummmm...
> 3.0 / 5.0 0.59999999999999998 how did I guess...
>
> That's just an example. Python cannot divide. Period. If you try 1.0 / 3 you
> get 0.33333333333333331, 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, Feb 5, 2004

3. ### Dan BishopGuest

"Dan Williams" <> wrote in message news:<>...
> 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.

Then you haven't been looking hard enough.

> 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

This is for bug-compatibility with old versions of Python. It is
strongly recommended that all new Python scripts use "from __future__
import division".

> 3 / 5.0 0.59999999999999998 eh?
> 3.0 / 3 0.59999999999999998 ummmm...
> 3.0 / 5.0 0.59999999999999998 how did I guess...

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.33333333333333333p-1.
The exact decimal equivalent of this is
0.59999999999999997779553950749686919152736663818359375. In the
default 17 significant digits format, this rounds to
0.59999999999999998.

> That's just an example. Python cannot divide. Period.

It's off by only 37 parts per quintillion.

> If you try 1.0 / 3 you get 0.33333333333333331,

More precisely, you get 0x1.5555555555555p-2, or
0.333333333333333314829616256247390992939472198486328125.

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

*Nothing* is subtracting from the last digit because your floating
part hardware has no concept of decimal digits, only bits.

> Now, is this just me, or can someone else duplicate it?

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

Dan Bishop, Feb 6, 2004
4. ### Erik Max FrancisGuest

Dan Williams wrote:

> Fire up python or whatever.
>
> Do this: Result:
>
> 3 / 5 0 Fair enough, int / int = int
> 3 / 5.0 0.59999999999999998 eh?
> 3.0 / 3 0.59999999999999998 ummmm...
> 3.0 / 5.0 0.59999999999999998 how did I guess...
>
> That's just an example. Python cannot divide. Period.

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

--
__ Erik Max Francis && && 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

Erik Max Francis, Feb 6, 2004
5. ### Kurt B. KaiserGuest

"Dan Williams" <> writes:

> Fire up python or whatever.
>
> Do this: Result:
>
> 3 / 5 0 Fair enough, int / int = int
> 3 / 5.0 0.59999999999999998 eh?
> 3.0 / 3 0.59999999999999998 ummmm...
> 3.0 / 5.0 0.59999999999999998 how did I guess...
>

>>> print 3.0 / 5

0.6
>>> print (3.0 / 5)

0.6
>>> print (3.0 / 5, 3.0 / 5)

(0.59999999999999998, 0.59999999999999998)
>>>

[...]

> Wacky.

--
KBK

Kurt B. Kaiser, Feb 6, 2004
6. ### Josiah CarlsonGuest

>>Wacky.

Not wacky, there is a difference between str() and repr():

>>> str(3.0/5)

'0.6'
>>> repr(3.0/5)

'0.59999999999999998'

- Josiah

Josiah Carlson, Feb 6, 2004
7. ### Ben FinneyGuest

On Thu, 5 Feb 2004 17:44:55 -0000, Dan Williams wrote:
> Python cannot divide. Period.

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/>

Ben Finney, Feb 6, 2004
8. ### John HunterGuest

>>>>> "Kurt" == Kurt B Kaiser <> writes:

This prints a float
>>> print (3.0 / 5)

0.6

This prints a tuple of floats
>>> print (3.0 / 5, 3.0 / 5)

(0.59999999999999998, 0.59999999999999998)

This prints a (length 1) tuple of floats
>>> print (3.0 / 5, )

(0.59999999999999998,)

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

John Hunter, Feb 7, 2004
9. ### Kurt B. KaiserGuest

John Hunter <> writes:

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

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"

Kurt B. Kaiser, Feb 7, 2004
10. ### AxleGuest

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

Kurt B. Kaiser wrote:

> John Hunter <> writes:
>
>
>>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.

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

Axle, Feb 8, 2004
11. ### Josiah CarlsonGuest

Axle wrote:
> Yes, python can divide... use the / operator

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:
0011111111100011001100110011001100110011001100110011001100110011

If we then convert that back into decimal, we get .59999999999999998.

Binary representation thanks to:
http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

- Josiah

Josiah Carlson, Feb 8, 2004