2.6.1 - simple division

Lie Ryan lie.1296 at gmail.com
Sun Mar 8 10:36:11 EDT 2009


farsight at gmail.com wrote:
> On Mar 8, 2:16 pm, farsi... at gmail.com wrote:
>>>>> 4 / 5.0
>> 0.800000000000000004

This one is a common FAQ. Basically floating point is never to be 
trusted. This issue is quite language agnostic, however some language 
decided to "hide" the issue, python does not. For more information on 
floating point and its intricacies: 
http://docs.python.org/tutorial/floatingpoint.html

 >>>> 0.8 * 5
>> 4.0
>>
>> python 2.6.1 on mac. What the hell is going on here?
> 
> Pure curiosity prompted me to try the following:
>>>> 40 / 5.0
> 8.0
> 
> Strange...

Strange, I don't see anything strange with that...

Perhaps you meant, python returns 4.0 instead of 4? It's because in 
division with at least one of the divisor or the dividend a floating 
point will return a floating point value.

Perhaps you're confusing it with integer division, in which both divisor 
and dividend are integers. In python 2.6, this will still return 
integers, but this will change (edit: have changed) in python 3.x, 
division of integer by integer will always result in floating point even 
if the result can be represented exactly by an integer. You can do 'from 
__future__ import division' to use the new division semantic in python 2.x



More information about the Python-list mailing list