[SciPy-user] Python number handling?

Robert Kern rkern at ucsd.edu
Fri Jul 15 07:25:52 EDT 2005


Howey, David A wrote:
> 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.
> 
> Has anyone had any problems with this?

Long, long ago, Guido decided to make integers behave this way because C 
integers behaved this way (close enough at any rate; Python differs from 
C with negative integers, but arguably does something more consistent). 
In many cases, this behavior is actually useful although it tends to get 
in the way a good amount of the time.

Numeric Int arrays also do this because they just do C operations more 
or less.

Most of us have learned to just add the .0 wherever we really want a 
float or to make the explicit float() or .astype(Float) conversion when 
we really need it.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the SciPy-User mailing list