blob: 8b21decfa53fcc6088c421a6f0d0181c7624e5d0 [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):
Victor Stinnerbe143ec2019-11-20 02:51:30 +010020 return 'errno = 0;'
Brett Cannonb0fc4902014-10-14 17:37:02 -040021
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("""
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030029if (errno == EDOM) {
Brett Cannonb0fc4902014-10-14 17:37:02 -040030 PyErr_SetString(PyExc_ValueError, "math domain error");
31 goto exit;
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030032}
33else if (errno == ERANGE) {
Brett Cannonb0fc4902014-10-14 17:37:02 -040034 PyErr_SetString(PyExc_OverflowError, "math range error");
35 goto exit;
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030036}
37else {
Brett Cannonb0fc4902014-10-14 17:37:02 -040038 return_value = PyComplex_FromCComplex(_return_value);
Serhiy Storchakaebe95fd2016-06-09 16:02:15 +030039}
Brett Cannonb0fc4902014-10-14 17:37:02 -040040""".strip())
41[python start generated code]*/
Victor Stinnerbe143ec2019-11-20 02:51:30 +010042/*[python end generated code: output=da39a3ee5e6b4b0d input=8b27adb674c08321]*/
Brett Cannonb0fc4902014-10-14 17:37:02 -040043
Christian Heimes53876d92008-04-19 00:31:39 +000044#if (FLT_RADIX != 2 && FLT_RADIX != 16)
45#error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"
Guido van Rossum71aa32f1996-01-12 01:34:57 +000046#endif
47
Christian Heimes53876d92008-04-19 00:31:39 +000048#ifndef M_LN2
49#define M_LN2 (0.6931471805599453094) /* natural log of 2 */
50#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000051
Christian Heimes53876d92008-04-19 00:31:39 +000052#ifndef M_LN10
53#define M_LN10 (2.302585092994045684) /* natural log of 10 */
54#endif
55
56/*
57 CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log,
58 inverse trig and inverse hyperbolic trig functions. Its log is used in the
Ezio Melotti13925002011-03-16 11:05:33 +020059 evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary
Christian Heimes53876d92008-04-19 00:31:39 +000060 overflow.
61 */
62
63#define CM_LARGE_DOUBLE (DBL_MAX/4.)
64#define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE))
65#define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
66#define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
67
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068/*
Christian Heimes53876d92008-04-19 00:31:39 +000069 CM_SCALE_UP is an odd integer chosen such that multiplication by
70 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
71 CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
72 square roots accurately when the real and imaginary parts of the argument
73 are subnormal.
74*/
75
76#if FLT_RADIX==2
77#define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1)
78#elif FLT_RADIX==16
79#define CM_SCALE_UP (4*DBL_MANT_DIG+1)
80#endif
81#define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000082
Mark Dickinson84e63112016-08-29 13:56:58 +010083/* Constants cmath.inf, cmath.infj, cmath.nan, cmath.nanj.
84 cmath.nan and cmath.nanj are defined only when either
85 PY_NO_SHORT_FLOAT_REPR is *not* defined (which should be
86 the most common situation on machines using an IEEE 754
87 representation), or Py_NAN is defined. */
88
89static double
90m_inf(void)
91{
92#ifndef PY_NO_SHORT_FLOAT_REPR
93 return _Py_dg_infinity(0);
94#else
95 return Py_HUGE_VAL;
96#endif
97}
98
99static Py_complex
100c_infj(void)
101{
102 Py_complex r;
103 r.real = 0.0;
104 r.imag = m_inf();
105 return r;
106}
107
108#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
109
110static double
111m_nan(void)
112{
113#ifndef PY_NO_SHORT_FLOAT_REPR
114 return _Py_dg_stdnan(0);
115#else
116 return Py_NAN;
117#endif
118}
119
120static Py_complex
121c_nanj(void)
122{
123 Py_complex r;
124 r.real = 0.0;
125 r.imag = m_nan();
126 return r;
127}
128
129#endif
130
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000131/* forward declarations */
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300132static Py_complex cmath_asinh_impl(PyObject *, Py_complex);
133static Py_complex cmath_atanh_impl(PyObject *, Py_complex);
134static Py_complex cmath_cosh_impl(PyObject *, Py_complex);
135static Py_complex cmath_sinh_impl(PyObject *, Py_complex);
136static Py_complex cmath_sqrt_impl(PyObject *, Py_complex);
137static Py_complex cmath_tanh_impl(PyObject *, Py_complex);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000138static PyObject * math_error(void);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000139
Christian Heimes53876d92008-04-19 00:31:39 +0000140/* Code to deal with special values (infinities, NaNs, etc.). */
141
142/* special_type takes a double and returns an integer code indicating
143 the type of the double as follows:
144*/
145
146enum special_types {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 ST_NINF, /* 0, negative infinity */
148 ST_NEG, /* 1, negative finite number (nonzero) */
149 ST_NZERO, /* 2, -0. */
150 ST_PZERO, /* 3, +0. */
151 ST_POS, /* 4, positive finite number (nonzero) */
152 ST_PINF, /* 5, positive infinity */
153 ST_NAN /* 6, Not a Number */
Christian Heimes53876d92008-04-19 00:31:39 +0000154};
155
156static enum special_types
157special_type(double d)
158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 if (Py_IS_FINITE(d)) {
160 if (d != 0) {
161 if (copysign(1., d) == 1.)
162 return ST_POS;
163 else
164 return ST_NEG;
165 }
166 else {
167 if (copysign(1., d) == 1.)
168 return ST_PZERO;
169 else
170 return ST_NZERO;
171 }
172 }
173 if (Py_IS_NAN(d))
174 return ST_NAN;
175 if (copysign(1., d) == 1.)
176 return ST_PINF;
177 else
178 return ST_NINF;
Christian Heimes53876d92008-04-19 00:31:39 +0000179}
180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181#define SPECIAL_VALUE(z, table) \
182 if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
183 errno = 0; \
184 return table[special_type((z).real)] \
185 [special_type((z).imag)]; \
186 }
Christian Heimes53876d92008-04-19 00:31:39 +0000187
188#define P Py_MATH_PI
189#define P14 0.25*Py_MATH_PI
190#define P12 0.5*Py_MATH_PI
191#define P34 0.75*Py_MATH_PI
Christian Heimesa342c012008-04-20 21:01:16 +0000192#define INF Py_HUGE_VAL
193#define N Py_NAN
Christian Heimes53876d92008-04-19 00:31:39 +0000194#define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
195
196/* First, the C functions that do the real work. Each of the c_*
197 functions computes and returns the C99 Annex G recommended result
198 and also sets errno as follows: errno = 0 if no floating-point
199 exception is associated with the result; errno = EDOM if C99 Annex
200 G recommends raising divide-by-zero or invalid for this result; and
201 errno = ERANGE where the overflow floating-point signal should be
202 raised.
203*/
204
Christian Heimesa342c012008-04-20 21:01:16 +0000205static Py_complex acos_special_values[7][7];
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000206
Brett Cannonb0fc4902014-10-14 17:37:02 -0400207/*[clinic input]
208cmath.acos -> Py_complex_protected
209
210 z: Py_complex_protected
211 /
212
213Return the arc cosine of z.
214[clinic start generated code]*/
215
Tim Peters14e26402001-02-20 20:15:19 +0000216static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300217cmath_acos_impl(PyObject *module, Py_complex z)
218/*[clinic end generated code: output=40bd42853fd460ae input=bd6cbd78ae851927]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 SPECIAL_VALUE(z, acos_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
225 /* avoid unnecessary overflow for large arguments */
226 r.real = atan2(fabs(z.imag), z.real);
227 /* split into cases to make sure that the branch cut has the
228 correct continuity on systems with unsigned zeros */
229 if (z.real < 0.) {
230 r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
231 M_LN2*2., z.imag);
232 } else {
233 r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
234 M_LN2*2., -z.imag);
235 }
236 } else {
237 s1.real = 1.-z.real;
238 s1.imag = -z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400239 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 s2.real = 1.+z.real;
241 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400242 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 r.real = 2.*atan2(s1.real, s2.real);
244 r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real);
245 }
246 errno = 0;
247 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000248}
249
Guido van Rossumc6e22901998-12-04 19:26:43 +0000250
Christian Heimesa342c012008-04-20 21:01:16 +0000251static Py_complex acosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000252
Brett Cannonb0fc4902014-10-14 17:37:02 -0400253/*[clinic input]
254cmath.acosh = cmath.acos
255
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000256Return the inverse hyperbolic cosine of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400257[clinic start generated code]*/
258
Tim Peters14e26402001-02-20 20:15:19 +0000259static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300260cmath_acosh_impl(PyObject *module, Py_complex z)
261/*[clinic end generated code: output=3e2454d4fcf404ca input=3f61bee7d703e53c]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 SPECIAL_VALUE(z, acosh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
268 /* avoid unnecessary overflow for large arguments */
269 r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
270 r.imag = atan2(z.imag, z.real);
271 } else {
272 s1.real = z.real - 1.;
273 s1.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400274 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 s2.real = z.real + 1.;
276 s2.imag = z.imag;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400277 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag);
279 r.imag = 2.*atan2(s1.imag, s2.real);
280 }
281 errno = 0;
282 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000283}
284
Brett Cannonb0fc4902014-10-14 17:37:02 -0400285/*[clinic input]
286cmath.asin = cmath.acos
Guido van Rossumc6e22901998-12-04 19:26:43 +0000287
Brett Cannonb0fc4902014-10-14 17:37:02 -0400288Return the arc sine of z.
289[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000290
Tim Peters14e26402001-02-20 20:15:19 +0000291static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300292cmath_asin_impl(PyObject *module, Py_complex z)
293/*[clinic end generated code: output=3b264cd1b16bf4e1 input=be0bf0cfdd5239c5]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* asin(z) = -i asinh(iz) */
296 Py_complex s, r;
297 s.real = -z.imag;
298 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400299 s = cmath_asinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 r.real = s.imag;
301 r.imag = -s.real;
302 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000303}
304
Guido van Rossumc6e22901998-12-04 19:26:43 +0000305
Christian Heimesa342c012008-04-20 21:01:16 +0000306static Py_complex asinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000307
Brett Cannonb0fc4902014-10-14 17:37:02 -0400308/*[clinic input]
309cmath.asinh = cmath.acos
310
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000311Return the inverse hyperbolic sine of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400312[clinic start generated code]*/
313
Tim Peters14e26402001-02-20 20:15:19 +0000314static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300315cmath_asinh_impl(PyObject *module, Py_complex z)
316/*[clinic end generated code: output=733d8107841a7599 input=5c09448fcfc89a79]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 Py_complex s1, s2, r;
Christian Heimes53876d92008-04-19 00:31:39 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 SPECIAL_VALUE(z, asinh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
323 if (z.imag >= 0.) {
324 r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
325 M_LN2*2., z.real);
326 } else {
327 r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
328 M_LN2*2., -z.real);
329 }
330 r.imag = atan2(z.imag, fabs(z.real));
331 } else {
332 s1.real = 1.+z.imag;
333 s1.imag = -z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400334 s1 = cmath_sqrt_impl(module, s1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 s2.real = 1.-z.imag;
336 s2.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400337 s2 = cmath_sqrt_impl(module, s2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag);
339 r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
340 }
341 errno = 0;
342 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000343}
344
Guido van Rossumc6e22901998-12-04 19:26:43 +0000345
Brett Cannonb0fc4902014-10-14 17:37:02 -0400346/*[clinic input]
347cmath.atan = cmath.acos
348
349Return the arc tangent of z.
350[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000351
Tim Peters14e26402001-02-20 20:15:19 +0000352static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300353cmath_atan_impl(PyObject *module, Py_complex z)
354/*[clinic end generated code: output=b6bfc497058acba4 input=3b21ff7d5eac632a]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 /* atan(z) = -i atanh(iz) */
357 Py_complex s, r;
358 s.real = -z.imag;
359 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400360 s = cmath_atanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 r.real = s.imag;
362 r.imag = -s.real;
363 return r;
Christian Heimes53876d92008-04-19 00:31:39 +0000364}
365
Christian Heimese57950f2008-04-21 13:08:03 +0000366/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
367 C99 for atan2(0., 0.). */
Christian Heimes53876d92008-04-19 00:31:39 +0000368static double
369c_atan2(Py_complex z)
370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
372 return Py_NAN;
373 if (Py_IS_INFINITY(z.imag)) {
374 if (Py_IS_INFINITY(z.real)) {
375 if (copysign(1., z.real) == 1.)
376 /* atan2(+-inf, +inf) == +-pi/4 */
377 return copysign(0.25*Py_MATH_PI, z.imag);
378 else
379 /* atan2(+-inf, -inf) == +-pi*3/4 */
380 return copysign(0.75*Py_MATH_PI, z.imag);
381 }
382 /* atan2(+-inf, x) == +-pi/2 for finite x */
383 return copysign(0.5*Py_MATH_PI, z.imag);
384 }
385 if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
386 if (copysign(1., z.real) == 1.)
387 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
388 return copysign(0., z.imag);
389 else
390 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
391 return copysign(Py_MATH_PI, z.imag);
392 }
393 return atan2(z.imag, z.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000394}
395
Guido van Rossumc6e22901998-12-04 19:26:43 +0000396
Christian Heimesa342c012008-04-20 21:01:16 +0000397static Py_complex atanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000398
Brett Cannonb0fc4902014-10-14 17:37:02 -0400399/*[clinic input]
400cmath.atanh = cmath.acos
401
Mark Dickinsoncc8617b2015-01-11 13:22:44 +0000402Return the inverse hyperbolic tangent of z.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400403[clinic start generated code]*/
404
Tim Peters14e26402001-02-20 20:15:19 +0000405static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300406cmath_atanh_impl(PyObject *module, Py_complex z)
407/*[clinic end generated code: output=e83355f93a989c9e input=2b3fdb82fb34487b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 Py_complex r;
410 double ay, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 SPECIAL_VALUE(z, atanh_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
415 if (z.real < 0.) {
Brett Cannonb0fc4902014-10-14 17:37:02 -0400416 return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
Christian Heimes53876d92008-04-19 00:31:39 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 ay = fabs(z.imag);
420 if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
421 /*
422 if abs(z) is large then we use the approximation
423 atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
424 of z.imag)
425 */
426 h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
427 r.real = z.real/4./h/h;
428 /* the two negations in the next line cancel each other out
429 except when working with unsigned zeros: they're there to
430 ensure that the branch cut has the correct continuity on
431 systems that don't support signed zeros */
432 r.imag = -copysign(Py_MATH_PI/2., -z.imag);
433 errno = 0;
434 } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
435 /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
436 if (ay == 0.) {
437 r.real = INF;
438 r.imag = z.imag;
439 errno = EDOM;
440 } else {
441 r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
442 r.imag = copysign(atan2(2., -ay)/2, z.imag);
443 errno = 0;
444 }
445 } else {
446 r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
447 r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
448 errno = 0;
449 }
450 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000451}
452
Guido van Rossumc6e22901998-12-04 19:26:43 +0000453
Brett Cannonb0fc4902014-10-14 17:37:02 -0400454/*[clinic input]
455cmath.cos = cmath.acos
456
457Return the cosine of z.
458[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000459
Tim Peters14e26402001-02-20 20:15:19 +0000460static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300461cmath_cos_impl(PyObject *module, Py_complex z)
462/*[clinic end generated code: output=fd64918d5b3186db input=6022e39b77127ac7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 /* cos(z) = cosh(iz) */
465 Py_complex r;
466 r.real = -z.imag;
467 r.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400468 r = cmath_cosh_impl(module, r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000470}
471
Guido van Rossumc6e22901998-12-04 19:26:43 +0000472
Christian Heimes53876d92008-04-19 00:31:39 +0000473/* cosh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000474static Py_complex cosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000475
Brett Cannonb0fc4902014-10-14 17:37:02 -0400476/*[clinic input]
477cmath.cosh = cmath.acos
478
479Return the hyperbolic cosine of z.
480[clinic start generated code]*/
481
Tim Peters14e26402001-02-20 20:15:19 +0000482static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300483cmath_cosh_impl(PyObject *module, Py_complex z)
484/*[clinic end generated code: output=2e969047da601bdb input=d6b66339e9cc332b]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_complex r;
487 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
490 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
491 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
492 (z.imag != 0.)) {
493 if (z.real > 0) {
494 r.real = copysign(INF, cos(z.imag));
495 r.imag = copysign(INF, sin(z.imag));
496 }
497 else {
498 r.real = copysign(INF, cos(z.imag));
499 r.imag = -copysign(INF, sin(z.imag));
500 }
501 }
502 else {
503 r = cosh_special_values[special_type(z.real)]
504 [special_type(z.imag)];
505 }
506 /* need to set errno = EDOM if y is +/- infinity and x is not
507 a NaN */
508 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
509 errno = EDOM;
510 else
511 errno = 0;
512 return r;
513 }
Christian Heimes53876d92008-04-19 00:31:39 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
516 /* deal correctly with cases where cosh(z.real) overflows but
517 cosh(z) does not. */
518 x_minus_one = z.real - copysign(1., z.real);
519 r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
520 r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
521 } else {
522 r.real = cos(z.imag) * cosh(z.real);
523 r.imag = sin(z.imag) * sinh(z.real);
524 }
525 /* detect overflow, and set errno accordingly */
526 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
527 errno = ERANGE;
528 else
529 errno = 0;
530 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000531}
532
Guido van Rossumc6e22901998-12-04 19:26:43 +0000533
Christian Heimes53876d92008-04-19 00:31:39 +0000534/* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for
535 finite y */
Christian Heimesa342c012008-04-20 21:01:16 +0000536static Py_complex exp_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000537
Brett Cannonb0fc4902014-10-14 17:37:02 -0400538/*[clinic input]
539cmath.exp = cmath.acos
540
541Return the exponential value e**z.
542[clinic start generated code]*/
543
Tim Peters14e26402001-02-20 20:15:19 +0000544static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300545cmath_exp_impl(PyObject *module, Py_complex z)
546/*[clinic end generated code: output=edcec61fb9dfda6c input=8b9e6cf8a92174c3]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_complex r;
549 double l;
Christian Heimes53876d92008-04-19 00:31:39 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
552 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
553 && (z.imag != 0.)) {
554 if (z.real > 0) {
555 r.real = copysign(INF, cos(z.imag));
556 r.imag = copysign(INF, sin(z.imag));
557 }
558 else {
559 r.real = copysign(0., cos(z.imag));
560 r.imag = copysign(0., sin(z.imag));
561 }
562 }
563 else {
564 r = exp_special_values[special_type(z.real)]
565 [special_type(z.imag)];
566 }
567 /* need to set errno = EDOM if y is +/- infinity and x is not
568 a NaN and not -infinity */
569 if (Py_IS_INFINITY(z.imag) &&
570 (Py_IS_FINITE(z.real) ||
571 (Py_IS_INFINITY(z.real) && z.real > 0)))
572 errno = EDOM;
573 else
574 errno = 0;
575 return r;
576 }
Christian Heimes53876d92008-04-19 00:31:39 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (z.real > CM_LOG_LARGE_DOUBLE) {
579 l = exp(z.real-1.);
580 r.real = l*cos(z.imag)*Py_MATH_E;
581 r.imag = l*sin(z.imag)*Py_MATH_E;
582 } else {
583 l = exp(z.real);
584 r.real = l*cos(z.imag);
585 r.imag = l*sin(z.imag);
586 }
587 /* detect overflow, and set errno accordingly */
588 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
589 errno = ERANGE;
590 else
591 errno = 0;
592 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000593}
594
Christian Heimesa342c012008-04-20 21:01:16 +0000595static Py_complex log_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000596
Tim Peters14e26402001-02-20 20:15:19 +0000597static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000598c_log(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /*
601 The usual formula for the real part is log(hypot(z.real, z.imag)).
602 There are four situations where this formula is potentially
603 problematic:
Christian Heimes53876d92008-04-19 00:31:39 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 (1) the absolute value of z is subnormal. Then hypot is subnormal,
606 so has fewer than the usual number of bits of accuracy, hence may
607 have large relative error. This then gives a large absolute error
608 in the log. This can be solved by rescaling z by a suitable power
609 of 2.
Christian Heimes53876d92008-04-19 00:31:39 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 (2) the absolute value of z is greater than DBL_MAX (e.g. when both
612 z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
613 Again, rescaling solves this.
Christian Heimes53876d92008-04-19 00:31:39 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 (3) the absolute value of z is close to 1. In this case it's
616 difficult to achieve good accuracy, at least in part because a
617 change of 1ulp in the real or imaginary part of z can result in a
618 change of billions of ulps in the correctly rounded answer.
Christian Heimes53876d92008-04-19 00:31:39 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 (4) z = 0. The simplest thing to do here is to call the
621 floating-point log with an argument of 0, and let its behaviour
622 (returning -infinity, signaling a floating-point exception, setting
623 errno, or whatever) determine that of c_log. So the usual formula
624 is fine here.
Christian Heimes53876d92008-04-19 00:31:39 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 */
Christian Heimes53876d92008-04-19 00:31:39 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 Py_complex r;
629 double ax, ay, am, an, h;
Christian Heimes53876d92008-04-19 00:31:39 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 SPECIAL_VALUE(z, log_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 ax = fabs(z.real);
634 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
637 r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
638 } else if (ax < DBL_MIN && ay < DBL_MIN) {
639 if (ax > 0. || ay > 0.) {
640 /* catch cases where hypot(ax, ay) is subnormal */
641 r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
642 ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
643 }
644 else {
645 /* log(+/-0. +/- 0i) */
646 r.real = -INF;
647 r.imag = atan2(z.imag, z.real);
648 errno = EDOM;
649 return r;
650 }
651 } else {
652 h = hypot(ax, ay);
653 if (0.71 <= h && h <= 1.73) {
654 am = ax > ay ? ax : ay; /* max(ax, ay) */
655 an = ax > ay ? ay : ax; /* min(ax, ay) */
656 r.real = m_log1p((am-1)*(am+1)+an*an)/2.;
657 } else {
658 r.real = log(h);
659 }
660 }
661 r.imag = atan2(z.imag, z.real);
662 errno = 0;
663 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000664}
665
Guido van Rossumc6e22901998-12-04 19:26:43 +0000666
Brett Cannonb0fc4902014-10-14 17:37:02 -0400667/*[clinic input]
668cmath.log10 = cmath.acos
669
670Return the base-10 logarithm of z.
671[clinic start generated code]*/
672
Tim Peters14e26402001-02-20 20:15:19 +0000673static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300674cmath_log10_impl(PyObject *module, Py_complex z)
675/*[clinic end generated code: output=2922779a7c38cbe1 input=cff5644f73c1519c]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_complex r;
678 int errno_save;
Christian Heimes53876d92008-04-19 00:31:39 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 r = c_log(z);
681 errno_save = errno; /* just in case the divisions affect errno */
682 r.real = r.real / M_LN10;
683 r.imag = r.imag / M_LN10;
684 errno = errno_save;
685 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000686}
687
Guido van Rossumc6e22901998-12-04 19:26:43 +0000688
Brett Cannonb0fc4902014-10-14 17:37:02 -0400689/*[clinic input]
690cmath.sin = cmath.acos
691
692Return the sine of z.
693[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000694
Tim Peters14e26402001-02-20 20:15:19 +0000695static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300696cmath_sin_impl(PyObject *module, Py_complex z)
697/*[clinic end generated code: output=980370d2ff0bb5aa input=2d3519842a8b4b85]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* sin(z) = -i sin(iz) */
700 Py_complex s, r;
701 s.real = -z.imag;
702 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400703 s = cmath_sinh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 r.real = s.imag;
705 r.imag = -s.real;
706 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000707}
708
Guido van Rossumc6e22901998-12-04 19:26:43 +0000709
Christian Heimes53876d92008-04-19 00:31:39 +0000710/* sinh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000711static Py_complex sinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000712
Brett Cannonb0fc4902014-10-14 17:37:02 -0400713/*[clinic input]
714cmath.sinh = cmath.acos
715
716Return the hyperbolic sine of z.
717[clinic start generated code]*/
718
Tim Peters14e26402001-02-20 20:15:19 +0000719static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300720cmath_sinh_impl(PyObject *module, Py_complex z)
721/*[clinic end generated code: output=38b0a6cce26f3536 input=d2d3fc8c1ddfd2dd]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 Py_complex r;
724 double x_minus_one;
Christian Heimes53876d92008-04-19 00:31:39 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 /* special treatment for sinh(+/-inf + iy) if y is finite and
727 nonzero */
728 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
729 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
730 && (z.imag != 0.)) {
731 if (z.real > 0) {
732 r.real = copysign(INF, cos(z.imag));
733 r.imag = copysign(INF, sin(z.imag));
734 }
735 else {
736 r.real = -copysign(INF, cos(z.imag));
737 r.imag = copysign(INF, sin(z.imag));
738 }
739 }
740 else {
741 r = sinh_special_values[special_type(z.real)]
742 [special_type(z.imag)];
743 }
744 /* need to set errno = EDOM if y is +/- infinity and x is not
745 a NaN */
746 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
747 errno = EDOM;
748 else
749 errno = 0;
750 return r;
751 }
Christian Heimes53876d92008-04-19 00:31:39 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
754 x_minus_one = z.real - copysign(1., z.real);
755 r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
756 r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
757 } else {
758 r.real = cos(z.imag) * sinh(z.real);
759 r.imag = sin(z.imag) * cosh(z.real);
760 }
761 /* detect overflow, and set errno accordingly */
762 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
763 errno = ERANGE;
764 else
765 errno = 0;
766 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000767}
768
Guido van Rossumc6e22901998-12-04 19:26:43 +0000769
Christian Heimesa342c012008-04-20 21:01:16 +0000770static Py_complex sqrt_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000771
Brett Cannonb0fc4902014-10-14 17:37:02 -0400772/*[clinic input]
773cmath.sqrt = cmath.acos
774
775Return the square root of z.
776[clinic start generated code]*/
777
Tim Peters14e26402001-02-20 20:15:19 +0000778static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300779cmath_sqrt_impl(PyObject *module, Py_complex z)
780/*[clinic end generated code: output=b6507b3029c339fc input=7088b166fc9a58c7]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 /*
783 Method: use symmetries to reduce to the case when x = z.real and y
784 = z.imag are nonnegative. Then the real part of the result is
785 given by
Christian Heimes53876d92008-04-19 00:31:39 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 s = sqrt((x + hypot(x, y))/2)
Christian Heimes53876d92008-04-19 00:31:39 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 and the imaginary part is
Christian Heimes53876d92008-04-19 00:31:39 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 d = (y/2)/s
Christian Heimes53876d92008-04-19 00:31:39 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 If either x or y is very large then there's a risk of overflow in
794 computation of the expression x + hypot(x, y). We can avoid this
795 by rewriting the formula for s as:
Christian Heimes53876d92008-04-19 00:31:39 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 s = 2*sqrt(x/8 + hypot(x/8, y/8))
Christian Heimes53876d92008-04-19 00:31:39 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 This costs us two extra multiplications/divisions, but avoids the
800 overhead of checking for x and y large.
Christian Heimes53876d92008-04-19 00:31:39 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 If both x and y are subnormal then hypot(x, y) may also be
803 subnormal, so will lack full precision. We solve this by rescaling
804 x and y by a sufficiently large power of 2 to ensure that x and y
805 are normal.
806 */
Christian Heimes53876d92008-04-19 00:31:39 +0000807
808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 Py_complex r;
810 double s,d;
811 double ax, ay;
Christian Heimes53876d92008-04-19 00:31:39 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 SPECIAL_VALUE(z, sqrt_special_values);
Christian Heimes53876d92008-04-19 00:31:39 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 if (z.real == 0. && z.imag == 0.) {
816 r.real = 0.;
817 r.imag = z.imag;
818 return r;
819 }
Christian Heimes53876d92008-04-19 00:31:39 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 ax = fabs(z.real);
822 ay = fabs(z.imag);
Christian Heimes53876d92008-04-19 00:31:39 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
825 /* here we catch cases where hypot(ax, ay) is subnormal */
826 ax = ldexp(ax, CM_SCALE_UP);
827 s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
828 CM_SCALE_DOWN);
829 } else {
830 ax /= 8.;
831 s = 2.*sqrt(ax + hypot(ax, ay/8.));
832 }
833 d = ay/(2.*s);
Christian Heimes53876d92008-04-19 00:31:39 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (z.real >= 0.) {
836 r.real = s;
837 r.imag = copysign(d, z.imag);
838 } else {
839 r.real = d;
840 r.imag = copysign(s, z.imag);
841 }
842 errno = 0;
843 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000844}
845
Guido van Rossumc6e22901998-12-04 19:26:43 +0000846
Brett Cannonb0fc4902014-10-14 17:37:02 -0400847/*[clinic input]
848cmath.tan = cmath.acos
849
850Return the tangent of z.
851[clinic start generated code]*/
Guido van Rossumc6e22901998-12-04 19:26:43 +0000852
Tim Peters14e26402001-02-20 20:15:19 +0000853static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300854cmath_tan_impl(PyObject *module, Py_complex z)
855/*[clinic end generated code: output=7c5f13158a72eb13 input=fc167e528767888e]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 /* tan(z) = -i tanh(iz) */
858 Py_complex s, r;
859 s.real = -z.imag;
860 s.imag = z.real;
Brett Cannonb0fc4902014-10-14 17:37:02 -0400861 s = cmath_tanh_impl(module, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 r.real = s.imag;
863 r.imag = -s.real;
864 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000865}
866
Guido van Rossumc6e22901998-12-04 19:26:43 +0000867
Christian Heimes53876d92008-04-19 00:31:39 +0000868/* tanh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000869static Py_complex tanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000870
Brett Cannonb0fc4902014-10-14 17:37:02 -0400871/*[clinic input]
872cmath.tanh = cmath.acos
873
874Return the hyperbolic tangent of z.
875[clinic start generated code]*/
876
Tim Peters14e26402001-02-20 20:15:19 +0000877static Py_complex
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300878cmath_tanh_impl(PyObject *module, Py_complex z)
879/*[clinic end generated code: output=36d547ef7aca116c input=22f67f9dc6d29685]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 /* Formula:
Christian Heimes53876d92008-04-19 00:31:39 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
884 (1+tan(y)^2 tanh(x)^2)
Christian Heimes53876d92008-04-19 00:31:39 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
887 as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
888 by 4 exp(-2*x) instead, to avoid possible overflow in the
889 computation of cosh(x).
Christian Heimes53876d92008-04-19 00:31:39 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 */
Christian Heimes53876d92008-04-19 00:31:39 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_complex r;
894 double tx, ty, cx, txty, denom;
Christian Heimes53876d92008-04-19 00:31:39 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 /* special treatment for tanh(+/-inf + iy) if y is finite and
897 nonzero */
898 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
899 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
900 && (z.imag != 0.)) {
901 if (z.real > 0) {
902 r.real = 1.0;
903 r.imag = copysign(0.,
904 2.*sin(z.imag)*cos(z.imag));
905 }
906 else {
907 r.real = -1.0;
908 r.imag = copysign(0.,
909 2.*sin(z.imag)*cos(z.imag));
910 }
911 }
912 else {
913 r = tanh_special_values[special_type(z.real)]
914 [special_type(z.imag)];
915 }
916 /* need to set errno = EDOM if z.imag is +/-infinity and
917 z.real is finite */
918 if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
919 errno = EDOM;
920 else
921 errno = 0;
922 return r;
923 }
Christian Heimes53876d92008-04-19 00:31:39 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 /* danger of overflow in 2.*z.imag !*/
926 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
927 r.real = copysign(1., z.real);
928 r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
929 } else {
930 tx = tanh(z.real);
931 ty = tan(z.imag);
932 cx = 1./cosh(z.real);
933 txty = tx*ty;
934 denom = 1. + txty*txty;
935 r.real = tx*(1.+ty*ty)/denom;
936 r.imag = ((ty/denom)*cx)*cx;
937 }
938 errno = 0;
939 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000940}
941
Guido van Rossumc6e22901998-12-04 19:26:43 +0000942
Brett Cannonb0fc4902014-10-14 17:37:02 -0400943/*[clinic input]
944cmath.log
945
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300946 z as x: Py_complex
947 base as y_obj: object = NULL
Brett Cannonb0fc4902014-10-14 17:37:02 -0400948 /
949
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300950log(z[, base]) -> the logarithm of z to the given base.
Brett Cannonb0fc4902014-10-14 17:37:02 -0400951
952If the base not specified, returns the natural logarithm (base e) of z.
953[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +0000954
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000955static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300956cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300957/*[clinic end generated code: output=4effdb7d258e0d94 input=230ed3a71ecd000a]*/
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 Py_complex y;
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 x = c_log(x);
Brett Cannonb0fc4902014-10-14 17:37:02 -0400963 if (y_obj != NULL) {
964 y = PyComplex_AsCComplex(y_obj);
965 if (PyErr_Occurred()) {
966 return NULL;
967 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 y = c_log(y);
Antoine Pitroude08cb62014-07-07 19:08:47 -0400969 x = _Py_c_quot(x, y);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 if (errno != 0)
972 return math_error();
973 return PyComplex_FromCComplex(x);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000974}
975
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000976
977/* And now the glue to make them available from Python: */
978
Roger E. Masse24070ca1996-12-09 22:59:53 +0000979static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000980math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (errno == EDOM)
983 PyErr_SetString(PyExc_ValueError, "math domain error");
984 else if (errno == ERANGE)
985 PyErr_SetString(PyExc_OverflowError, "math range error");
986 else /* Unexpected math error */
987 PyErr_SetFromErrno(PyExc_ValueError);
988 return NULL;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000989}
990
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000991
Brett Cannonb0fc4902014-10-14 17:37:02 -0400992/*[clinic input]
993cmath.phase
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000994
Brett Cannonb0fc4902014-10-14 17:37:02 -0400995 z: Py_complex
996 /
997
998Return argument, also known as the phase angle, of a complex.
999[clinic start generated code]*/
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001000
Christian Heimes53876d92008-04-19 00:31:39 +00001001static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001002cmath_phase_impl(PyObject *module, Py_complex z)
1003/*[clinic end generated code: output=50725086a7bfd253 input=5cf75228ba94b69d]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 double phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -04001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 phi = c_atan2(z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 if (errno != 0)
1010 return math_error();
1011 else
1012 return PyFloat_FromDouble(phi);
Christian Heimes53876d92008-04-19 00:31:39 +00001013}
1014
Brett Cannonb0fc4902014-10-14 17:37:02 -04001015/*[clinic input]
1016cmath.polar
1017
1018 z: Py_complex
1019 /
1020
1021Convert a complex from rectangular coordinates to polar coordinates.
1022
1023r is the distance from 0 and phi the phase angle.
1024[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001025
1026static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001027cmath_polar_impl(PyObject *module, Py_complex z)
1028/*[clinic end generated code: output=d0a8147c41dbb654 input=26c353574fd1a861]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 double r, phi;
Brett Cannonb0fc4902014-10-14 17:37:02 -04001031
Antoine Pitrou6bc217d2015-06-23 14:31:11 +02001032 errno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 phi = c_atan2(z); /* should not cause any exception */
Antoine Pitroua72f0cd2015-06-23 14:38:13 +02001034 r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (errno != 0)
1036 return math_error();
1037 else
1038 return Py_BuildValue("dd", r, phi);
Christian Heimes53876d92008-04-19 00:31:39 +00001039}
1040
Christian Heimes53876d92008-04-19 00:31:39 +00001041/*
1042 rect() isn't covered by the C99 standard, but it's not too hard to
1043 figure out 'spirit of C99' rules for special value handing:
1044
1045 rect(x, t) should behave like exp(log(x) + it) for positive-signed x
1046 rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x
1047 rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0)
1048 gives nan +- i0 with the sign of the imaginary part unspecified.
1049
1050*/
1051
Christian Heimesa342c012008-04-20 21:01:16 +00001052static Py_complex rect_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +00001053
Brett Cannonb0fc4902014-10-14 17:37:02 -04001054/*[clinic input]
1055cmath.rect
1056
1057 r: double
1058 phi: double
1059 /
1060
1061Convert from polar coordinates to rectangular coordinates.
1062[clinic start generated code]*/
1063
Christian Heimes53876d92008-04-19 00:31:39 +00001064static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001065cmath_rect_impl(PyObject *module, double r, double phi)
1066/*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_complex z;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 errno = 0;
Christian Heimes53876d92008-04-19 00:31:39 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* deal with special values */
1072 if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
1073 /* if r is +/-infinity and phi is finite but nonzero then
1074 result is (+-INF +-INF i), but we need to compute cos(phi)
1075 and sin(phi) to figure out the signs. */
1076 if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
1077 && (phi != 0.))) {
1078 if (r > 0) {
1079 z.real = copysign(INF, cos(phi));
1080 z.imag = copysign(INF, sin(phi));
1081 }
1082 else {
1083 z.real = -copysign(INF, cos(phi));
1084 z.imag = -copysign(INF, sin(phi));
1085 }
1086 }
1087 else {
1088 z = rect_special_values[special_type(r)]
1089 [special_type(phi)];
1090 }
1091 /* need to set errno = EDOM if r is a nonzero number and phi
1092 is infinite */
1093 if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
1094 errno = EDOM;
1095 else
1096 errno = 0;
1097 }
Mark Dickinson58ceecf2013-07-20 17:59:13 +01001098 else if (phi == 0.0) {
1099 /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See
1100 bugs.python.org/issue18513. */
1101 z.real = r;
1102 z.imag = r * phi;
1103 errno = 0;
1104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 else {
1106 z.real = r * cos(phi);
1107 z.imag = r * sin(phi);
1108 errno = 0;
1109 }
Christian Heimes53876d92008-04-19 00:31:39 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (errno != 0)
1112 return math_error();
1113 else
1114 return PyComplex_FromCComplex(z);
Christian Heimes53876d92008-04-19 00:31:39 +00001115}
1116
Brett Cannonb0fc4902014-10-14 17:37:02 -04001117/*[clinic input]
1118cmath.isfinite = cmath.polar
1119
1120Return True if both the real and imaginary parts of z are finite, else False.
1121[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001122
1123static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001124cmath_isfinite_impl(PyObject *module, Py_complex z)
1125/*[clinic end generated code: output=ac76611e2c774a36 input=848e7ee701895815]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001126{
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001127 return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
1128}
1129
Brett Cannonb0fc4902014-10-14 17:37:02 -04001130/*[clinic input]
1131cmath.isnan = cmath.polar
1132
1133Checks if the real or imaginary part of z not a number (NaN).
1134[clinic start generated code]*/
Mark Dickinson8e0c9962010-07-11 17:38:24 +00001135
1136static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001137cmath_isnan_impl(PyObject *module, Py_complex z)
1138/*[clinic end generated code: output=e7abf6e0b28beab7 input=71799f5d284c9baf]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001141}
1142
Brett Cannonb0fc4902014-10-14 17:37:02 -04001143/*[clinic input]
1144cmath.isinf = cmath.polar
1145
1146Checks if the real or imaginary part of z is infinite.
1147[clinic start generated code]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001148
1149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001150cmath_isinf_impl(PyObject *module, Py_complex z)
1151/*[clinic end generated code: output=502a75a79c773469 input=363df155c7181329]*/
Christian Heimes53876d92008-04-19 00:31:39 +00001152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
1154 Py_IS_INFINITY(z.imag));
Christian Heimes53876d92008-04-19 00:31:39 +00001155}
1156
Tal Einatd5519ed2015-05-31 22:05:00 +03001157/*[clinic input]
1158cmath.isclose -> bool
1159
1160 a: Py_complex
1161 b: Py_complex
1162 *
1163 rel_tol: double = 1e-09
1164 maximum difference for being considered "close", relative to the
1165 magnitude of the input values
1166 abs_tol: double = 0.0
1167 maximum difference for being considered "close", regardless of the
1168 magnitude of the input values
1169
1170Determine whether two complex numbers are close in value.
1171
1172Return True if a is close in value to b, and False otherwise.
1173
1174For the values to be considered close, the difference between them must be
1175smaller than at least one of the tolerances.
1176
1177-inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is
1178not close to anything, even itself. inf and -inf are only close to themselves.
1179[clinic start generated code]*/
1180
1181static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001182cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b,
Tal Einatd5519ed2015-05-31 22:05:00 +03001183 double rel_tol, double abs_tol)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001184/*[clinic end generated code: output=8a2486cc6e0014d1 input=df9636d7de1d4ac3]*/
Tal Einatd5519ed2015-05-31 22:05:00 +03001185{
1186 double diff;
1187
1188 /* sanity check on the inputs */
1189 if (rel_tol < 0.0 || abs_tol < 0.0 ) {
1190 PyErr_SetString(PyExc_ValueError,
1191 "tolerances must be non-negative");
1192 return -1;
1193 }
1194
1195 if ( (a.real == b.real) && (a.imag == b.imag) ) {
1196 /* short circuit exact equality -- needed to catch two infinities of
1197 the same sign. And perhaps speeds things up a bit sometimes.
1198 */
1199 return 1;
1200 }
1201
1202 /* This catches the case of two infinities of opposite sign, or
1203 one infinity and one finite number. Two infinities of opposite
1204 sign would otherwise have an infinite relative tolerance.
1205 Two infinities of the same sign are caught by the equality check
1206 above.
1207 */
1208
1209 if (Py_IS_INFINITY(a.real) || Py_IS_INFINITY(a.imag) ||
1210 Py_IS_INFINITY(b.real) || Py_IS_INFINITY(b.imag)) {
1211 return 0;
1212 }
1213
1214 /* now do the regular computation
1215 this is essentially the "weak" test from the Boost library
1216 */
1217
1218 diff = _Py_c_abs(_Py_c_diff(a, b));
1219
1220 return (((diff <= rel_tol * _Py_c_abs(b)) ||
1221 (diff <= rel_tol * _Py_c_abs(a))) ||
1222 (diff <= abs_tol));
1223}
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001225PyDoc_STRVAR(module_doc,
Ned Batchelder6faad352019-05-17 05:59:14 -04001226"This module provides access to mathematical functions for complex\n"
1227"numbers.");
Guido van Rossumc6e22901998-12-04 19:26:43 +00001228
Roger E. Masse24070ca1996-12-09 22:59:53 +00001229static PyMethodDef cmath_methods[] = {
Brett Cannonb0fc4902014-10-14 17:37:02 -04001230 CMATH_ACOS_METHODDEF
1231 CMATH_ACOSH_METHODDEF
1232 CMATH_ASIN_METHODDEF
1233 CMATH_ASINH_METHODDEF
1234 CMATH_ATAN_METHODDEF
1235 CMATH_ATANH_METHODDEF
1236 CMATH_COS_METHODDEF
1237 CMATH_COSH_METHODDEF
1238 CMATH_EXP_METHODDEF
Tal Einatd5519ed2015-05-31 22:05:00 +03001239 CMATH_ISCLOSE_METHODDEF
Brett Cannonb0fc4902014-10-14 17:37:02 -04001240 CMATH_ISFINITE_METHODDEF
1241 CMATH_ISINF_METHODDEF
1242 CMATH_ISNAN_METHODDEF
1243 CMATH_LOG_METHODDEF
1244 CMATH_LOG10_METHODDEF
1245 CMATH_PHASE_METHODDEF
1246 CMATH_POLAR_METHODDEF
1247 CMATH_RECT_METHODDEF
1248 CMATH_SIN_METHODDEF
1249 CMATH_SINH_METHODDEF
1250 CMATH_SQRT_METHODDEF
1251 CMATH_TAN_METHODDEF
1252 CMATH_TANH_METHODDEF
1253 {NULL, NULL} /* sentinel */
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001254};
1255
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256
1257static struct PyModuleDef cmathmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 PyModuleDef_HEAD_INIT,
1259 "cmath",
1260 module_doc,
1261 -1,
1262 cmath_methods,
1263 NULL,
1264 NULL,
1265 NULL,
1266 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001267};
1268
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001269PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001270PyInit_cmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 PyObject *m;
Tim Peters14e26402001-02-20 20:15:19 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 m = PyModule_Create(&cmathmodule);
1275 if (m == NULL)
1276 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 PyModule_AddObject(m, "pi",
1279 PyFloat_FromDouble(Py_MATH_PI));
1280 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
Guido van Rossum0a891d72016-08-15 09:12:52 -07001281 PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
Mark Dickinson84e63112016-08-29 13:56:58 +01001282 PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
1283 PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj()));
1284#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
1285 PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
1286 PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj()));
1287#endif
Christian Heimesa342c012008-04-20 21:01:16 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* initialize special value tables */
Christian Heimesa342c012008-04-20 21:01:16 +00001290
1291#define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
1292#define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
1293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 INIT_SPECIAL_VALUES(acos_special_values, {
1295 C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
1296 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1297 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1298 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1299 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1300 C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
1301 C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
1302 })
Christian Heimesa342c012008-04-20 21:01:16 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 INIT_SPECIAL_VALUES(acosh_special_values, {
1305 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1306 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1307 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1308 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1309 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1310 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1311 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1312 })
Christian Heimesa342c012008-04-20 21:01:16 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 INIT_SPECIAL_VALUES(asinh_special_values, {
1315 C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
1316 C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
1317 C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
1318 C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
1319 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1320 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1321 C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
1322 })
Christian Heimesa342c012008-04-20 21:01:16 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 INIT_SPECIAL_VALUES(atanh_special_values, {
1325 C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
1326 C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
1327 C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
1328 C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
1329 C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
1330 C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
1331 C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
1332 })
Christian Heimesa342c012008-04-20 21:01:16 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 INIT_SPECIAL_VALUES(cosh_special_values, {
1335 C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
1336 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1337 C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
1338 C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
1339 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1340 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1341 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1342 })
Christian Heimesa342c012008-04-20 21:01:16 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 INIT_SPECIAL_VALUES(exp_special_values, {
1345 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1346 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1347 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1348 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1349 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1350 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1351 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1352 })
Christian Heimesa342c012008-04-20 21:01:16 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 INIT_SPECIAL_VALUES(log_special_values, {
1355 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1356 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1357 C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
1358 C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
1359 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1360 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1361 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1362 })
Christian Heimesa342c012008-04-20 21:01:16 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 INIT_SPECIAL_VALUES(sinh_special_values, {
1365 C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
1366 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1367 C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
1368 C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
1369 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1370 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1371 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1372 })
Christian Heimesa342c012008-04-20 21:01:16 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 INIT_SPECIAL_VALUES(sqrt_special_values, {
1375 C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
1376 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1377 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1378 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1379 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1380 C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
1381 C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
1382 })
Christian Heimesa342c012008-04-20 21:01:16 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 INIT_SPECIAL_VALUES(tanh_special_values, {
1385 C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
1386 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1387 C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
1388 C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
1389 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1390 C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
1391 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1392 })
Christian Heimesa342c012008-04-20 21:01:16 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 INIT_SPECIAL_VALUES(rect_special_values, {
1395 C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
1396 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1397 C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
1398 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1399 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1400 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1401 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1402 })
1403 return m;
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001404}