Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to let a Python program raise an alarm whenever a floating-point overflow or underflow occurs?

I have a python program P that calls numpy, scipy and many other libraries in scientific computing. I can modify program P but cannot modify the libraries it calls.

Now I want the program P raises an alarm whenever a floating-point overflow or underflow occurs. How can I do that?

An example of overflow is to compute an exponential function e^x when x is large. But that exponential function is not something I can touch. It maybe called indirectly, so checking the return value or parameter of the function is not doable. Also there might be other numerical functions that cause overflow. So I am looking for a systematic way to detect floating-point overflow at run time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try using the method seterr.

https://numpy.org/doc/stable/reference/generated/numpy.seterr.html

import numpy as np

np.seterr(over='raise')

np.exp(1000000)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading