[Python-ideas] Floating point contexts in Python core

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 11 17:52:43 CEST 2012


On 11 October 2012 15:55, Serhiy Storchaka <storchaka at gmail.com> wrote:
> On 11.10.12 15:31, Sturla Molden wrote:
>>
>>  >>> np.int64(1)/np.int64(0)
>> 0
>>
>>  >>> np.int32(1)/np.int32(0)
>> 0
>
>
> For such behavior must be some rationale.

I don't know what the rationale for that is but it is at least
controllable in numpy:

>>> import numpy as np
>>> np.seterr(all='raise')  # Exceptions instead of mostly useless values
{'over': 'raise', 'divide': 'raise', 'invalid': 'raise', 'under': 'raise'}
>>> np.int32(1) / np.int32(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FloatingPointError: divide by zero encountered in long_scalars
>>> np.float32(1e20) * np.float32(1e20)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FloatingPointError: overflow encountered in float_scalars
>>> np.float32('inf')
inf
>>> np.float32('inf') / np.float32('inf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FloatingPointError: invalid value encountered in float_scalars


Oscar



More information about the Python-ideas mailing list