uncertainties icon indicating copy to clipboard operation
uncertainties copied to clipboard

NaN instead of ZeroDivisionError in unumpy

Open ponty opened this issue 12 years ago • 5 comments

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

ponty avatar Feb 20 '13 16:02 ponty

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).

lebigot avatar Feb 22 '13 04:02 lebigot

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.

lebigot avatar Apr 10 '14 08:04 lebigot

I just hit this! I need it very much :)

pwaller avatar Jan 11 '16 18:01 pwaller

Is there any clever/pythonic way to catch that exception and assign np.inf?

ricleal avatar Sep 07 '16 19:09 ricleal

@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.

user3483203 avatar Aug 02 '19 16:08 user3483203