Because in the case of np.inf type is float (base data type), whereas in the case of upper_int / lower_int the data type is numpy.float64 . A similar question can be reproduced with
In [7]: a = np.float64('inf') In [8]: type(a) Out[8]: numpy.float64 In [9]: a - a RuntimeWarning: invalid value encountered in double_scalars if __name__ == '__main__': Out[9]: nan
In the case of np.inf / float -
In [3]: float('inf') - float('inf') Out[3]: nan In [11]: np.inf Out[11]: inf In [12]: type(np.inf) Out[12]: float
I think this may be because in the case of normal inf you cannot get it from the calculation. Example -
>>> 123123123123. ** 2 1.5159303447561418e+22 >>> _ ** 2 2.298044810152475e+44 >>> _ ** 2 5.281009949468725e+88 >>> _ ** 2 2.788906608638767e+177 >>> _ ** 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: (34, 'Result too large')
Instead, you always get an overflow error.
While using np.float64 you can get the infinity value from the calculation (although even at that time it would give a warning) -
In [63]: n = np.float64('123123123123123123123') In [64]: n Out[64]: 1.2312312312312313e+20 In [65]: n = n ** 2 In [66]: n = n ** 2 In [67]: n = n ** 2 In [68]: n = n ** 2 C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: overflow encountered in double_scalars if __name__ == '__main__': In [69]: n Out[69]: inf
Therefore, since you can get infinity through np.float64 by calculation, they throw more warnings when you try to do more calculations on it, which may try to reduce the number from infinity to a much smaller value, i.e. subtract / divide by infinity (multiplication or adding infinity is beautiful, because adding infinity to infinity will only return infinity). Example -
In [71]: n - n C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars if __name__ == '__main__': Out[71]: nan In [72]: n/n C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars if __name__ == '__main__': Out[72]: nan In [73]: n*n Out[73]: inf
Although in your case, I believe you may have received a direct infinite value from the source.