[SciPy-user] Python number handling?
Lance Boyle
lanceboyle at cwazy.co.uk
Wed Aug 10 19:26:29 EDT 2005
On Aug 10, 2005, at 10:19 AM, Mary Haley wrote:
> 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
Yes. But what about a / b given that a and b are dynamically typed?
Jerry
More information about the SciPy-User
mailing list