473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jul 18 '05 #1
10 2862
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

Jul 18 '05 #2
"Dan Williams" <da*@ithium.net> wrote in message news:<ma***************************************@py thon.org>...
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.599999999999999977795539507496869191527366638183 59375. 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.333333333333333314829616256247390992939472198486 328125.
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.
Jul 18 '05 #3
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 && ma*@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
Jul 18 '05 #4
"Dan Williams" <da*@ithium.net> 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
Jul 18 '05 #5
>>Wacky.

Not wacky, there is a difference between str() and repr():
str(3.0/5) '0.6' repr(3.0/5)

'0.59999999999999998'
- Josiah
Jul 18 '05 #6
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/>
Jul 18 '05 #7
>>>>> "Kurt" == Kurt B Kaiser <kb*@shore.net> 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

Jul 18 '05 #8
John Hunter <jd******@ace.bsd.uchicago.edu> 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;

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

--
KBK

"Don't sit there with your face all screwed up. Swallow!
Cod liver oil is good for you"
Jul 18 '05 #9
Yes, python can divide... use the / operator

Kurt B. Kaiser wrote:
John Hunter <jd******@ace.bsd.uchicago.edu> 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;

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


Jul 18 '05 #10
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:
00111111111000110011001100110011001100110011001100 11001100110011

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
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
11
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
17
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
7
by: Anton Mellit | last post by:
Hi, I am working on a Pari-Python module (see about GP/PARI at http://pari.math.u-bordeaux.fr/). Similar project was started by Stefane Fermigier 12 years ago (you can find a post about it on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.