floating point arithmetic

Terry Reedy tjreedy at udel.edu
Tue Aug 26 22:39:31 EDT 2008



John Machin wrote:
> On Aug 27, 7:11 am, fred8865 <xtd8... at gmail.com> wrote:
> 
>> I understand that due to different arithmetic used in floating points
>> they are just approximations. Hence, 180/100=1 in my python interpreter.
> 
> It's not "hence". What you are seeing is truncating integer division.
> 
>> How can I tackle this problem of inaccurate floating point numbers?
> 
>>>> 180 / 100
> 1
>>>> 180 / 100.
> 1.8
>>>> 180 / float(100)
> 1.8
>>>> from __future__ import division
>>>> 180 / 100
> 1.8
> --

Or start using 3.0, which fixed this ;-)

 >>> 180/100
1.8




More information about the Python-list mailing list