22

I read about double scalar but just partly understand. From my understanding, it is the range that is available to calculate by Numpy. That's why most questions here focusing on the division by zero (which is an error because the answer will be out of range (infinity)).

But I am so unsure that my understanding is correct. Also, I can't see other causes about RuntimeWarning:overflow encountered in double_scalars. What can cause overflow encountered in double scalars?

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Jan
  • 1,389
  • 4
  • 17
  • 43

3 Answers3

16

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:\anaconda\lib\site-packages\ipykernel\__main__.py:9: RuntimeWarning: overflow encountered in double_scalars
Community
  • 1
  • 1
devautor
  • 2,506
  • 4
  • 21
  • 31
4

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

Also, here's 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.

3

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
romeric
  • 2,325
  • 3
  • 19
  • 35