blob: 8319767b8a9eef9876d33862460a328694f6ea6f [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]
Brett Cannonb0fc4902014-10-14 17:37:02 -040013module cmath
14[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030015/*[clinic end generated code: output=da39a3ee5e6b4b0d input=308d6839f4a46333]*/
Brett Cannonb0fc4902014-10-14 17:37:02 -040016
17/*[python input]
18class Py_complex_protected_converter(Py_complex_converter):
19 def modify(self):
20 return 'errno = 0; PyFPE_START_PROTECT("complex function", goto exit);'
21
22
23class Py_complex_protected_return_converter(CReturnConverter):
24 type = "Py_complex"
25
26 def render(self, function, data):
27 self.declare(data)
28 data.return_conversion.append("""
29PyFPE_END_PROTECT(_return_value);
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030030if (errno == EDOM) {
Brett Cannonb0fc4902014-10-14 17:37:02 -040031 PyErr_SetString(PyExc_ValueError, "math domain error");
32 goto exit;
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030033}
34else if (errno == ERANGE) {
Brett Cannonb0fc4902014-10-14 17:37:02 -040035 PyErr_SetString(PyExc_OverflowError, "math range error");
36 goto exit;
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030037}
38else {
Brett Cannonb0fc4902014-10-14 17:37:02 -040039 return_value = PyComplex_FromCComplex(_return_value);
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030040}
Brett Cannonb0fc4902014-10-14 17:37:02 -040041""".strip())
42[python start generated code]*/
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030043/*[python end generated code: output=da39a3ee5e6b4b0d input=345daa075b1028e7]*/
Brett Cannonb0fc4902014-10-14 17:37:02 -040044
Christian Heimes53876d92008-04-19 00:31:39 +000045#if (FLT_RADIX != 2 && FLT_RADIX != 16)
46#error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"
Guido van Rossum71aa32f1996-01-12 01:34:57 +000047#endif
48
Christian Heimes53876d92008-04-19 00:31:39 +000049#ifndef M_LN2
50#define M_LN2 (0.6931471805599453094) /* natural log of 2 */
51#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000052
Christian Heimes53876d92008-04-19 00:31:39 +000053#ifndef M_LN10
54#define M_LN10 (2.302585092994045684) /* natural log of 10 */
55#endif
56
57/*
58 CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log,
59 inverse trig and inverse hyperbolic trig functions. Its log is used in the
Ezio Melotti13925002011-03-16 11:05:33 +020060 evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary
Christian Heimes53876d92008-04-19 00:31:39 +000061 overflow.
62 */
63
64#define CM_LARGE_DOUBLE (DBL_MAX/4.)
65#define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE))
66#define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
67#define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
68
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069/*
Christian Heimes53876d92008-04-19 00:31:39 +000070 CM_SCALE_UP is an odd integer chosen such that multiplication by
71 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
72 CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
73 square roots accurately when the real and imaginary parts of the argument
74 are subnormal.
75*/
76
77#if FLT_RADIX==2
78#define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1)
79#elif FLT_RADIX==16
80#define CM_SCALE_UP (4*DBL_MANT_DIG+1)
81#endif
82#define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000083
Mark Dickinson84e63112016-08-29 13:56:58 +010084/* Constants cmath.inf, cmath.infj, cmath.nan, cmath.nanj.
85 cmath.nan and cmath.nanj are defined only when either
86 PY_NO_SHORT_FLOAT_REPR is *not* defined (which should be
87 the most common situation on machines using an IEEE 754
88 representation), or Py_NAN is defined. */
89
90static double
91m_inf(void)
92{
93#ifndef PY_NO_SHORT_FLOAT_REPR
94 return _Py_dg_infinity(0);
95#else
96 return Py_HUGE_VAL;
97#endif
98}
99
100static Py_complex
101c_infj(void)
102{
103 Py_complex r;
104 r.real = 0.0;
105 r.imag = m_inf();
106 return r;
107}
108
109#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
110
111static double
112m_nan(void)
113{
114#ifndef PY_NO_SHORT_FLOAT_REPR
115 return _Py_dg_stdnan(0);
116#else
117 return Py_NAN;
118#endif
119}
120
121static Py_complex
122c_nanj(void)
123{
124 Py_complex r;
125 r.real = 0.0;
126 r.imag = m_nan();
127 return r;
128}
129
130#endif
131
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000132/* forward declarations */
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300133static Py_complex cmath_asinh_impl(PyObject *, Py_complex);
134static Py_complex cmath_atanh_impl(PyObject *, Py_complex);
135static Py_complex cmath_cosh_impl(PyObject *, Py_complex);
136static Py_complex cmath_sinh_impl(PyObject *, Py_complex);
137static Py_complex cmath_sqrt_impl(PyObject *, Py_complex);
138static Py_complex cmath_tanh_impl(PyObject *, Py_complex);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000139static PyObject * math_error(void);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000140
Christian Heimes53876d92008-04-19 00:31:39 +0000141/* Code to deal with special values (infinities, NaNs, etc.). */
142
143/* special_type takes a double and returns an integer code indicating
144 the type of the double as follows:
145*/
146
147enum special_types {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 ST_NINF, /* 0, negative infinity */
149 ST_NEG, /* 1, negative finite number (nonzero) */
150 ST_NZERO, /* 2, -0. */
151 ST_PZERO, /* 3, +0. */
152 ST_POS, /* 4, positive finite number (nonzero) */
153 ST_PINF, /* 5, positive infinity */
154 ST_NAN /* 6, Not a Number */
Christian Heimes53876d92008-04-19 00:31:39 +0000155};
156
157static enum special_types
158special_type(double d)
159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 if (Py_IS_FINITE(d)) {
161 if (d != 0) {
162 if (copysign(1., d) == 1.)
163 return ST_POS;
164 else
165 return ST_NEG;
166 }
167 else {
168 if (copysign(1., d) == 1.)
169 return ST_PZERO;
170 else
171 return ST_NZERO;
172 }
173 }
174 if (Py_IS_NAN(d))
175 return ST_NAN;
176 if (copysign(1., d) == 1.)
177 return ST_PINF;
178 else
179 return ST_NINF;
Christian Heimes53876d92008-04-19 00:31:39 +0000180}
181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182#define SPECIAL_VALUE(z, table) \
183 if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
184 errno = 0; \
185 return table[special_type((z).real)] \
186 [special_type((z).imag)]; \
187 }
Christian Heimes53876d92008-04-19 00:31:39 +0000188
189#define P Py_MATH_PI
190#define P14 0.25*Py_MATH_PI
191#define P12 0.5*Py_MATH_PI
192#define P34 0.75*Py_MATH_PI
Christian Heimesa342c012008-04-20 21:01:16 +0000193#define INF Py_HUGE_VAL
194#define N Py_NAN
Christian Heimes53876d92008-04-19 00:31:39 +0000195#define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
196
197/* First, the C functions that do the real work. Each of the c_*
198 functions computes and returns the C99 Annex G recommended result
199 and also sets errno as follows: errno = 0 if no floating-point
200 exception is associated with the result; errno = EDOM if C99 Annex
201 G recommends raising divide-by-zero or invalid for this result; and
202 errno = ERANGE where the overflow floating-point signal should be
203 raised.
204*/
205
Christian Heimesa342c012008-04-20 21:01:16 +0000206static Py_complex acos_special_values[7][7];
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000207
Brett Cannonb0fc4902014-10-14 17:37:02 -0400208/*[clinic input]
209cmath.acos -> Py_complex_protected
210
211 z: Py_complex_protected
212 /
213
214Return the arc cosine of z.
215[clinic start generated code]*/
216
Tim Peters14e26402001-02-20 20:15:19 +0000217static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300218cmath_acos_impl(PyObject *module, Py_complex z)
219/*[clinic end generated code: output=40bd42853fd460ae input=bd6cbd78ae851927]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 SPECIAL_VALUE(z, acos_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
226 /* avoid unnecessary overflow for large arguments */
227 r.real = atan2(fabs(z.imag), z.real);
228 /* split into cases to make sure that the branch cut has the
229 correct continuity on systems with unsigned zeros */
230 if (z.real < 0.) {
231 r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
232 M_LN2*2., z.imag);
233 } else {
234 r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
235 M_LN2*2., -z.imag);
236 }
237 } else {
238 s1.real = 1.-z.real;
239 s1.imag = -z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400240 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 s2.real = 1.+z.real;
242 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400243 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 r.real = 2.*atan2(s1.real, s2.real);
245 r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real);
246 }
247 errno = 0;
248 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000249}
250
Guido van Rossumc6e22901998-12-04 19:26:43 +0000251
Christian Heimesa342c012008-04-20 21:01:16 +0000252static Py_complex acosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000253
Brett Cannonb0fc4902014-10-14 17:37:02 -0400254/*[clinic input]
255cmath.acosh = cmath.acos
256
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000257Return the inverse hyperbolic cosine of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400258[clinic start generated code]*/
259
Tim Peters14e26402001-02-20 20:15:19 +0000260static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300261cmath_acosh_impl(PyObject *module, Py_complex z)
262/*[clinic end generated code: output=3e2454d4fcf404ca input=3f61bee7d703e53c]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 SPECIAL_VALUE(z, acosh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
269 /* avoid unnecessary overflow for large arguments */
270 r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
271 r.imag = atan2(z.imag, z.real);
272 } else {
273 s1.real = z.real - 1.;
274 s1.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400275 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 s2.real = z.real + 1.;
277 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400278 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag);
280 r.imag = 2.*atan2(s1.imag, s2.real);
281 }
282 errno = 0;
283 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000284}
285
Brett Cannonb0fc4902014-10-14 17:37:02 -0400286/*[clinic input]
287cmath.asin = cmath.acos
Guido van Rossumc6e22901998-12-04 19:26:43 +0000288
Brett Cannonb0fc4902014-10-14 17:37:02 -0400289Return the arc sine of z.
290[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000291
Tim Peters14e26402001-02-20 20:15:19 +0000292static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300293cmath_asin_impl(PyObject *module, Py_complex z)
294/*[clinic end generated code: output=3b264cd1b16bf4e1 input=be0bf0cfdd5239c5]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* asin(z) = -i asinh(iz) */
297 Py_complex s, r;
298 s.real = -z.imag;
299 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400300 s = cmath_asinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 r.real = s.imag;
302 r.imag = -s.real;
303 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000304}
305
Guido van Rossumc6e22901998-12-04 19:26:43 +0000306
Christian Heimesa342c012008-04-20 21:01:16 +0000307static Py_complex asinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000308
Brett Cannonb0fc4902014-10-14 17:37:02 -0400309/*[clinic input]
310cmath.asinh = cmath.acos
311
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000312Return the inverse hyperbolic sine of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400313[clinic start generated code]*/
314
Tim Peters14e26402001-02-20 20:15:19 +0000315static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300316cmath_asinh_impl(PyObject *module, Py_complex z)
317/*[clinic end generated code: output=733d8107841a7599 input=5c09448fcfc89a79]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 SPECIAL_VALUE(z, asinh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
324 if (z.imag >= 0.) {
325 r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
326 M_LN2*2., z.real);
327 } else {
328 r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
329 M_LN2*2., -z.real);
330 }
331 r.imag = atan2(z.imag, fabs(z.real));
332 } else {
333 s1.real = 1.+z.imag;
334 s1.imag = -z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400335 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 s2.real = 1.-z.imag;
337 s2.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400338 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag);
340 r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
341 }
342 errno = 0;
343 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000344}
345
Guido van Rossumc6e22901998-12-04 19:26:43 +0000346
Brett Cannonb0fc4902014-10-14 17:37:02 -0400347/*[clinic input]
348cmath.atan = cmath.acos
349
350Return the arc tangent of z.
351[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000352
Tim Peters14e26402001-02-20 20:15:19 +0000353static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300354cmath_atan_impl(PyObject *module, Py_complex z)
355/*[clinic end generated code: output=b6bfc497058acba4 input=3b21ff7d5eac632a]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 /* atan(z) = -i atanh(iz) */
358 Py_complex s, r;
359 s.real = -z.imag;
360 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400361 s = cmath_atanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 r.real = s.imag;
363 r.imag = -s.real;
364 return r;
Christian Heimes53876d92008-04-19 00:31:39 +0000365}
366
Christian Heimese57950f2008-04-21 13:08:03 +0000367/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
368 C99 for atan2(0., 0.). */
Christian Heimes53876d92008-04-19 00:31:39 +0000369static double
370c_atan2(Py_complex z)
371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
373 return Py_NAN;
374 if (Py_IS_INFINITY(z.imag)) {
375 if (Py_IS_INFINITY(z.real)) {
376 if (copysign(1., z.real) == 1.)
377 /* atan2(+-inf, +inf) == +-pi/4 */
378 return copysign(0.25*Py_MATH_PI, z.imag);
379 else
380 /* atan2(+-inf, -inf) == +-pi*3/4 */
381 return copysign(0.75*Py_MATH_PI, z.imag);
382 }
383 /* atan2(+-inf, x) == +-pi/2 for finite x */
384 return copysign(0.5*Py_MATH_PI, z.imag);
385 }
386 if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
387 if (copysign(1., z.real) == 1.)
388 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
389 return copysign(0., z.imag);
390 else
391 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
392 return copysign(Py_MATH_PI, z.imag);
393 }
394 return atan2(z.imag, z.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000395}
396
Guido van Rossumc6e22901998-12-04 19:26:43 +0000397
Christian Heimesa342c012008-04-20 21:01:16 +0000398static Py_complex atanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000399
Brett Cannonb0fc4902014-10-14 17:37:02 -0400400/*[clinic input]
401cmath.atanh = cmath.acos
402
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000403Return the inverse hyperbolic tangent of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400404[clinic start generated code]*/
405
Tim Peters14e26402001-02-20 20:15:19 +0000406static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300407cmath_atanh_impl(PyObject *module, Py_complex z)
408/*[clinic end generated code: output=e83355f93a989c9e input=2b3fdb82fb34487b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 Py_complex r;
411 double ay, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 SPECIAL_VALUE(z, atanh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
416 if (z.real < 0.) {
Brett Cannonb0fc4902014-10-14 17:37:02 -0400417 return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
Christian Heimes53876d92008-04-19 00:31:39 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 ay = fabs(z.imag);
421 if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
422 /*
423 if abs(z) is large then we use the approximation
424 atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
425 of z.imag)
426 */
427 h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
428 r.real = z.real/4./h/h;
429 /* the two negations in the next line cancel each other out
430 except when working with unsigned zeros: they're there to
431 ensure that the branch cut has the correct continuity on
432 systems that don't support signed zeros */
433 r.imag = -copysign(Py_MATH_PI/2., -z.imag);
434 errno = 0;
435 } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
436 /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
437 if (ay == 0.) {
438 r.real = INF;
439 r.imag = z.imag;
440 errno = EDOM;
441 } else {
442 r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
443 r.imag = copysign(atan2(2., -ay)/2, z.imag);
444 errno = 0;
445 }
446 } else {
447 r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
448 r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
449 errno = 0;
450 }
451 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000452}
453
Guido van Rossumc6e22901998-12-04 19:26:43 +0000454
Brett Cannonb0fc4902014-10-14 17:37:02 -0400455/*[clinic input]
456cmath.cos = cmath.acos
457
458Return the cosine of z.
459[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000460
Tim Peters14e26402001-02-20 20:15:19 +0000461static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300462cmath_cos_impl(PyObject *module, Py_complex z)
463/*[clinic end generated code: output=fd64918d5b3186db input=6022e39b77127ac7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* cos(z) = cosh(iz) */
466 Py_complex r;
467 r.real = -z.imag;
468 r.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400469 r = cmath_cosh_impl(module, r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000471}
472
Guido van Rossumc6e22901998-12-04 19:26:43 +0000473
Christian Heimes53876d92008-04-19 00:31:39 +0000474/* cosh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000475static Py_complex cosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000476
Brett Cannonb0fc4902014-10-14 17:37:02 -0400477/*[clinic input]
478cmath.cosh = cmath.acos
479
480Return the hyperbolic cosine of z.
481[clinic start generated code]*/
482
Tim Peters14e26402001-02-20 20:15:19 +0000483static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300484cmath_cosh_impl(PyObject *module, Py_complex z)
485/*[clinic end generated code: output=2e969047da601bdb input=d6b66339e9cc332b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 Py_complex r;
488 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
491 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
492 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
493 (z.imag != 0.)) {
494 if (z.real > 0) {
495 r.real = copysign(INF, cos(z.imag));
496 r.imag = copysign(INF, sin(z.imag));
497 }
498 else {
499 r.real = copysign(INF, cos(z.imag));
500 r.imag = -copysign(INF, sin(z.imag));
501 }
502 }
503 else {
504 r = cosh_special_values[special_type(z.real)]
505 [special_type(z.imag)];
506 }
507 /* need to set errno = EDOM if y is +/- infinity and x is not
508 a NaN */
509 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
510 errno = EDOM;
511 else
512 errno = 0;
513 return r;
514 }
Christian Heimes53876d92008-04-19 00:31:39 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
517 /* deal correctly with cases where cosh(z.real) overflows but
518 cosh(z) does not. */
519 x_minus_one = z.real - copysign(1., z.real);
520 r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
521 r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
522 } else {
523 r.real = cos(z.imag) * cosh(z.real);
524 r.imag = sin(z.imag) * sinh(z.real);
525 }
526 /* detect overflow, and set errno accordingly */
527 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
528 errno = ERANGE;
529 else
530 errno = 0;
531 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000532}
533
Guido van Rossumc6e22901998-12-04 19:26:43 +0000534
Christian Heimes53876d92008-04-19 00:31:39 +0000535/* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for
536 finite y */
Christian Heimesa342c012008-04-20 21:01:16 +0000537static Py_complex exp_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000538
Brett Cannonb0fc4902014-10-14 17:37:02 -0400539/*[clinic input]
540cmath.exp = cmath.acos
541
542Return the exponential value e**z.
543[clinic start generated code]*/
544
Tim Peters14e26402001-02-20 20:15:19 +0000545static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300546cmath_exp_impl(PyObject *module, Py_complex z)
547/*[clinic end generated code: output=edcec61fb9dfda6c input=8b9e6cf8a92174c3]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_complex r;
550 double l;
Christian Heimes53876d92008-04-19 00:31:39 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
553 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
554 && (z.imag != 0.)) {
555 if (z.real > 0) {
556 r.real = copysign(INF, cos(z.imag));
557 r.imag = copysign(INF, sin(z.imag));
558 }
559 else {
560 r.real = copysign(0., cos(z.imag));
561 r.imag = copysign(0., sin(z.imag));
562 }
563 }
564 else {
565 r = exp_special_values[special_type(z.real)]
566 [special_type(z.imag)];
567 }
568 /* need to set errno = EDOM if y is +/- infinity and x is not
569 a NaN and not -infinity */
570 if (Py_IS_INFINITY(z.imag) &&
571 (Py_IS_FINITE(z.real) ||
572 (Py_IS_INFINITY(z.real) && z.real > 0)))
573 errno = EDOM;
574 else
575 errno = 0;
576 return r;
577 }
Christian Heimes53876d92008-04-19 00:31:39 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (z.real > CM_LOG_LARGE_DOUBLE) {
580 l = exp(z.real-1.);
581 r.real = l*cos(z.imag)*Py_MATH_E;
582 r.imag = l*sin(z.imag)*Py_MATH_E;
583 } else {
584 l = exp(z.real);
585 r.real = l*cos(z.imag);
586 r.imag = l*sin(z.imag);
587 }
588 /* detect overflow, and set errno accordingly */
589 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
590 errno = ERANGE;
591 else
592 errno = 0;
593 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000594}
595
Christian Heimesa342c012008-04-20 21:01:16 +0000596static Py_complex log_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000597
Tim Peters14e26402001-02-20 20:15:19 +0000598static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000599c_log(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /*
602 The usual formula for the real part is log(hypot(z.real, z.imag)).
603 There are four situations where this formula is potentially
604 problematic:
Christian Heimes53876d92008-04-19 00:31:39 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 (1) the absolute value of z is subnormal. Then hypot is subnormal,
607 so has fewer than the usual number of bits of accuracy, hence may
608 have large relative error. This then gives a large absolute error
609 in the log. This can be solved by rescaling z by a suitable power
610 of 2.
Christian Heimes53876d92008-04-19 00:31:39 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 (2) the absolute value of z is greater than DBL_MAX (e.g. when both
613 z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
614 Again, rescaling solves this.
Christian Heimes53876d92008-04-19 00:31:39 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 (3) the absolute value of z is close to 1. In this case it's
617 difficult to achieve good accuracy, at least in part because a
618 change of 1ulp in the real or imaginary part of z can result in a
619 change of billions of ulps in the correctly rounded answer.
Christian Heimes53876d92008-04-19 00:31:39 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 (4) z = 0. The simplest thing to do here is to call the
622 floating-point log with an argument of 0, and let its behaviour
623 (returning -infinity, signaling a floating-point exception, setting
624 errno, or whatever) determine that of c_log. So the usual formula
625 is fine here.
Christian Heimes53876d92008-04-19 00:31:39 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 */
Christian Heimes53876d92008-04-19 00:31:39 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 Py_complex r;
630 double ax, ay, am, an, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 SPECIAL_VALUE(z, log_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 ax = fabs(z.real);
635 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
638 r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
639 } else if (ax < DBL_MIN && ay < DBL_MIN) {
640 if (ax > 0. || ay > 0.) {
641 /* catch cases where hypot(ax, ay) is subnormal */
642 r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
643 ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
644 }
645 else {
646 /* log(+/-0. +/- 0i) */
647 r.real = -INF;
648 r.imag = atan2(z.imag, z.real);
649 errno = EDOM;
650 return r;
651 }
652 } else {
653 h = hypot(ax, ay);
654 if (0.71 <= h && h <= 1.73) {
655 am = ax > ay ? ax : ay; /* max(ax, ay) */
656 an = ax > ay ? ay : ax; /* min(ax, ay) */
657 r.real = m_log1p((am-1)*(am+1)+an*an)/2.;
658 } else {
659 r.real = log(h);
660 }
661 }
662 r.imag = atan2(z.imag, z.real);
663 errno = 0;
664 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000665}
666
Guido van Rossumc6e22901998-12-04 19:26:43 +0000667
Brett Cannonb0fc4902014-10-14 17:37:02 -0400668/*[clinic input]
669cmath.log10 = cmath.acos
670
671Return the base-10 logarithm of z.
672[clinic start generated code]*/
673
Tim Peters14e26402001-02-20 20:15:19 +0000674static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300675cmath_log10_impl(PyObject *module, Py_complex z)
676/*[clinic end generated code: output=2922779a7c38cbe1 input=cff5644f73c1519c]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 Py_complex r;
679 int errno_save;
Christian Heimes53876d92008-04-19 00:31:39 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 r = c_log(z);
682 errno_save = errno; /* just in case the divisions affect errno */
683 r.real = r.real / M_LN10;
684 r.imag = r.imag / M_LN10;
685 errno = errno_save;
686 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000687}
688
Guido van Rossumc6e22901998-12-04 19:26:43 +0000689
Brett Cannonb0fc4902014-10-14 17:37:02 -0400690/*[clinic input]
691cmath.sin = cmath.acos
692
693Return the sine of z.
694[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000695
Tim Peters14e26402001-02-20 20:15:19 +0000696static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300697cmath_sin_impl(PyObject *module, Py_complex z)
698/*[clinic end generated code: output=980370d2ff0bb5aa input=2d3519842a8b4b85]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 /* sin(z) = -i sin(iz) */
701 Py_complex s, r;
702 s.real = -z.imag;
703 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400704 s = cmath_sinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 r.real = s.imag;
706 r.imag = -s.real;
707 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000708}
709
Guido van Rossumc6e22901998-12-04 19:26:43 +0000710
Christian Heimes53876d92008-04-19 00:31:39 +0000711/* sinh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000712static Py_complex sinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000713
Brett Cannonb0fc4902014-10-14 17:37:02 -0400714/*[clinic input]
715cmath.sinh = cmath.acos
716
717Return the hyperbolic sine of z.
718[clinic start generated code]*/
719
Tim Peters14e26402001-02-20 20:15:19 +0000720static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300721cmath_sinh_impl(PyObject *module, Py_complex z)
722/*[clinic end generated code: output=38b0a6cce26f3536 input=d2d3fc8c1ddfd2dd]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 Py_complex r;
725 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* special treatment for sinh(+/-inf + iy) if y is finite and
728 nonzero */
729 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
730 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
731 && (z.imag != 0.)) {
732 if (z.real > 0) {
733 r.real = copysign(INF, cos(z.imag));
734 r.imag = copysign(INF, sin(z.imag));
735 }
736 else {
737 r.real = -copysign(INF, cos(z.imag));
738 r.imag = copysign(INF, sin(z.imag));
739 }
740 }
741 else {
742 r = sinh_special_values[special_type(z.real)]
743 [special_type(z.imag)];
744 }
745 /* need to set errno = EDOM if y is +/- infinity and x is not
746 a NaN */
747 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
748 errno = EDOM;
749 else
750 errno = 0;
751 return r;
752 }
Christian Heimes53876d92008-04-19 00:31:39 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
755 x_minus_one = z.real - copysign(1., z.real);
756 r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
757 r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
758 } else {
759 r.real = cos(z.imag) * sinh(z.real);
760 r.imag = sin(z.imag) * cosh(z.real);
761 }
762 /* detect overflow, and set errno accordingly */
763 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
764 errno = ERANGE;
765 else
766 errno = 0;
767 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000768}
769
Guido van Rossumc6e22901998-12-04 19:26:43 +0000770
Christian Heimesa342c012008-04-20 21:01:16 +0000771static Py_complex sqrt_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000772
Brett Cannonb0fc4902014-10-14 17:37:02 -0400773/*[clinic input]
774cmath.sqrt = cmath.acos
775
776Return the square root of z.
777[clinic start generated code]*/
778
Tim Peters14e26402001-02-20 20:15:19 +0000779static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300780cmath_sqrt_impl(PyObject *module, Py_complex z)
781/*[clinic end generated code: output=b6507b3029c339fc input=7088b166fc9a58c7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /*
784 Method: use symmetries to reduce to the case when x = z.real and y
785 = z.imag are nonnegative. Then the real part of the result is
786 given by
Christian Heimes53876d92008-04-19 00:31:39 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 s = sqrt((x + hypot(x, y))/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 and the imaginary part is
Christian Heimes53876d92008-04-19 00:31:39 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 d = (y/2)/s
Christian Heimes53876d92008-04-19 00:31:39 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 If either x or y is very large then there's a risk of overflow in
795 computation of the expression x + hypot(x, y). We can avoid this
796 by rewriting the formula for s as:
Christian Heimes53876d92008-04-19 00:31:39 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 s = 2*sqrt(x/8 + hypot(x/8, y/8))
Christian Heimes53876d92008-04-19 00:31:39 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 This costs us two extra multiplications/divisions, but avoids the
801 overhead of checking for x and y large.
Christian Heimes53876d92008-04-19 00:31:39 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 If both x and y are subnormal then hypot(x, y) may also be
804 subnormal, so will lack full precision. We solve this by rescaling
805 x and y by a sufficiently large power of 2 to ensure that x and y
806 are normal.
807 */
Christian Heimes53876d92008-04-19 00:31:39 +0000808
809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_complex r;
811 double s,d;
812 double ax, ay;
Christian Heimes53876d92008-04-19 00:31:39 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 SPECIAL_VALUE(z, sqrt_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (z.real == 0. && z.imag == 0.) {
817 r.real = 0.;
818 r.imag = z.imag;
819 return r;
820 }
Christian Heimes53876d92008-04-19 00:31:39 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 ax = fabs(z.real);
823 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
826 /* here we catch cases where hypot(ax, ay) is subnormal */
827 ax = ldexp(ax, CM_SCALE_UP);
828 s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
829 CM_SCALE_DOWN);
830 } else {
831 ax /= 8.;
832 s = 2.*sqrt(ax + hypot(ax, ay/8.));
833 }
834 d = ay/(2.*s);
Christian Heimes53876d92008-04-19 00:31:39 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (z.real >= 0.) {
837 r.real = s;
838 r.imag = copysign(d, z.imag);
839 } else {
840 r.real = d;
841 r.imag = copysign(s, z.imag);
842 }
843 errno = 0;
844 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000845}
846
Guido van Rossumc6e22901998-12-04 19:26:43 +0000847
Brett Cannonb0fc4902014-10-14 17:37:02 -0400848/*[clinic input]
849cmath.tan = cmath.acos
850
851Return the tangent of z.
852[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000853
Tim Peters14e26402001-02-20 20:15:19 +0000854static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300855cmath_tan_impl(PyObject *module, Py_complex z)
856/*[clinic end generated code: output=7c5f13158a72eb13 input=fc167e528767888e]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* tan(z) = -i tanh(iz) */
859 Py_complex s, r;
860 s.real = -z.imag;
861 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400862 s = cmath_tanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 r.real = s.imag;
864 r.imag = -s.real;
865 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000866}
867
Guido van Rossumc6e22901998-12-04 19:26:43 +0000868
Christian Heimes53876d92008-04-19 00:31:39 +0000869/* tanh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000870static Py_complex tanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000871
Brett Cannonb0fc4902014-10-14 17:37:02 -0400872/*[clinic input]
873cmath.tanh = cmath.acos
874
875Return the hyperbolic tangent of z.
876[clinic start generated code]*/
877
Tim Peters14e26402001-02-20 20:15:19 +0000878static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300879cmath_tanh_impl(PyObject *module, Py_complex z)
880/*[clinic end generated code: output=36d547ef7aca116c input=22f67f9dc6d29685]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Formula:
Christian Heimes53876d92008-04-19 00:31:39 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
885 (1+tan(y)^2 tanh(x)^2)
Christian Heimes53876d92008-04-19 00:31:39 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
888 as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
889 by 4 exp(-2*x) instead, to avoid possible overflow in the
890 computation of cosh(x).
Christian Heimes53876d92008-04-19 00:31:39 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 */
Christian Heimes53876d92008-04-19 00:31:39 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 Py_complex r;
895 double tx, ty, cx, txty, denom;
Christian Heimes53876d92008-04-19 00:31:39 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 /* special treatment for tanh(+/-inf + iy) if y is finite and
898 nonzero */
899 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
900 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
901 && (z.imag != 0.)) {
902 if (z.real > 0) {
903 r.real = 1.0;
904 r.imag = copysign(0.,
905 2.*sin(z.imag)*cos(z.imag));
906 }
907 else {
908 r.real = -1.0;
909 r.imag = copysign(0.,
910 2.*sin(z.imag)*cos(z.imag));
911 }
912 }
913 else {
914 r = tanh_special_values[special_type(z.real)]
915 [special_type(z.imag)];
916 }
917 /* need to set errno = EDOM if z.imag is +/-infinity and
918 z.real is finite */
919 if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
920 errno = EDOM;
921 else
922 errno = 0;
923 return r;
924 }
Christian Heimes53876d92008-04-19 00:31:39 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* danger of overflow in 2.*z.imag !*/
927 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
928 r.real = copysign(1., z.real);
929 r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
930 } else {
931 tx = tanh(z.real);
932 ty = tan(z.imag);
933 cx = 1./cosh(z.real);
934 txty = tx*ty;
935 denom = 1. + txty*txty;
936 r.real = tx*(1.+ty*ty)/denom;
937 r.imag = ((ty/denom)*cx)*cx;
938 }
939 errno = 0;
940 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000941}
942
Guido van Rossumc6e22901998-12-04 19:26:43 +0000943
Brett Cannonb0fc4902014-10-14 17:37:02 -0400944/*[clinic input]
945cmath.log
946
947 x: Py_complex
948 y_obj: object = NULL
949 /
950
951The logarithm of z to the given base.
952
953If the base not specified, returns the natural logarithm (base e) of z.
954[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000955
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000956static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300957cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj)
958/*[clinic end generated code: output=4effdb7d258e0d94 input=ee0e823a7c6e68ea]*/
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_complex y;
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 errno = 0;
963 PyFPE_START_PROTECT("complex function", return 0)
964 x = c_log(x);
Brett Cannonb0fc4902014-10-14 17:37:02 -0400965 if (y_obj != NULL) {
966 y = PyComplex_AsCComplex(y_obj);
967 if (PyErr_Occurred()) {
968 return NULL;
969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 y = c_log(y);
Antoine Pitroude08cb62014-07-07 19:08:47 -0400971 x = _Py_c_quot(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 }
973 PyFPE_END_PROTECT(x)
974 if (errno != 0)
975 return math_error();
976 return PyComplex_FromCComplex(x);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000977}
978
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000979
980/* And now the glue to make them available from Python: */
981
Roger E. Masse24070ca1996-12-09 22:59:53 +0000982static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000983math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (errno == EDOM)
986 PyErr_SetString(PyExc_ValueError, "math domain error");
987 else if (errno == ERANGE)
988 PyErr_SetString(PyExc_OverflowError, "math range error");
989 else /* Unexpected math error */
990 PyErr_SetFromErrno(PyExc_ValueError);
991 return NULL;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000992}
993
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000994
Brett Cannonb0fc4902014-10-14 17:37:02 -0400995/*[clinic input]
996cmath.phase
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000997
Brett Cannonb0fc4902014-10-14 17:37:02 -0400998 z: Py_complex
999 /
1000
1001Return argument, also known as the phase angle, of a complex.
1002[clinic start generated code]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001003
Christian Heimes53876d92008-04-19 00:31:39 +00001004static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001005cmath_phase_impl(PyObject *module, Py_complex z)
1006/*[clinic end generated code: output=50725086a7bfd253 input=5cf75228ba94b69d]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 double phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -04001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 errno = 0;
1011 PyFPE_START_PROTECT("arg function", return 0)
1012 phi = c_atan2(z);
1013 PyFPE_END_PROTECT(phi)
1014 if (errno != 0)
1015 return math_error();
1016 else
1017 return PyFloat_FromDouble(phi);
Christian Heimes53876d92008-04-19 00:31:39 +00001018}
1019
Brett Cannonb0fc4902014-10-14 17:37:02 -04001020/*[clinic input]
1021cmath.polar
1022
1023 z: Py_complex
1024 /
1025
1026Convert a complex from rectangular coordinates to polar coordinates.
1027
1028r is the distance from 0 and phi the phase angle.
1029[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001030
1031static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001032cmath_polar_impl(PyObject *module, Py_complex z)
1033/*[clinic end generated code: output=d0a8147c41dbb654 input=26c353574fd1a861]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 double r, phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -04001036
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02001037 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 PyFPE_START_PROTECT("polar function", return 0)
1039 phi = c_atan2(z); /* should not cause any exception */
Antoine Pitroua72f0cd2015-06-23 14:38:13 +02001040 r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyFPE_END_PROTECT(r)
1042 if (errno != 0)
1043 return math_error();
1044 else
1045 return Py_BuildValue("dd", r, phi);
Christian Heimes53876d92008-04-19 00:31:39 +00001046}
1047
Christian Heimes53876d92008-04-19 00:31:39 +00001048/*
1049 rect() isn't covered by the C99 standard, but it's not too hard to
1050 figure out 'spirit of C99' rules for special value handing:
1051
1052 rect(x, t) should behave like exp(log(x) + it) for positive-signed x
1053 rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x
1054 rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0)
1055 gives nan +- i0 with the sign of the imaginary part unspecified.
1056
1057*/
1058
Christian Heimesa342c012008-04-20 21:01:16 +00001059static Py_complex rect_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +00001060
Brett Cannonb0fc4902014-10-14 17:37:02 -04001061/*[clinic input]
1062cmath.rect
1063
1064 r: double
1065 phi: double
1066 /
1067
1068Convert from polar coordinates to rectangular coordinates.
1069[clinic start generated code]*/
1070
Christian Heimes53876d92008-04-19 00:31:39 +00001071static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001072cmath_rect_impl(PyObject *module, double r, double phi)
1073/*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 Py_complex z;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 errno = 0;
1077 PyFPE_START_PROTECT("rect function", return 0)
Christian Heimes53876d92008-04-19 00:31:39 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* deal with special values */
1080 if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
1081 /* if r is +/-infinity and phi is finite but nonzero then
1082 result is (+-INF +-INF i), but we need to compute cos(phi)
1083 and sin(phi) to figure out the signs. */
1084 if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
1085 && (phi != 0.))) {
1086 if (r > 0) {
1087 z.real = copysign(INF, cos(phi));
1088 z.imag = copysign(INF, sin(phi));
1089 }
1090 else {
1091 z.real = -copysign(INF, cos(phi));
1092 z.imag = -copysign(INF, sin(phi));
1093 }
1094 }
1095 else {
1096 z = rect_special_values[special_type(r)]
1097 [special_type(phi)];
1098 }
1099 /* need to set errno = EDOM if r is a nonzero number and phi
1100 is infinite */
1101 if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
1102 errno = EDOM;
1103 else
1104 errno = 0;
1105 }
Mark Dickinson58ceecf2013-07-20 17:59:13 +01001106 else if (phi == 0.0) {
1107 /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See
1108 bugs.python.org/issue18513. */
1109 z.real = r;
1110 z.imag = r * phi;
1111 errno = 0;
1112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 else {
1114 z.real = r * cos(phi);
1115 z.imag = r * sin(phi);
1116 errno = 0;
1117 }
Christian Heimes53876d92008-04-19 00:31:39 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyFPE_END_PROTECT(z)
1120 if (errno != 0)
1121 return math_error();
1122 else
1123 return PyComplex_FromCComplex(z);
Christian Heimes53876d92008-04-19 00:31:39 +00001124}
1125
Brett Cannonb0fc4902014-10-14 17:37:02 -04001126/*[clinic input]
1127cmath.isfinite = cmath.polar
1128
1129Return True if both the real and imaginary parts of z are finite, else False.
1130[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001131
1132static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001133cmath_isfinite_impl(PyObject *module, Py_complex z)
1134/*[clinic end generated code: output=ac76611e2c774a36 input=848e7ee701895815]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001135{
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001136 return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
1137}
1138
Brett Cannonb0fc4902014-10-14 17:37:02 -04001139/*[clinic input]
1140cmath.isnan = cmath.polar
1141
1142Checks if the real or imaginary part of z not a number (NaN).
1143[clinic start generated code]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001144
1145static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001146cmath_isnan_impl(PyObject *module, Py_complex z)
1147/*[clinic end generated code: output=e7abf6e0b28beab7 input=71799f5d284c9baf]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001150}
1151
Brett Cannonb0fc4902014-10-14 17:37:02 -04001152/*[clinic input]
1153cmath.isinf = cmath.polar
1154
1155Checks if the real or imaginary part of z is infinite.
1156[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001157
1158static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001159cmath_isinf_impl(PyObject *module, Py_complex z)
1160/*[clinic end generated code: output=502a75a79c773469 input=363df155c7181329]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
1163 Py_IS_INFINITY(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001164}
1165
Tal Einatd5519ed2015-05-31 22:05:00 +03001166/*[clinic input]
1167cmath.isclose -> bool
1168
1169 a: Py_complex
1170 b: Py_complex
1171 *
1172 rel_tol: double = 1e-09
1173 maximum difference for being considered "close", relative to the
1174 magnitude of the input values
1175 abs_tol: double = 0.0
1176 maximum difference for being considered "close", regardless of the
1177 magnitude of the input values
1178
1179Determine whether two complex numbers are close in value.
1180
1181Return True if a is close in value to b, and False otherwise.
1182
1183For the values to be considered close, the difference between them must be
1184smaller than at least one of the tolerances.
1185
1186-inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is
1187not close to anything, even itself. inf and -inf are only close to themselves.
1188[clinic start generated code]*/
1189
1190static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001191cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b,
Tal Einatd5519ed2015-05-31 22:05:00 +03001192 double rel_tol, double abs_tol)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001193/*[clinic end generated code: output=8a2486cc6e0014d1 input=df9636d7de1d4ac3]*/
Tal Einatd5519ed2015-05-31 22:05:00 +03001194{
1195 double diff;
1196
1197 /* sanity check on the inputs */
1198 if (rel_tol < 0.0 || abs_tol < 0.0 ) {
1199 PyErr_SetString(PyExc_ValueError,
1200 "tolerances must be non-negative");
1201 return -1;
1202 }
1203
1204 if ( (a.real == b.real) && (a.imag == b.imag) ) {
1205 /* short circuit exact equality -- needed to catch two infinities of
1206 the same sign. And perhaps speeds things up a bit sometimes.
1207 */
1208 return 1;
1209 }
1210
1211 /* This catches the case of two infinities of opposite sign, or
1212 one infinity and one finite number. Two infinities of opposite
1213 sign would otherwise have an infinite relative tolerance.
1214 Two infinities of the same sign are caught by the equality check
1215 above.
1216 */
1217
1218 if (Py_IS_INFINITY(a.real) || Py_IS_INFINITY(a.imag) ||
1219 Py_IS_INFINITY(b.real) || Py_IS_INFINITY(b.imag)) {
1220 return 0;
1221 }
1222
1223 /* now do the regular computation
1224 this is essentially the "weak" test from the Boost library
1225 */
1226
1227 diff = _Py_c_abs(_Py_c_diff(a, b));
1228
1229 return (((diff <= rel_tol * _Py_c_abs(b)) ||
1230 (diff <= rel_tol * _Py_c_abs(a))) ||
1231 (diff <= abs_tol));
1232}
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001234PyDoc_STRVAR(module_doc,
Tim Peters14e26402001-02-20 20:15:19 +00001235"This module is always available. It provides access to mathematical\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001236"functions for complex numbers.");
Guido van Rossumc6e22901998-12-04 19:26:43 +00001237
Roger E. Masse24070ca1996-12-09 22:59:53 +00001238static PyMethodDef cmath_methods[] = {
Brett Cannonb0fc4902014-10-14 17:37:02 -04001239 CMATH_ACOS_METHODDEF
1240 CMATH_ACOSH_METHODDEF
1241 CMATH_ASIN_METHODDEF
1242 CMATH_ASINH_METHODDEF
1243 CMATH_ATAN_METHODDEF
1244 CMATH_ATANH_METHODDEF
1245 CMATH_COS_METHODDEF
1246 CMATH_COSH_METHODDEF
1247 CMATH_EXP_METHODDEF
Tal Einatd5519ed2015-05-31 22:05:00 +03001248 CMATH_ISCLOSE_METHODDEF
Brett Cannonb0fc4902014-10-14 17:37:02 -04001249 CMATH_ISFINITE_METHODDEF
1250 CMATH_ISINF_METHODDEF
1251 CMATH_ISNAN_METHODDEF
1252 CMATH_LOG_METHODDEF
1253 CMATH_LOG10_METHODDEF
1254 CMATH_PHASE_METHODDEF
1255 CMATH_POLAR_METHODDEF
1256 CMATH_RECT_METHODDEF
1257 CMATH_SIN_METHODDEF
1258 CMATH_SINH_METHODDEF
1259 CMATH_SQRT_METHODDEF
1260 CMATH_TAN_METHODDEF
1261 CMATH_TANH_METHODDEF
1262 {NULL, NULL} /* sentinel */
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001263};
1264
Martin v. Löwis1a214512008-06-11 05:26:20 +00001265
1266static struct PyModuleDef cmathmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 PyModuleDef_HEAD_INIT,
1268 "cmath",
1269 module_doc,
1270 -1,
1271 cmath_methods,
1272 NULL,
1273 NULL,
1274 NULL,
1275 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001276};
1277
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001278PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001279PyInit_cmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 PyObject *m;
Tim Peters14e26402001-02-20 20:15:19 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 m = PyModule_Create(&cmathmodule);
1284 if (m == NULL)
1285 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyModule_AddObject(m, "pi",
1288 PyFloat_FromDouble(Py_MATH_PI));
1289 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
Guido van Rossum0a891d72016-08-15 09:12:52 -07001290 PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
Mark Dickinson84e63112016-08-29 13:56:58 +01001291 PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
1292 PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj()));
1293#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
1294 PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
1295 PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj()));
1296#endif
Christian Heimesa342c012008-04-20 21:01:16 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* initialize special value tables */
Christian Heimesa342c012008-04-20 21:01:16 +00001299
1300#define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
1301#define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
1302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 INIT_SPECIAL_VALUES(acos_special_values, {
1304 C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
1305 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1306 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1307 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1308 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1309 C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
1310 C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
1311 })
Christian Heimesa342c012008-04-20 21:01:16 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 INIT_SPECIAL_VALUES(acosh_special_values, {
1314 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1315 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1316 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1317 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1318 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1319 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1320 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1321 })
Christian Heimesa342c012008-04-20 21:01:16 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 INIT_SPECIAL_VALUES(asinh_special_values, {
1324 C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
1325 C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
1326 C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
1327 C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
1328 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1329 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1330 C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
1331 })
Christian Heimesa342c012008-04-20 21:01:16 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 INIT_SPECIAL_VALUES(atanh_special_values, {
1334 C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
1335 C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
1336 C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
1337 C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
1338 C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
1339 C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
1340 C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
1341 })
Christian Heimesa342c012008-04-20 21:01:16 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 INIT_SPECIAL_VALUES(cosh_special_values, {
1344 C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
1345 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1346 C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
1347 C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
1348 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1349 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1350 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1351 })
Christian Heimesa342c012008-04-20 21:01:16 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 INIT_SPECIAL_VALUES(exp_special_values, {
1354 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1355 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1356 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1357 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1358 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1359 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1360 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1361 })
Christian Heimesa342c012008-04-20 21:01:16 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 INIT_SPECIAL_VALUES(log_special_values, {
1364 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1365 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1366 C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
1367 C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
1368 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1369 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1370 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1371 })
Christian Heimesa342c012008-04-20 21:01:16 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 INIT_SPECIAL_VALUES(sinh_special_values, {
1374 C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
1375 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1376 C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
1377 C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
1378 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1379 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1380 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1381 })
Christian Heimesa342c012008-04-20 21:01:16 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 INIT_SPECIAL_VALUES(sqrt_special_values, {
1384 C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
1385 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1386 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1387 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1388 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1389 C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
1390 C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
1391 })
Christian Heimesa342c012008-04-20 21:01:16 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 INIT_SPECIAL_VALUES(tanh_special_values, {
1394 C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
1395 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1396 C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
1397 C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
1398 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1399 C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
1400 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1401 })
Christian Heimesa342c012008-04-20 21:01:16 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 INIT_SPECIAL_VALUES(rect_special_values, {
1404 C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
1405 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1406 C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
1407 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1408 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1409 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1410 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1411 })
1412 return m;
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001413}