[SciPy-user] Python number handling?
Mary Haley
haley at ucar.edu
Wed Aug 10 13:19:35 EDT 2005
> Robert Kern wrote:
>
>> 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.
>
> Robert is, as usual, spot on with his comments.
>
> It should be noted that Python is moving towards having 107 / 4 return
> the floating-point result while 107 // 4 will be integer division.
>
> Note the following on Python 2.3.4
>
> >>> 107 / 4
> 26
> >>> from __future__ import division
> >>> 107 / 4
> 26.75
>
>
> I still think it is a good idea to use 4.0 when you mean to interpret 4
> as a float.
>
> -Travis
This seems like a major fundamental change to make to a language,
especially this late in the game. Won't this kind of thing break lots
of existing codes? Don't people depend on this for integer indexing?
It seems that the old behavior should be kept as is, and some
completely new syntax should be used for forcing floating point
behavior. (Or, as Travis pointed out, just use a floating point when
you really want floating point calculations to take place.)
I am rather shocked by this revelation, and am anxious to hear how
others in SciPy-land feel about this change.
--Mary
-------------------------------------------------
Mary Haley haley at ucar.edu
NCAR/SCD/VETS 303-497-1254 (voice)
1850 Table Mesa Dr 303-497-1239 (fax)
Boulder, CO 80305
-------------------------------------------------
More information about the SciPy-User
mailing list