floating point arithmetic

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Aug 27 00:49:11 EDT 2008


En Tue, 26 Aug 2008 18:11:30 -0300, fred8865 <xtd8865 at gmail.com> escribi�:

> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.
> How can I tackle this problem of inaccurate floating point numbers?
> thank you

In the current Python versions (2.2 and up), 180/100 means integer  
division (because both operands are integer).
If you want a floating point result, use 180.0/100 or  
float(some_variable)/100

Starting with Python 3.0, the / operator will return a floating point  
result ("true division"). So in that Python version, 180/100 gives 1.8
To enable that behavior on Python 2.x, execute "from __future__ import  
division":

>>> 180/100
1
>>> from __future__ import division
>>> 180/100
1.8

In any Python version, 180//100 always means integer division:

>>> 180//100
1

-- 
Gabriel Genellina




More information about the Python-list mailing list