def test_expm1(self): import math, cmath from numpypy import array, expm1 inf = float('inf') ninf = -float('inf') nan = float('nan') a = array([-5.0, -0.0, 0.0, 12345678.0, float("inf"), -float('inf'), -12343424.0]) b = expm1(a) for i in range(4): try: res = math.exp(a[i]) - 1 except OverflowError: res = float('inf') assert b[i] == res assert expm1(1e-50) == 1e-50
def test_expm1(self): import math, cmath from numpypy import array, expm1 inf = float('inf') ninf = -float('inf') nan = float('nan') cmpl = complex for c, rel_err in (('complex64', 2e-7), ('complex128', 2e-15), ('clongdouble', 2e-15)): a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.), cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.), cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.), cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.), cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.), cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf), cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf), cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan), ] b = expm1(array(a,dtype=c)) for i in range(len(a)): try: res = cmath.exp(a[i]) - 1. if a[i].imag == 0. and math.copysign(1., a[i].imag)<0: res = cmpl(res.real, -0.) elif a[i].imag == 0.: res = cmpl(res.real, 0.) except OverflowError: res = cmpl(inf, nan) except ValueError: res = cmpl(nan, nan) msg = 'result of expm1(%r(%r)) got %r expected %r\n ' % \ (c,a[i], b[i], res) # cast untranslated boxed results to float, # does no harm when translated t1 = float(res.real) t2 = float(b[i].real) self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) t1 = float(res.imag) t2 = float(b[i].imag) self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)