Integer Division

Grant Edwards invalid at invalid
Fri Jun 19 10:44:17 EDT 2009


On 2009-06-19, Anjanesh Lekshminarayanan <mail at anjanesh.net> wrote:
>>>> a = 1
>>>> b = 25
>>>> a / b
> 0
>>>> float(a) / b
> 0.040000000000000001
>>>>
>
>>>> from __future__ import division
>>>> a = 1
>>>> b = 25
>>>> a / b
> 0.040000000000000001
>>>>
>
> In what simple way can I get just 0.04 ?

You can't.  There _is_no_ 0.04 when using binary IEEE floating
point (which is what's used for floats on any computer you're
likely to run across).

You could use some other data type (rationals, BCD floats,
etc.).

-- 
Grant Edwards                   grante             Yow! Hello.  I know
                                  at               the divorce rate among
                               visi.com            unmarried Catholic Alaskan
                                                   females!!



More information about the Python-list mailing list