Python can't divide??!?!

Josiah Carlson jcarlson at nospam.uci.edu
Sun Feb 8 02:00:10 EST 2004


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



More information about the Python-list mailing list