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

arigo commits-noreply at bitbucket.org
Mon Jan 17 16:23:37 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: cmath
Changeset: r40776:069a9550b51d
Date: 2011-01-17 16:23 +0100
http://bitbucket.org/pypy/pypy/changeset/069a9550b51d/

Log:	(lac, arigo)

	acosh().

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
@@ -2,10 +2,16 @@
 # Package initialisation
 from pypy.interpreter.mixedmodule import MixedModule
 
+names_and_docstrings = {
+    'sqrt': "Return the square root of x.",
+    'acos': "Return the arc cosine of x.",
+    'acosh': "Return the hyperbolic arccosine of x.",
+    }
+
+
 class Module(MixedModule):
     appleveldefs = {
     }
 
-    interpleveldefs = {
-        'sqrt': 'interp_cmath.wrapped_sqrt',
-    }
+    interpleveldefs = dict([(name, 'interp_cmath.wrapped_' + name)
+                            for name in names_and_docstrings])

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
@@ -2,12 +2,13 @@
 from math import fabs
 from pypy.rlib.rarithmetic import copysign, asinh
 from pypy.interpreter.gateway import ObjSpace, W_Root
-from pypy.module.cmath import Module
+from pypy.module.cmath import Module, names_and_docstrings
 from pypy.module.cmath.constant import DBL_MIN, CM_SCALE_UP, CM_SCALE_DOWN
 from pypy.module.cmath.constant import CM_LARGE_DOUBLE, M_LN2
 from pypy.module.cmath.special_value import isfinite, special_type
 from pypy.module.cmath.special_value import sqrt_special_values
 from pypy.module.cmath.special_value import acos_special_values
+from pypy.module.cmath.special_value import acosh_special_values
 
 
 def unaryfn(c_func):
@@ -20,6 +21,7 @@
     name = c_func.func_name
     assert name.startswith('c_')
     wrapper.unwrap_spec = [ObjSpace, W_Root]
+    wrapper.func_doc = names_and_docstrings[name[2:]]
     globals()['wrapped_' + name[2:]] = wrapper
     return c_func
 
@@ -99,3 +101,22 @@
         real = 2.*math.atan2(s1x, s2x)
         imag = asinh(s2x*s1y - s2y*s1x)
     return (real, imag)
+
+
+ at unaryfn
+def c_acosh(x, y):
+    # XXX the following two lines seem unnecessary at least on Linux;
+    # the tests pass fine without them
+    if not isfinite(x) or not isfinite(y):
+        return acosh_special_values[special_type(x)][special_type(y)]
+
+    if fabs(x) > CM_LARGE_DOUBLE or fabs(y) > CM_LARGE_DOUBLE:
+        # avoid unnecessary overflow for large arguments
+        real = math.log(math.hypot(x/2., y/2.)) + M_LN2*2.
+        imag = math.atan2(y, x)
+    else:
+        s1x, s1y = c_sqrt(x - 1., y)
+        s2x, s2y = c_sqrt(x + 1., y)
+        real = asinh(s1x*s2x + s1y*s2y)
+        imag = 2.*math.atan2(s1y, s2x)
+    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
@@ -66,6 +66,16 @@
     (N,INF),   (N,N),    (N,N),    (N,N),     (N,N),     (N,-INF),   (N,N),
     ])
 
+acosh_special_values = build_table([
+    (INF,-P34), (INF,-P),  (INF,-P),  (INF,P),  (INF,P),  (INF,P34), (INF,N),
+    (INF,-P12), (U,U),     (U,U),     (U,U),    (U,U),    (INF,P12), (N,N),
+    (INF,-P12), (U,U),     (0.,-P12), (0.,P12), (U,U),    (INF,P12), (N,N),
+    (INF,-P12), (U,U),     (0.,-P12), (0.,P12), (U,U),    (INF,P12), (N,N),
+    (INF,-P12), (U,U),     (U,U),     (U,U),    (U,U),    (INF,P12), (N,N),
+    (INF,-P14), (INF,-0.), (INF,-0.), (INF,0.), (INF,0.), (INF,P14), (INF,N),
+    (INF,N),    (N,N),     (N,N),     (N,N),    (N,N),    (INF,N),   (N,N),
+    ])
+
 sqrt_special_values = build_table([
     (INF,-INF), (0.,-INF), (0.,-INF), (0.,INF), (0.,INF), (INF,INF), (N,INF),
     (INF,-INF), (U,U),     (U,U),     (U,U),    (U,U),    (INF,INF), (N,N),


More information about the Pypy-commit mailing list