[SciPy-user] Re: Python number handling?

skip at pobox.com skip at pobox.com
Fri Jul 15 09:41:05 EDT 2005


    >> I'm new to python...
    >> Why, why oh why did someone decide to make python do this:
    >> 
    >> Integer case
    >> 
    >> In [5]: 107 / 4 
    >> Out[5]: 26
    >> 
    >> Float case
    >> 
    >> In [6]: 107.0  / 4
    >> Out[6]: 26.75
    >> 
    >> I'm a bit worried that when it come to using variables, because you
    >> don't declare them (eg as integer or float) python might mess up
    >> division and give you the first result above rather than the second.

In recent versions of Python you have full control over truncation of
division results:

    >>> from __future__ import division
    >>> 107/4
    26.75
    >>> 107//4
    26
    >>> 107.0/4
    26.75
    >>> 107.0//4
    26.0

That will eventually be the default.

I think the main reason Guido made integer division return integer results
was that that's the way C did it.

Skip




More information about the SciPy-User mailing list