Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def exponent_frexp(x):
return umath.frexp(x)[1]
# to help make it a drop-in replacement for math (even though
# factorial() does not work on numbers with uncertainties
# because it is restricted to integers, as for
# math.factorial()):
assert umath.factorial(4) == 24
# Boolean functions:
assert not umath.isinf(x)
# Comparison, possibly between an AffineScalarFunc object and a
# boolean, which makes things more difficult for this code:
assert umath.isinf(x) == False
# fsum is special because it does not take a fixed number of
# variables:
assert umath.fsum([x, x]).nominal_value == -3
# The same exceptions should be generated when numbers with uncertainties
# are used:
## !! The Nose testing framework seems to catch an exception when
## it is aliased: "exc = OverflowError; ... except exc:..."
## surprisingly catches OverflowError. So, tests are written in a
## version-specific manner (until the Nose issue is resolved).
if sys.version_info < (2, 6):
try:
math.log(0)
except OverflowError(err_math): # "as", for Python 2.6+
pass
else:
def test_broadcast_funcs():
"""
Test of mathematical functions that work with NumPy arrays of
numbers with uncertainties.
"""
x = uncertainties.ufloat((0.2, 0.1))
arr = numpy.array([x, 2*x])
assert unumpy.cos(arr)[1] == uncertainties.umath.cos(arr[1])
# Some functions do not bear the same name in the math module and
# in NumPy (acos instead of arccos, etc.):
assert unumpy.arccos(arr)[1] == uncertainties.umath.acos(arr[1])
# The acos() function should not exist in unumpy because it does
# not exist in numpy:
assert not hasattr(numpy, 'acos')
assert not hasattr(unumpy, 'acos')
# Test of the __all__ variable:
assert 'acos' not in unumpy.__all__