blob: 6d9b2ed3278c50cb34e4148d693a491358bb00cb [file] [log] [blame]
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001/* Complex math module */
2
3/* much code borrowed from mathmodule.c */
4
Roger E. Masse24070ca1996-12-09 22:59:53 +00005#include "Python.h"
Mark Dickinsonf3718592009-12-21 15:27:41 +00006#include "_math.h"
Christian Heimes53876d92008-04-19 00:31:39 +00007/* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from
8 float.h. We assume that FLT_RADIX is either 2 or 16. */
9#include <float.h>
Guido van Rossum71aa32f1996-01-12 01:34:57 +000010
Brett Cannonb0fc4902014-10-14 17:37:02 -040011#include "clinic/cmathmodule.c.h"
12/*[clinic input]
13output preset file
14module cmath
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ef7e0fdd8a143c03]*/
17
18/*[python input]
19class Py_complex_protected_converter(Py_complex_converter):
20 def modify(self):
21 return 'errno = 0; PyFPE_START_PROTECT("complex function", goto exit);'
22
23
24class Py_complex_protected_return_converter(CReturnConverter):
25 type = "Py_complex"
26
27 def render(self, function, data):
28 self.declare(data)
29 data.return_conversion.append("""
30PyFPE_END_PROTECT(_return_value);
31if (errno == EDOM) {{
32 PyErr_SetString(PyExc_ValueError, "math domain error");
33 goto exit;
34}}
35else if (errno == ERANGE) {{
36 PyErr_SetString(PyExc_OverflowError, "math range error");
37 goto exit;
38}}
39else {{
40 return_value = PyComplex_FromCComplex(_return_value);
41}}
42""".strip())
43[python start generated code]*/
44/*[python end generated code: output=da39a3ee5e6b4b0d input=231019039a6fbb9a]*/
45
Christian Heimes53876d92008-04-19 00:31:39 +000046#if (FLT_RADIX != 2 && FLT_RADIX != 16)
47#error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"
Guido van Rossum71aa32f1996-01-12 01:34:57 +000048#endif
49
Christian Heimes53876d92008-04-19 00:31:39 +000050#ifndef M_LN2
51#define M_LN2 (0.6931471805599453094) /* natural log of 2 */
52#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000053
Christian Heimes53876d92008-04-19 00:31:39 +000054#ifndef M_LN10
55#define M_LN10 (2.302585092994045684) /* natural log of 10 */
56#endif
57
58/*
59 CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log,
60 inverse trig and inverse hyperbolic trig functions. Its log is used in the
Ezio Melotti13925002011-03-16 11:05:33 +020061 evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary
Christian Heimes53876d92008-04-19 00:31:39 +000062 overflow.
63 */
64
65#define CM_LARGE_DOUBLE (DBL_MAX/4.)
66#define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE))
67#define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
68#define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
69
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070/*
Christian Heimes53876d92008-04-19 00:31:39 +000071 CM_SCALE_UP is an odd integer chosen such that multiplication by
72 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
73 CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
74 square roots accurately when the real and imaginary parts of the argument
75 are subnormal.
76*/
77
78#if FLT_RADIX==2
79#define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1)
80#elif FLT_RADIX==16
81#define CM_SCALE_UP (4*DBL_MANT_DIG+1)
82#endif
83#define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000084
85/* forward declarations */
Brett Cannonb0fc4902014-10-14 17:37:02 -040086static Py_complex cmath_asinh_impl(PyModuleDef *, Py_complex);
87static Py_complex cmath_atanh_impl(PyModuleDef *, Py_complex);
88static Py_complex cmath_cosh_impl(PyModuleDef *, Py_complex);
89static Py_complex cmath_sinh_impl(PyModuleDef *, Py_complex);
90static Py_complex cmath_sqrt_impl(PyModuleDef *, Py_complex);
91static Py_complex cmath_tanh_impl(PyModuleDef *, Py_complex);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000092static PyObject * math_error(void);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000093
Christian Heimes53876d92008-04-19 00:31:39 +000094/* Code to deal with special values (infinities, NaNs, etc.). */
95
96/* special_type takes a double and returns an integer code indicating
97 the type of the double as follows:
98*/
99
100enum special_types {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ST_NINF, /* 0, negative infinity */
102 ST_NEG, /* 1, negative finite number (nonzero) */
103 ST_NZERO, /* 2, -0. */
104 ST_PZERO, /* 3, +0. */
105 ST_POS, /* 4, positive finite number (nonzero) */
106 ST_PINF, /* 5, positive infinity */
107 ST_NAN /* 6, Not a Number */
Christian Heimes53876d92008-04-19 00:31:39 +0000108};
109
110static enum special_types
111special_type(double d)
112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (Py_IS_FINITE(d)) {
114 if (d != 0) {
115 if (copysign(1., d) == 1.)
116 return ST_POS;
117 else
118 return ST_NEG;
119 }
120 else {
121 if (copysign(1., d) == 1.)
122 return ST_PZERO;
123 else
124 return ST_NZERO;
125 }
126 }
127 if (Py_IS_NAN(d))
128 return ST_NAN;
129 if (copysign(1., d) == 1.)
130 return ST_PINF;
131 else
132 return ST_NINF;
Christian Heimes53876d92008-04-19 00:31:39 +0000133}
134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135#define SPECIAL_VALUE(z, table) \
136 if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
137 errno = 0; \
138 return table[special_type((z).real)] \
139 [special_type((z).imag)]; \
140 }
Christian Heimes53876d92008-04-19 00:31:39 +0000141
142#define P Py_MATH_PI
143#define P14 0.25*Py_MATH_PI
144#define P12 0.5*Py_MATH_PI
145#define P34 0.75*Py_MATH_PI
Christian Heimesa342c012008-04-20 21:01:16 +0000146#define INF Py_HUGE_VAL
147#define N Py_NAN
Christian Heimes53876d92008-04-19 00:31:39 +0000148#define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
149
150/* First, the C functions that do the real work. Each of the c_*
151 functions computes and returns the C99 Annex G recommended result
152 and also sets errno as follows: errno = 0 if no floating-point
153 exception is associated with the result; errno = EDOM if C99 Annex
154 G recommends raising divide-by-zero or invalid for this result; and
155 errno = ERANGE where the overflow floating-point signal should be
156 raised.
157*/
158
Christian Heimesa342c012008-04-20 21:01:16 +0000159static Py_complex acos_special_values[7][7];
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000160
Brett Cannonb0fc4902014-10-14 17:37:02 -0400161/*[clinic input]
162cmath.acos -> Py_complex_protected
163
164 z: Py_complex_protected
165 /
166
167Return the arc cosine of z.
168[clinic start generated code]*/
169
Tim Peters14e26402001-02-20 20:15:19 +0000170static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400171cmath_acos_impl(PyModuleDef *module, Py_complex z)
172/*[clinic end generated code: output=7c1dd21ff818db6b input=bd6cbd78ae851927]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 SPECIAL_VALUE(z, acos_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
179 /* avoid unnecessary overflow for large arguments */
180 r.real = atan2(fabs(z.imag), z.real);
181 /* split into cases to make sure that the branch cut has the
182 correct continuity on systems with unsigned zeros */
183 if (z.real < 0.) {
184 r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
185 M_LN2*2., z.imag);
186 } else {
187 r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
188 M_LN2*2., -z.imag);
189 }
190 } else {
191 s1.real = 1.-z.real;
192 s1.imag = -z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400193 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 s2.real = 1.+z.real;
195 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400196 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 r.real = 2.*atan2(s1.real, s2.real);
198 r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real);
199 }
200 errno = 0;
201 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000202}
203
Guido van Rossumc6e22901998-12-04 19:26:43 +0000204
Christian Heimesa342c012008-04-20 21:01:16 +0000205static Py_complex acosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000206
Brett Cannonb0fc4902014-10-14 17:37:02 -0400207/*[clinic input]
208cmath.acosh = cmath.acos
209
210Return the hyperbolic arccosine of z.
211[clinic start generated code]*/
212
Tim Peters14e26402001-02-20 20:15:19 +0000213static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400214cmath_acosh_impl(PyModuleDef *module, Py_complex z)
215/*[clinic end generated code: output=c23c776429def981 input=bc016412080bb3e9]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 SPECIAL_VALUE(z, acosh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
222 /* avoid unnecessary overflow for large arguments */
223 r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
224 r.imag = atan2(z.imag, z.real);
225 } else {
226 s1.real = z.real - 1.;
227 s1.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400228 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 s2.real = z.real + 1.;
230 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400231 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag);
233 r.imag = 2.*atan2(s1.imag, s2.real);
234 }
235 errno = 0;
236 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000237}
238
Brett Cannonb0fc4902014-10-14 17:37:02 -0400239/*[clinic input]
240cmath.asin = cmath.acos
Guido van Rossumc6e22901998-12-04 19:26:43 +0000241
Brett Cannonb0fc4902014-10-14 17:37:02 -0400242Return the arc sine of z.
243[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000244
Tim Peters14e26402001-02-20 20:15:19 +0000245static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400246cmath_asin_impl(PyModuleDef *module, Py_complex z)
247/*[clinic end generated code: output=42d2346d46690826 input=be0bf0cfdd5239c5]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 /* asin(z) = -i asinh(iz) */
250 Py_complex s, r;
251 s.real = -z.imag;
252 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400253 s = cmath_asinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 r.real = s.imag;
255 r.imag = -s.real;
256 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000257}
258
Guido van Rossumc6e22901998-12-04 19:26:43 +0000259
Christian Heimesa342c012008-04-20 21:01:16 +0000260static Py_complex asinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000261
Brett Cannonb0fc4902014-10-14 17:37:02 -0400262/*[clinic input]
263cmath.asinh = cmath.acos
264
265Return the hyperbolic arc sine of z.
266[clinic start generated code]*/
267
Tim Peters14e26402001-02-20 20:15:19 +0000268static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400269cmath_asinh_impl(PyModuleDef *module, Py_complex z)
270/*[clinic end generated code: output=0c6664823c7b1b35 input=5a21fa0242928c9b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 SPECIAL_VALUE(z, asinh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
277 if (z.imag >= 0.) {
278 r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
279 M_LN2*2., z.real);
280 } else {
281 r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
282 M_LN2*2., -z.real);
283 }
284 r.imag = atan2(z.imag, fabs(z.real));
285 } else {
286 s1.real = 1.+z.imag;
287 s1.imag = -z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400288 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 s2.real = 1.-z.imag;
290 s2.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400291 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag);
293 r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
294 }
295 errno = 0;
296 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000297}
298
Guido van Rossumc6e22901998-12-04 19:26:43 +0000299
Brett Cannonb0fc4902014-10-14 17:37:02 -0400300/*[clinic input]
301cmath.atan = cmath.acos
302
303Return the arc tangent of z.
304[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000305
Tim Peters14e26402001-02-20 20:15:19 +0000306static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400307cmath_atan_impl(PyModuleDef *module, Py_complex z)
308/*[clinic end generated code: output=b7d44f02c6a5c3b5 input=3b21ff7d5eac632a]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* atan(z) = -i atanh(iz) */
311 Py_complex s, r;
312 s.real = -z.imag;
313 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400314 s = cmath_atanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 r.real = s.imag;
316 r.imag = -s.real;
317 return r;
Christian Heimes53876d92008-04-19 00:31:39 +0000318}
319
Christian Heimese57950f2008-04-21 13:08:03 +0000320/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
321 C99 for atan2(0., 0.). */
Christian Heimes53876d92008-04-19 00:31:39 +0000322static double
323c_atan2(Py_complex z)
324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
326 return Py_NAN;
327 if (Py_IS_INFINITY(z.imag)) {
328 if (Py_IS_INFINITY(z.real)) {
329 if (copysign(1., z.real) == 1.)
330 /* atan2(+-inf, +inf) == +-pi/4 */
331 return copysign(0.25*Py_MATH_PI, z.imag);
332 else
333 /* atan2(+-inf, -inf) == +-pi*3/4 */
334 return copysign(0.75*Py_MATH_PI, z.imag);
335 }
336 /* atan2(+-inf, x) == +-pi/2 for finite x */
337 return copysign(0.5*Py_MATH_PI, z.imag);
338 }
339 if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
340 if (copysign(1., z.real) == 1.)
341 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
342 return copysign(0., z.imag);
343 else
344 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
345 return copysign(Py_MATH_PI, z.imag);
346 }
347 return atan2(z.imag, z.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000348}
349
Guido van Rossumc6e22901998-12-04 19:26:43 +0000350
Christian Heimesa342c012008-04-20 21:01:16 +0000351static Py_complex atanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000352
Brett Cannonb0fc4902014-10-14 17:37:02 -0400353/*[clinic input]
354cmath.atanh = cmath.acos
355
356Return the hyperbolic arc tangent of z.
357[clinic start generated code]*/
358
Tim Peters14e26402001-02-20 20:15:19 +0000359static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400360cmath_atanh_impl(PyModuleDef *module, Py_complex z)
361/*[clinic end generated code: output=279e0b9fefc8da7c input=df19cdc9f9d431c9]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_complex r;
364 double ay, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 SPECIAL_VALUE(z, atanh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
369 if (z.real < 0.) {
Brett Cannonb0fc4902014-10-14 17:37:02 -0400370 return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Christian Heimes53876d92008-04-19 00:31:39 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 ay = fabs(z.imag);
374 if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
375 /*
376 if abs(z) is large then we use the approximation
377 atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
378 of z.imag)
379 */
380 h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
381 r.real = z.real/4./h/h;
382 /* the two negations in the next line cancel each other out
383 except when working with unsigned zeros: they're there to
384 ensure that the branch cut has the correct continuity on
385 systems that don't support signed zeros */
386 r.imag = -copysign(Py_MATH_PI/2., -z.imag);
387 errno = 0;
388 } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
389 /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
390 if (ay == 0.) {
391 r.real = INF;
392 r.imag = z.imag;
393 errno = EDOM;
394 } else {
395 r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
396 r.imag = copysign(atan2(2., -ay)/2, z.imag);
397 errno = 0;
398 }
399 } else {
400 r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
401 r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
402 errno = 0;
403 }
404 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000405}
406
Guido van Rossumc6e22901998-12-04 19:26:43 +0000407
Brett Cannonb0fc4902014-10-14 17:37:02 -0400408/*[clinic input]
409cmath.cos = cmath.acos
410
411Return the cosine of z.
412[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000413
Tim Peters14e26402001-02-20 20:15:19 +0000414static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400415cmath_cos_impl(PyModuleDef *module, Py_complex z)
416/*[clinic end generated code: output=9d1cdc1b5e761667 input=6022e39b77127ac7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 /* cos(z) = cosh(iz) */
419 Py_complex r;
420 r.real = -z.imag;
421 r.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400422 r = cmath_cosh_impl(module, r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000424}
425
Guido van Rossumc6e22901998-12-04 19:26:43 +0000426
Christian Heimes53876d92008-04-19 00:31:39 +0000427/* cosh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000428static Py_complex cosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000429
Brett Cannonb0fc4902014-10-14 17:37:02 -0400430/*[clinic input]
431cmath.cosh = cmath.acos
432
433Return the hyperbolic cosine of z.
434[clinic start generated code]*/
435
Tim Peters14e26402001-02-20 20:15:19 +0000436static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400437cmath_cosh_impl(PyModuleDef *module, Py_complex z)
438/*[clinic end generated code: output=f3b5d3282b3024d3 input=d6b66339e9cc332b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 Py_complex r;
441 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
444 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
445 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
446 (z.imag != 0.)) {
447 if (z.real > 0) {
448 r.real = copysign(INF, cos(z.imag));
449 r.imag = copysign(INF, sin(z.imag));
450 }
451 else {
452 r.real = copysign(INF, cos(z.imag));
453 r.imag = -copysign(INF, sin(z.imag));
454 }
455 }
456 else {
457 r = cosh_special_values[special_type(z.real)]
458 [special_type(z.imag)];
459 }
460 /* need to set errno = EDOM if y is +/- infinity and x is not
461 a NaN */
462 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
463 errno = EDOM;
464 else
465 errno = 0;
466 return r;
467 }
Christian Heimes53876d92008-04-19 00:31:39 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
470 /* deal correctly with cases where cosh(z.real) overflows but
471 cosh(z) does not. */
472 x_minus_one = z.real - copysign(1., z.real);
473 r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
474 r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
475 } else {
476 r.real = cos(z.imag) * cosh(z.real);
477 r.imag = sin(z.imag) * sinh(z.real);
478 }
479 /* detect overflow, and set errno accordingly */
480 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
481 errno = ERANGE;
482 else
483 errno = 0;
484 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000485}
486
Guido van Rossumc6e22901998-12-04 19:26:43 +0000487
Christian Heimes53876d92008-04-19 00:31:39 +0000488/* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for
489 finite y */
Christian Heimesa342c012008-04-20 21:01:16 +0000490static Py_complex exp_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000491
Brett Cannonb0fc4902014-10-14 17:37:02 -0400492/*[clinic input]
493cmath.exp = cmath.acos
494
495Return the exponential value e**z.
496[clinic start generated code]*/
497
Tim Peters14e26402001-02-20 20:15:19 +0000498static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400499cmath_exp_impl(PyModuleDef *module, Py_complex z)
500/*[clinic end generated code: output=6f8825eb2bcad9ba input=8b9e6cf8a92174c3]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 Py_complex r;
503 double l;
Christian Heimes53876d92008-04-19 00:31:39 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
506 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
507 && (z.imag != 0.)) {
508 if (z.real > 0) {
509 r.real = copysign(INF, cos(z.imag));
510 r.imag = copysign(INF, sin(z.imag));
511 }
512 else {
513 r.real = copysign(0., cos(z.imag));
514 r.imag = copysign(0., sin(z.imag));
515 }
516 }
517 else {
518 r = exp_special_values[special_type(z.real)]
519 [special_type(z.imag)];
520 }
521 /* need to set errno = EDOM if y is +/- infinity and x is not
522 a NaN and not -infinity */
523 if (Py_IS_INFINITY(z.imag) &&
524 (Py_IS_FINITE(z.real) ||
525 (Py_IS_INFINITY(z.real) && z.real > 0)))
526 errno = EDOM;
527 else
528 errno = 0;
529 return r;
530 }
Christian Heimes53876d92008-04-19 00:31:39 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (z.real > CM_LOG_LARGE_DOUBLE) {
533 l = exp(z.real-1.);
534 r.real = l*cos(z.imag)*Py_MATH_E;
535 r.imag = l*sin(z.imag)*Py_MATH_E;
536 } else {
537 l = exp(z.real);
538 r.real = l*cos(z.imag);
539 r.imag = l*sin(z.imag);
540 }
541 /* detect overflow, and set errno accordingly */
542 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
543 errno = ERANGE;
544 else
545 errno = 0;
546 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000547}
548
Christian Heimesa342c012008-04-20 21:01:16 +0000549static Py_complex log_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000550
Tim Peters14e26402001-02-20 20:15:19 +0000551static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000552c_log(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /*
555 The usual formula for the real part is log(hypot(z.real, z.imag)).
556 There are four situations where this formula is potentially
557 problematic:
Christian Heimes53876d92008-04-19 00:31:39 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 (1) the absolute value of z is subnormal. Then hypot is subnormal,
560 so has fewer than the usual number of bits of accuracy, hence may
561 have large relative error. This then gives a large absolute error
562 in the log. This can be solved by rescaling z by a suitable power
563 of 2.
Christian Heimes53876d92008-04-19 00:31:39 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 (2) the absolute value of z is greater than DBL_MAX (e.g. when both
566 z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
567 Again, rescaling solves this.
Christian Heimes53876d92008-04-19 00:31:39 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 (3) the absolute value of z is close to 1. In this case it's
570 difficult to achieve good accuracy, at least in part because a
571 change of 1ulp in the real or imaginary part of z can result in a
572 change of billions of ulps in the correctly rounded answer.
Christian Heimes53876d92008-04-19 00:31:39 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 (4) z = 0. The simplest thing to do here is to call the
575 floating-point log with an argument of 0, and let its behaviour
576 (returning -infinity, signaling a floating-point exception, setting
577 errno, or whatever) determine that of c_log. So the usual formula
578 is fine here.
Christian Heimes53876d92008-04-19 00:31:39 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 */
Christian Heimes53876d92008-04-19 00:31:39 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 Py_complex r;
583 double ax, ay, am, an, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 SPECIAL_VALUE(z, log_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 ax = fabs(z.real);
588 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
591 r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
592 } else if (ax < DBL_MIN && ay < DBL_MIN) {
593 if (ax > 0. || ay > 0.) {
594 /* catch cases where hypot(ax, ay) is subnormal */
595 r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
596 ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
597 }
598 else {
599 /* log(+/-0. +/- 0i) */
600 r.real = -INF;
601 r.imag = atan2(z.imag, z.real);
602 errno = EDOM;
603 return r;
604 }
605 } else {
606 h = hypot(ax, ay);
607 if (0.71 <= h && h <= 1.73) {
608 am = ax > ay ? ax : ay; /* max(ax, ay) */
609 an = ax > ay ? ay : ax; /* min(ax, ay) */
610 r.real = m_log1p((am-1)*(am+1)+an*an)/2.;
611 } else {
612 r.real = log(h);
613 }
614 }
615 r.imag = atan2(z.imag, z.real);
616 errno = 0;
617 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000618}
619
Guido van Rossumc6e22901998-12-04 19:26:43 +0000620
Brett Cannonb0fc4902014-10-14 17:37:02 -0400621/*[clinic input]
622cmath.log10 = cmath.acos
623
624Return the base-10 logarithm of z.
625[clinic start generated code]*/
626
Tim Peters14e26402001-02-20 20:15:19 +0000627static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400628cmath_log10_impl(PyModuleDef *module, Py_complex z)
629/*[clinic end generated code: output=c7c426ca0e782341 input=cff5644f73c1519c]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 Py_complex r;
632 int errno_save;
Christian Heimes53876d92008-04-19 00:31:39 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 r = c_log(z);
635 errno_save = errno; /* just in case the divisions affect errno */
636 r.real = r.real / M_LN10;
637 r.imag = r.imag / M_LN10;
638 errno = errno_save;
639 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000640}
641
Guido van Rossumc6e22901998-12-04 19:26:43 +0000642
Brett Cannonb0fc4902014-10-14 17:37:02 -0400643/*[clinic input]
644cmath.sin = cmath.acos
645
646Return the sine of z.
647[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000648
Tim Peters14e26402001-02-20 20:15:19 +0000649static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400650cmath_sin_impl(PyModuleDef *module, Py_complex z)
651/*[clinic end generated code: output=e7f5e2b253825ac7 input=2d3519842a8b4b85]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* sin(z) = -i sin(iz) */
654 Py_complex s, r;
655 s.real = -z.imag;
656 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400657 s = cmath_sinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 r.real = s.imag;
659 r.imag = -s.real;
660 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000661}
662
Guido van Rossumc6e22901998-12-04 19:26:43 +0000663
Christian Heimes53876d92008-04-19 00:31:39 +0000664/* sinh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000665static Py_complex sinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000666
Brett Cannonb0fc4902014-10-14 17:37:02 -0400667/*[clinic input]
668cmath.sinh = cmath.acos
669
670Return the hyperbolic sine of z.
671[clinic start generated code]*/
672
Tim Peters14e26402001-02-20 20:15:19 +0000673static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400674cmath_sinh_impl(PyModuleDef *module, Py_complex z)
675/*[clinic end generated code: output=d71fff8298043a95 input=d2d3fc8c1ddfd2dd]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_complex r;
678 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 /* special treatment for sinh(+/-inf + iy) if y is finite and
681 nonzero */
682 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
683 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
684 && (z.imag != 0.)) {
685 if (z.real > 0) {
686 r.real = copysign(INF, cos(z.imag));
687 r.imag = copysign(INF, sin(z.imag));
688 }
689 else {
690 r.real = -copysign(INF, cos(z.imag));
691 r.imag = copysign(INF, sin(z.imag));
692 }
693 }
694 else {
695 r = sinh_special_values[special_type(z.real)]
696 [special_type(z.imag)];
697 }
698 /* need to set errno = EDOM if y is +/- infinity and x is not
699 a NaN */
700 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
701 errno = EDOM;
702 else
703 errno = 0;
704 return r;
705 }
Christian Heimes53876d92008-04-19 00:31:39 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
708 x_minus_one = z.real - copysign(1., z.real);
709 r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
710 r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
711 } else {
712 r.real = cos(z.imag) * sinh(z.real);
713 r.imag = sin(z.imag) * cosh(z.real);
714 }
715 /* detect overflow, and set errno accordingly */
716 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
717 errno = ERANGE;
718 else
719 errno = 0;
720 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000721}
722
Guido van Rossumc6e22901998-12-04 19:26:43 +0000723
Christian Heimesa342c012008-04-20 21:01:16 +0000724static Py_complex sqrt_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000725
Brett Cannonb0fc4902014-10-14 17:37:02 -0400726/*[clinic input]
727cmath.sqrt = cmath.acos
728
729Return the square root of z.
730[clinic start generated code]*/
731
Tim Peters14e26402001-02-20 20:15:19 +0000732static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400733cmath_sqrt_impl(PyModuleDef *module, Py_complex z)
734/*[clinic end generated code: output=b6bda283d0c5a7b4 input=7088b166fc9a58c7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 /*
737 Method: use symmetries to reduce to the case when x = z.real and y
738 = z.imag are nonnegative. Then the real part of the result is
739 given by
Christian Heimes53876d92008-04-19 00:31:39 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 s = sqrt((x + hypot(x, y))/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 and the imaginary part is
Christian Heimes53876d92008-04-19 00:31:39 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 d = (y/2)/s
Christian Heimes53876d92008-04-19 00:31:39 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 If either x or y is very large then there's a risk of overflow in
748 computation of the expression x + hypot(x, y). We can avoid this
749 by rewriting the formula for s as:
Christian Heimes53876d92008-04-19 00:31:39 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 s = 2*sqrt(x/8 + hypot(x/8, y/8))
Christian Heimes53876d92008-04-19 00:31:39 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 This costs us two extra multiplications/divisions, but avoids the
754 overhead of checking for x and y large.
Christian Heimes53876d92008-04-19 00:31:39 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 If both x and y are subnormal then hypot(x, y) may also be
757 subnormal, so will lack full precision. We solve this by rescaling
758 x and y by a sufficiently large power of 2 to ensure that x and y
759 are normal.
760 */
Christian Heimes53876d92008-04-19 00:31:39 +0000761
762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 Py_complex r;
764 double s,d;
765 double ax, ay;
Christian Heimes53876d92008-04-19 00:31:39 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 SPECIAL_VALUE(z, sqrt_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (z.real == 0. && z.imag == 0.) {
770 r.real = 0.;
771 r.imag = z.imag;
772 return r;
773 }
Christian Heimes53876d92008-04-19 00:31:39 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 ax = fabs(z.real);
776 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
779 /* here we catch cases where hypot(ax, ay) is subnormal */
780 ax = ldexp(ax, CM_SCALE_UP);
781 s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
782 CM_SCALE_DOWN);
783 } else {
784 ax /= 8.;
785 s = 2.*sqrt(ax + hypot(ax, ay/8.));
786 }
787 d = ay/(2.*s);
Christian Heimes53876d92008-04-19 00:31:39 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 if (z.real >= 0.) {
790 r.real = s;
791 r.imag = copysign(d, z.imag);
792 } else {
793 r.real = d;
794 r.imag = copysign(s, z.imag);
795 }
796 errno = 0;
797 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000798}
799
Guido van Rossumc6e22901998-12-04 19:26:43 +0000800
Brett Cannonb0fc4902014-10-14 17:37:02 -0400801/*[clinic input]
802cmath.tan = cmath.acos
803
804Return the tangent of z.
805[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000806
Tim Peters14e26402001-02-20 20:15:19 +0000807static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400808cmath_tan_impl(PyModuleDef *module, Py_complex z)
809/*[clinic end generated code: output=df374bacf36d99b4 input=fc167e528767888e]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 /* tan(z) = -i tanh(iz) */
812 Py_complex s, r;
813 s.real = -z.imag;
814 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400815 s = cmath_tanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 r.real = s.imag;
817 r.imag = -s.real;
818 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000819}
820
Guido van Rossumc6e22901998-12-04 19:26:43 +0000821
Christian Heimes53876d92008-04-19 00:31:39 +0000822/* tanh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000823static Py_complex tanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000824
Brett Cannonb0fc4902014-10-14 17:37:02 -0400825/*[clinic input]
826cmath.tanh = cmath.acos
827
828Return the hyperbolic tangent of z.
829[clinic start generated code]*/
830
Tim Peters14e26402001-02-20 20:15:19 +0000831static Py_complex
Brett Cannonb0fc4902014-10-14 17:37:02 -0400832cmath_tanh_impl(PyModuleDef *module, Py_complex z)
833/*[clinic end generated code: output=f578773d27a18e96 input=22f67f9dc6d29685]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 /* Formula:
Christian Heimes53876d92008-04-19 00:31:39 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
838 (1+tan(y)^2 tanh(x)^2)
Christian Heimes53876d92008-04-19 00:31:39 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
841 as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
842 by 4 exp(-2*x) instead, to avoid possible overflow in the
843 computation of cosh(x).
Christian Heimes53876d92008-04-19 00:31:39 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 */
Christian Heimes53876d92008-04-19 00:31:39 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_complex r;
848 double tx, ty, cx, txty, denom;
Christian Heimes53876d92008-04-19 00:31:39 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* special treatment for tanh(+/-inf + iy) if y is finite and
851 nonzero */
852 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
853 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
854 && (z.imag != 0.)) {
855 if (z.real > 0) {
856 r.real = 1.0;
857 r.imag = copysign(0.,
858 2.*sin(z.imag)*cos(z.imag));
859 }
860 else {
861 r.real = -1.0;
862 r.imag = copysign(0.,
863 2.*sin(z.imag)*cos(z.imag));
864 }
865 }
866 else {
867 r = tanh_special_values[special_type(z.real)]
868 [special_type(z.imag)];
869 }
870 /* need to set errno = EDOM if z.imag is +/-infinity and
871 z.real is finite */
872 if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
873 errno = EDOM;
874 else
875 errno = 0;
876 return r;
877 }
Christian Heimes53876d92008-04-19 00:31:39 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 /* danger of overflow in 2.*z.imag !*/
880 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
881 r.real = copysign(1., z.real);
882 r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
883 } else {
884 tx = tanh(z.real);
885 ty = tan(z.imag);
886 cx = 1./cosh(z.real);
887 txty = tx*ty;
888 denom = 1. + txty*txty;
889 r.real = tx*(1.+ty*ty)/denom;
890 r.imag = ((ty/denom)*cx)*cx;
891 }
892 errno = 0;
893 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000894}
895
Guido van Rossumc6e22901998-12-04 19:26:43 +0000896
Brett Cannonb0fc4902014-10-14 17:37:02 -0400897/*[clinic input]
898cmath.log
899
900 x: Py_complex
901 y_obj: object = NULL
902 /
903
904The logarithm of z to the given base.
905
906If the base not specified, returns the natural logarithm (base e) of z.
907[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000908
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000909static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -0400910cmath_log_impl(PyModuleDef *module, Py_complex x, PyObject *y_obj)
911/*[clinic end generated code: output=35e2a1e5229b5a46 input=ee0e823a7c6e68ea]*/
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_complex y;
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 errno = 0;
916 PyFPE_START_PROTECT("complex function", return 0)
917 x = c_log(x);
Brett Cannonb0fc4902014-10-14 17:37:02 -0400918 if (y_obj != NULL) {
919 y = PyComplex_AsCComplex(y_obj);
920 if (PyErr_Occurred()) {
921 return NULL;
922 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 y = c_log(y);
Antoine Pitroude08cb62014-07-07 19:08:47 -0400924 x = _Py_c_quot(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 }
926 PyFPE_END_PROTECT(x)
927 if (errno != 0)
928 return math_error();
929 return PyComplex_FromCComplex(x);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000930}
931
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000932
933/* And now the glue to make them available from Python: */
934
Roger E. Masse24070ca1996-12-09 22:59:53 +0000935static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000936math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 if (errno == EDOM)
939 PyErr_SetString(PyExc_ValueError, "math domain error");
940 else if (errno == ERANGE)
941 PyErr_SetString(PyExc_OverflowError, "math range error");
942 else /* Unexpected math error */
943 PyErr_SetFromErrno(PyExc_ValueError);
944 return NULL;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000945}
946
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000947
Brett Cannonb0fc4902014-10-14 17:37:02 -0400948/*[clinic input]
949cmath.phase
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000950
Brett Cannonb0fc4902014-10-14 17:37:02 -0400951 z: Py_complex
952 /
953
954Return argument, also known as the phase angle, of a complex.
955[clinic start generated code]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000956
Christian Heimes53876d92008-04-19 00:31:39 +0000957static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -0400958cmath_phase_impl(PyModuleDef *module, Py_complex z)
959/*[clinic end generated code: output=e09eaf373cb624c3 input=5cf75228ba94b69d]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 double phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 errno = 0;
964 PyFPE_START_PROTECT("arg function", return 0)
965 phi = c_atan2(z);
966 PyFPE_END_PROTECT(phi)
967 if (errno != 0)
968 return math_error();
969 else
970 return PyFloat_FromDouble(phi);
Christian Heimes53876d92008-04-19 00:31:39 +0000971}
972
Brett Cannonb0fc4902014-10-14 17:37:02 -0400973/*[clinic input]
974cmath.polar
975
976 z: Py_complex
977 /
978
979Convert a complex from rectangular coordinates to polar coordinates.
980
981r is the distance from 0 and phi the phase angle.
982[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000983
984static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -0400985cmath_polar_impl(PyModuleDef *module, Py_complex z)
986/*[clinic end generated code: output=07d41b16c877875a input=26c353574fd1a861]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 double r, phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyFPE_START_PROTECT("polar function", return 0)
991 phi = c_atan2(z); /* should not cause any exception */
Antoine Pitroude08cb62014-07-07 19:08:47 -0400992 r = _Py_c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyFPE_END_PROTECT(r)
994 if (errno != 0)
995 return math_error();
996 else
997 return Py_BuildValue("dd", r, phi);
Christian Heimes53876d92008-04-19 00:31:39 +0000998}
999
Christian Heimes53876d92008-04-19 00:31:39 +00001000/*
1001 rect() isn't covered by the C99 standard, but it's not too hard to
1002 figure out 'spirit of C99' rules for special value handing:
1003
1004 rect(x, t) should behave like exp(log(x) + it) for positive-signed x
1005 rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x
1006 rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0)
1007 gives nan +- i0 with the sign of the imaginary part unspecified.
1008
1009*/
1010
Christian Heimesa342c012008-04-20 21:01:16 +00001011static Py_complex rect_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +00001012
Brett Cannonb0fc4902014-10-14 17:37:02 -04001013/*[clinic input]
1014cmath.rect
1015
1016 r: double
1017 phi: double
1018 /
1019
1020Convert from polar coordinates to rectangular coordinates.
1021[clinic start generated code]*/
1022
Christian Heimes53876d92008-04-19 00:31:39 +00001023static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -04001024cmath_rect_impl(PyModuleDef *module, double r, double phi)
1025/*[clinic end generated code: output=d97a8749bd63e9d5 input=24c5646d147efd69]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 Py_complex z;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 errno = 0;
1029 PyFPE_START_PROTECT("rect function", return 0)
Christian Heimes53876d92008-04-19 00:31:39 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 /* deal with special values */
1032 if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
1033 /* if r is +/-infinity and phi is finite but nonzero then
1034 result is (+-INF +-INF i), but we need to compute cos(phi)
1035 and sin(phi) to figure out the signs. */
1036 if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
1037 && (phi != 0.))) {
1038 if (r > 0) {
1039 z.real = copysign(INF, cos(phi));
1040 z.imag = copysign(INF, sin(phi));
1041 }
1042 else {
1043 z.real = -copysign(INF, cos(phi));
1044 z.imag = -copysign(INF, sin(phi));
1045 }
1046 }
1047 else {
1048 z = rect_special_values[special_type(r)]
1049 [special_type(phi)];
1050 }
1051 /* need to set errno = EDOM if r is a nonzero number and phi
1052 is infinite */
1053 if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
1054 errno = EDOM;
1055 else
1056 errno = 0;
1057 }
Mark Dickinson58ceecf2013-07-20 17:59:13 +01001058 else if (phi == 0.0) {
1059 /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See
1060 bugs.python.org/issue18513. */
1061 z.real = r;
1062 z.imag = r * phi;
1063 errno = 0;
1064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 else {
1066 z.real = r * cos(phi);
1067 z.imag = r * sin(phi);
1068 errno = 0;
1069 }
Christian Heimes53876d92008-04-19 00:31:39 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyFPE_END_PROTECT(z)
1072 if (errno != 0)
1073 return math_error();
1074 else
1075 return PyComplex_FromCComplex(z);
Christian Heimes53876d92008-04-19 00:31:39 +00001076}
1077
Brett Cannonb0fc4902014-10-14 17:37:02 -04001078/*[clinic input]
1079cmath.isfinite = cmath.polar
1080
1081Return True if both the real and imaginary parts of z are finite, else False.
1082[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001083
1084static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -04001085cmath_isfinite_impl(PyModuleDef *module, Py_complex z)
1086/*[clinic end generated code: output=8f6682fa93de45d6 input=848e7ee701895815]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001087{
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001088 return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
1089}
1090
Brett Cannonb0fc4902014-10-14 17:37:02 -04001091/*[clinic input]
1092cmath.isnan = cmath.polar
1093
1094Checks if the real or imaginary part of z not a number (NaN).
1095[clinic start generated code]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001096
1097static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -04001098cmath_isnan_impl(PyModuleDef *module, Py_complex z)
1099/*[clinic end generated code: output=b85fe8c2047718ee input=71799f5d284c9baf]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001102}
1103
Brett Cannonb0fc4902014-10-14 17:37:02 -04001104/*[clinic input]
1105cmath.isinf = cmath.polar
1106
1107Checks if the real or imaginary part of z is infinite.
1108[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001109
1110static PyObject *
Brett Cannonb0fc4902014-10-14 17:37:02 -04001111cmath_isinf_impl(PyModuleDef *module, Py_complex z)
1112/*[clinic end generated code: output=8ca9c6109e468bf4 input=363df155c7181329]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
1115 Py_IS_INFINITY(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001116}
1117
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001119PyDoc_STRVAR(module_doc,
Tim Peters14e26402001-02-20 20:15:19 +00001120"This module is always available. It provides access to mathematical\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001121"functions for complex numbers.");
Guido van Rossumc6e22901998-12-04 19:26:43 +00001122
Roger E. Masse24070ca1996-12-09 22:59:53 +00001123static PyMethodDef cmath_methods[] = {
Brett Cannonb0fc4902014-10-14 17:37:02 -04001124 CMATH_ACOS_METHODDEF
1125 CMATH_ACOSH_METHODDEF
1126 CMATH_ASIN_METHODDEF
1127 CMATH_ASINH_METHODDEF
1128 CMATH_ATAN_METHODDEF
1129 CMATH_ATANH_METHODDEF
1130 CMATH_COS_METHODDEF
1131 CMATH_COSH_METHODDEF
1132 CMATH_EXP_METHODDEF
1133 CMATH_ISFINITE_METHODDEF
1134 CMATH_ISINF_METHODDEF
1135 CMATH_ISNAN_METHODDEF
1136 CMATH_LOG_METHODDEF
1137 CMATH_LOG10_METHODDEF
1138 CMATH_PHASE_METHODDEF
1139 CMATH_POLAR_METHODDEF
1140 CMATH_RECT_METHODDEF
1141 CMATH_SIN_METHODDEF
1142 CMATH_SINH_METHODDEF
1143 CMATH_SQRT_METHODDEF
1144 CMATH_TAN_METHODDEF
1145 CMATH_TANH_METHODDEF
1146 {NULL, NULL} /* sentinel */
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001147};
1148
Martin v. Löwis1a214512008-06-11 05:26:20 +00001149
1150static struct PyModuleDef cmathmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 PyModuleDef_HEAD_INIT,
1152 "cmath",
1153 module_doc,
1154 -1,
1155 cmath_methods,
1156 NULL,
1157 NULL,
1158 NULL,
1159 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001160};
1161
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001162PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001163PyInit_cmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 PyObject *m;
Tim Peters14e26402001-02-20 20:15:19 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 m = PyModule_Create(&cmathmodule);
1168 if (m == NULL)
1169 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 PyModule_AddObject(m, "pi",
1172 PyFloat_FromDouble(Py_MATH_PI));
1173 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
Christian Heimesa342c012008-04-20 21:01:16 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 /* initialize special value tables */
Christian Heimesa342c012008-04-20 21:01:16 +00001176
1177#define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
1178#define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
1179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 INIT_SPECIAL_VALUES(acos_special_values, {
1181 C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
1182 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1183 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1184 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1185 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1186 C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
1187 C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
1188 })
Christian Heimesa342c012008-04-20 21:01:16 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 INIT_SPECIAL_VALUES(acosh_special_values, {
1191 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1192 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1193 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1194 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1195 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1196 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1197 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1198 })
Christian Heimesa342c012008-04-20 21:01:16 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 INIT_SPECIAL_VALUES(asinh_special_values, {
1201 C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
1202 C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
1203 C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
1204 C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
1205 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1206 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1207 C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
1208 })
Christian Heimesa342c012008-04-20 21:01:16 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 INIT_SPECIAL_VALUES(atanh_special_values, {
1211 C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
1212 C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
1213 C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
1214 C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
1215 C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
1216 C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
1217 C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
1218 })
Christian Heimesa342c012008-04-20 21:01:16 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 INIT_SPECIAL_VALUES(cosh_special_values, {
1221 C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
1222 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1223 C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
1224 C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
1225 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1226 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1227 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1228 })
Christian Heimesa342c012008-04-20 21:01:16 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 INIT_SPECIAL_VALUES(exp_special_values, {
1231 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1232 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1233 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1234 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1235 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1236 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1237 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1238 })
Christian Heimesa342c012008-04-20 21:01:16 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 INIT_SPECIAL_VALUES(log_special_values, {
1241 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1242 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1243 C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
1244 C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
1245 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1246 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1247 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1248 })
Christian Heimesa342c012008-04-20 21:01:16 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 INIT_SPECIAL_VALUES(sinh_special_values, {
1251 C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
1252 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1253 C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
1254 C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
1255 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1256 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1257 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1258 })
Christian Heimesa342c012008-04-20 21:01:16 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 INIT_SPECIAL_VALUES(sqrt_special_values, {
1261 C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
1262 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1263 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1264 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1265 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1266 C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
1267 C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
1268 })
Christian Heimesa342c012008-04-20 21:01:16 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 INIT_SPECIAL_VALUES(tanh_special_values, {
1271 C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
1272 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1273 C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
1274 C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
1275 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1276 C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
1277 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1278 })
Christian Heimesa342c012008-04-20 21:01:16 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 INIT_SPECIAL_VALUES(rect_special_values, {
1281 C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
1282 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1283 C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
1284 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1285 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1286 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1287 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1288 })
1289 return m;
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001290}