python - What are the causes of overflow encountered in double_scalars besides division by zero

python – What are the causes of overflow encountered in double_scalars besides division by zero?

python – What are the causes of overflow encountered in double_scalars besides division by zero?

Overflow error implies that an operation yields a value out of the range defined for the corresponding data type. For numpy double, that range is (-1.79769313486e+308, 1.79769313486e+308). Also, for a good discussion, please read this SO post.

Example:

import numpy as np
np.seterr(all=warn)
print Range of numpy double:, np.finfo(np.double).min, np.finfo(np.double).max
A = np.array([143],dtype=double)
a=A[-1]
print At the border:, a**a
B = np.array([144],dtype=double)
b=B[-1]
print Blowing out of range:, b**b

Output:

Range of numpy double: -1.79769313486e+308 1.79769313486e+308
At the border: 1.6332525973e+308 
Blowing out of range: inf
D:anacondalibsite-packagesipykernel__main__.py:9: RuntimeWarning: overflow encountered in double_scalars

Another very popular cause of a RuntimeWarning:Overflow encounter is the floating point error. For more information you can look here

Also, heres some definitions of floating-point exceptions.

The floating-point exceptions are defined in the IEEE 754 standard 1:

Division by zero: infinite result obtained from finite numbers.
Overflow: result too large to be expressed.
Underflow: result so close to zero that some precision was lost.
Invalid operation: result is not an expressible number, typically indicates that a NaN was produced.

I hope this helps, Good Luck.

python – What are the causes of overflow encountered in double_scalars besides division by zero?

NumPy obeys the IEEE floating point restrictions. The smallest to largest representable numbers in floating point precisions can be queried with numpy.finfo

In [35]: np.finfo(dtype=np.float64)
Out[35]: finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)

In [36]: np.finfo(dtype=np.float32)
Out[36]: finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)

So for double precision, any numpy functions (such as divide, exp, sqrt, ...) overflowing the range ~[-1.797e+308, 1.797e+308] will raise an overflow warning.

For example:

In [37]: np.ones(1)/1e-308 # fine
Out[37]: array([  1.00000000e+308]) 
In [38]: np.ones(1)/1e-309 # overflow
/usr/bin/ipython:1: RuntimeWarning: overflow encountered in divide
Out[38]: array([ inf])
In [39]: np.exp(1000.) # overflow
/usr/bin/ipython:1: RuntimeWarning: overflow encountered in exp
Out[39]: inf

Related posts on  python  :

Leave a Reply

Your email address will not be published.