Пример #1
0
 def test_exp2(self):
     from numpypy import array, exp2
     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 = exp2(array(a,dtype=c))
         for i in range(len(a)):
             try:
                 res = self.c_pow((2,0), (a[i].real, a[i].imag))
             except OverflowError:
                 res = (inf, nan)
             except ValueError:
                 res = (nan, nan)
             msg = 'result of 2**%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[0])
             t2 = float(b[i].real)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
             t1 = float(res[1])
             t2 = float(b[i].imag)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
Пример #2
0
    def test_exp2(self):
        import math
        from numpypy import array, exp2
        inf = float('inf')
        ninf = -float('inf')
        nan = float('nan')

        a = array([-5.0, -0.0, 0.0, 2, 12345678.0, inf, ninf, -12343424.0])
        b = exp2(a)
        for i in range(len(a)):
            try:
                res = 2 ** a[i]
            except OverflowError:
                res = float('inf')
            assert b[i] == res

        assert exp2(3) == 8
        assert math.isnan(exp2(nan))