uncertainties
uncertainties copied to clipboard
NaN instead of ZeroDivisionError in unumpy
Can I get inf or nan for 1/0 as it works with numpy?
from uncertainties import unumpy
import numpy
print 1./numpy.array([0])
print 1./unumpy.uarray(([0], 0))
[ inf]
ZeroDivisionError: float division by zero
Good point: numpy and unumpy should behave in the same way, ideally. I added this to my to-do list for uncertainties (I will add the ability to wrap() function with keyword arguments first, though).
1./unumpy.uarray(([0], 1))
should similarly return [ inf]
, and 1./unumpy.uarray(([-0.], 1))
should return [-inf]
(using uncertainties
in this case breaks the constraint that the result should not vary much over the main range of the random variable, but checking this is left to the user, who is free to break this constraint).
Quick update:
I thought a bit about this problem. A clean solution is to have numbers with uncertainty accept numpy.float**
objects as their nominal value. This is currently not handled by the code, which assumes that the nominal value and standard deviation are Python floats.
I feel that handling multiple types of values may have a few important implications for the code, but I need to have a closer look at it to know for sure.
I just hit this! I need it very much :)
Is there any clever/pythonic way to catch that exception and assign np.inf
?
@lebigot Have you considered coercing all nominal values to numpy
floats? That is certainly the behavior that I would expect when using arrays from a module called unumpy
. It would make the implications consistent at least.