[pypy-svn] pypy cmath: (lac, arigo)

arigo commits-noreply at bitbucket.org
Mon Jan 17 18:34:10 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: cmath
Changeset: r40801:5c552588af13
Date: 2011-01-17 18:17 +0100
http://bitbucket.org/pypy/pypy/changeset/5c552588af13/

Log:	(lac, arigo)

	cosh().

diff --git a/pypy/module/cmath/__init__.py b/pypy/module/cmath/__init__.py
--- a/pypy/module/cmath/__init__.py
+++ b/pypy/module/cmath/__init__.py
@@ -15,6 +15,7 @@
             "(base e) of x."),
     'log10': "Return the base-10 logarithm of x.",
     'exp': "Return the exponential value e**x.",
+    'cosh': "Return the hyperbolic cosine of x.",
     }
 
 

diff --git a/pypy/module/cmath/interp_cmath.py b/pypy/module/cmath/interp_cmath.py
--- a/pypy/module/cmath/interp_cmath.py
+++ b/pypy/module/cmath/interp_cmath.py
@@ -1,6 +1,6 @@
 import math
 from math import fabs
-from pypy.rlib.rarithmetic import copysign, asinh, log1p, isinf
+from pypy.rlib.rarithmetic import copysign, asinh, log1p, isinf, isnan
 from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
 from pypy.module.cmath import Module, names_and_docstrings
 from pypy.module.cmath.constant import DBL_MIN, CM_SCALE_UP, CM_SCALE_DOWN
@@ -16,6 +16,7 @@
 from pypy.module.cmath.special_value import atanh_special_values
 from pypy.module.cmath.special_value import log_special_values
 from pypy.module.cmath.special_value import exp_special_values
+from pypy.module.cmath.special_value import cosh_special_values
 
 
 def unaryfn(c_func):
@@ -318,3 +319,35 @@
     if isinf(real) or isinf(imag):
         raise OverflowError("math range error")
     return real, imag
+
+
+ at unaryfn
+def c_cosh(x, y):
+    if not isfinite(x) or not isfinite(y):
+        if isinf(x) and isfinite(y) and y != 0.:
+            if x > 0:
+                real = copysign(INF, math.cos(y))
+                imag = copysign(INF, math.sin(y))
+            else:
+                real = copysign(INF, math.cos(y))
+                imag = -copysign(INF, math.sin(y))
+            r = (real, imag)
+        else:
+            r = cosh_special_values[special_type(x)][special_type(y)]
+
+        # need to raise ValueError if y is +/- infinity and x is not
+        # a NaN
+        if isinf(y) and not isnan(x):
+            raise ValueError("math domain error")
+        return r
+
+    if fabs(x) > CM_LOG_LARGE_DOUBLE:
+        # deal correctly with cases where cosh(x) overflows but
+        # cosh(z) does not.
+        x_minus_one = x - copysign(1., x)
+        real = math.cos(y) * math.cosh(x_minus_one) * math.e
+        imag = math.sin(y) * math.sinh(x_minus_one) * math.e
+    else:
+        real = math.cos(y) * math.cosh(x)
+        imag = math.sin(y) * math.sinh(x)
+    return real, imag

diff --git a/pypy/module/cmath/special_value.py b/pypy/module/cmath/special_value.py
--- a/pypy/module/cmath/special_value.py
+++ b/pypy/module/cmath/special_value.py
@@ -125,3 +125,13 @@
     (INF,N), (U,U), (INF,-0.), (INF,0.), (U,U), (INF,N), (INF,N),
     (N,N),   (N,N), (N,-0.),   (N,0.),   (N,N), (N,N),   (N,N),
     ])
+
+cosh_special_values = build_table([
+    (INF,N), (U,U), (INF,0.),  (INF,-0.), (U,U), (INF,N), (INF,N),
+    (N,N),   (U,U), (U,U),     (U,U),     (U,U), (N,N),   (N,N),
+    (N,0.),  (U,U), (1.,0.),   (1.,-0.),  (U,U), (N,0.),  (N,0.),
+    (N,0.),  (U,U), (1.,-0.),  (1.,0.),   (U,U), (N,0.),  (N,0.),
+    (N,N),   (U,U), (U,U),     (U,U),     (U,U), (N,N),   (N,N),
+    (INF,N), (U,U), (INF,-0.), (INF,0.),  (U,U), (INF,N), (INF,N),
+    (N,N),   (N,N), (N,0.),    (N,0.),    (N,N), (N,N),   (N,N),
+    ])


More information about the Pypy-commit mailing list