blob: 347f88d9f8a9978d1f41a5702db2c768d63828cb [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"
Christian Heimes53876d92008-04-19 00:31:39 +00006/* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from
7 float.h. We assume that FLT_RADIX is either 2 or 16. */
8#include <float.h>
Guido van Rossum71aa32f1996-01-12 01:34:57 +00009
Christian Heimes53876d92008-04-19 00:31:39 +000010#if (FLT_RADIX != 2 && FLT_RADIX != 16)
11#error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"
Guido van Rossum71aa32f1996-01-12 01:34:57 +000012#endif
13
Christian Heimes53876d92008-04-19 00:31:39 +000014#ifndef M_LN2
15#define M_LN2 (0.6931471805599453094) /* natural log of 2 */
16#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000017
Christian Heimes53876d92008-04-19 00:31:39 +000018#ifndef M_LN10
19#define M_LN10 (2.302585092994045684) /* natural log of 10 */
20#endif
21
22/*
23 CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log,
24 inverse trig and inverse hyperbolic trig functions. Its log is used in the
25 evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unecessary
26 overflow.
27 */
28
29#define CM_LARGE_DOUBLE (DBL_MAX/4.)
30#define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE))
31#define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
32#define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
33
34/*
35 CM_SCALE_UP is an odd integer chosen such that multiplication by
36 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
37 CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
38 square roots accurately when the real and imaginary parts of the argument
39 are subnormal.
40*/
41
42#if FLT_RADIX==2
43#define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1)
44#elif FLT_RADIX==16
45#define CM_SCALE_UP (4*DBL_MANT_DIG+1)
46#endif
47#define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000048
49/* forward declarations */
Christian Heimes53876d92008-04-19 00:31:39 +000050static Py_complex c_asinh(Py_complex);
51static Py_complex c_atanh(Py_complex);
52static Py_complex c_cosh(Py_complex);
53static Py_complex c_sinh(Py_complex);
Jeremy Hylton938ace62002-07-17 16:30:39 +000054static Py_complex c_sqrt(Py_complex);
Christian Heimes53876d92008-04-19 00:31:39 +000055static Py_complex c_tanh(Py_complex);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000056static PyObject * math_error(void);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000057
Christian Heimes53876d92008-04-19 00:31:39 +000058/* Code to deal with special values (infinities, NaNs, etc.). */
59
60/* special_type takes a double and returns an integer code indicating
61 the type of the double as follows:
62*/
63
64enum special_types {
65 ST_NINF, /* 0, negative infinity */
66 ST_NEG, /* 1, negative finite number (nonzero) */
67 ST_NZERO, /* 2, -0. */
68 ST_PZERO, /* 3, +0. */
69 ST_POS, /* 4, positive finite number (nonzero) */
70 ST_PINF, /* 5, positive infinity */
71 ST_NAN, /* 6, Not a Number */
72};
73
74static enum special_types
75special_type(double d)
76{
77 if (Py_IS_FINITE(d)) {
78 if (d != 0) {
79 if (copysign(1., d) == 1.)
80 return ST_POS;
81 else
82 return ST_NEG;
83 }
84 else {
85 if (copysign(1., d) == 1.)
86 return ST_PZERO;
87 else
88 return ST_NZERO;
89 }
90 }
91 if (Py_IS_NAN(d))
92 return ST_NAN;
93 if (copysign(1., d) == 1.)
94 return ST_PINF;
95 else
96 return ST_NINF;
97}
98
99#define SPECIAL_VALUE(z, table) \
100 if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
101 errno = 0; \
102 return table[special_type((z).real)] \
103 [special_type((z).imag)]; \
104 }
105
106#define P Py_MATH_PI
107#define P14 0.25*Py_MATH_PI
108#define P12 0.5*Py_MATH_PI
109#define P34 0.75*Py_MATH_PI
Christian Heimesa342c012008-04-20 21:01:16 +0000110#define INF Py_HUGE_VAL
111#define N Py_NAN
Christian Heimes53876d92008-04-19 00:31:39 +0000112#define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
113
114/* First, the C functions that do the real work. Each of the c_*
115 functions computes and returns the C99 Annex G recommended result
116 and also sets errno as follows: errno = 0 if no floating-point
117 exception is associated with the result; errno = EDOM if C99 Annex
118 G recommends raising divide-by-zero or invalid for this result; and
119 errno = ERANGE where the overflow floating-point signal should be
120 raised.
121*/
122
Christian Heimesa342c012008-04-20 21:01:16 +0000123static Py_complex acos_special_values[7][7];
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000124
Tim Peters14e26402001-02-20 20:15:19 +0000125static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000126c_acos(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000127{
Christian Heimes53876d92008-04-19 00:31:39 +0000128 Py_complex s1, s2, r;
129
130 SPECIAL_VALUE(z, acos_special_values);
131
132 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
133 /* avoid unnecessary overflow for large arguments */
134 r.real = atan2(fabs(z.imag), z.real);
135 /* split into cases to make sure that the branch cut has the
136 correct continuity on systems with unsigned zeros */
137 if (z.real < 0.) {
138 r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
139 M_LN2*2., z.imag);
140 } else {
141 r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
142 M_LN2*2., -z.imag);
143 }
144 } else {
145 s1.real = 1.-z.real;
146 s1.imag = -z.imag;
147 s1 = c_sqrt(s1);
148 s2.real = 1.+z.real;
149 s2.imag = z.imag;
150 s2 = c_sqrt(s2);
151 r.real = 2.*atan2(s1.real, s2.real);
152 r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real);
153 }
154 errno = 0;
155 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000156}
157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158PyDoc_STRVAR(c_acos_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000159"acos(x)\n"
160"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161"Return the arc cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000162
163
Christian Heimesa342c012008-04-20 21:01:16 +0000164static Py_complex acosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000165
Tim Peters14e26402001-02-20 20:15:19 +0000166static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000167c_acosh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000168{
Christian Heimes53876d92008-04-19 00:31:39 +0000169 Py_complex s1, s2, r;
170
171 SPECIAL_VALUE(z, acosh_special_values);
172
173 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
174 /* avoid unnecessary overflow for large arguments */
175 r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
176 r.imag = atan2(z.imag, z.real);
177 } else {
178 s1.real = z.real - 1.;
179 s1.imag = z.imag;
180 s1 = c_sqrt(s1);
181 s2.real = z.real + 1.;
182 s2.imag = z.imag;
183 s2 = c_sqrt(s2);
184 r.real = asinh(s1.real*s2.real + s1.imag*s2.imag);
185 r.imag = 2.*atan2(s1.imag, s2.real);
186 }
187 errno = 0;
188 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000189}
190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000191PyDoc_STRVAR(c_acosh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000192"acosh(x)\n"
193"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194"Return the hyperbolic arccosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000195
196
Tim Peters14e26402001-02-20 20:15:19 +0000197static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000198c_asin(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000199{
Christian Heimes53876d92008-04-19 00:31:39 +0000200 /* asin(z) = -i asinh(iz) */
201 Py_complex s, r;
202 s.real = -z.imag;
203 s.imag = z.real;
204 s = c_asinh(s);
205 r.real = s.imag;
206 r.imag = -s.real;
207 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000208}
209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210PyDoc_STRVAR(c_asin_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000211"asin(x)\n"
212"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213"Return the arc sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000214
215
Christian Heimesa342c012008-04-20 21:01:16 +0000216static Py_complex asinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000217
Tim Peters14e26402001-02-20 20:15:19 +0000218static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000219c_asinh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000220{
Christian Heimes53876d92008-04-19 00:31:39 +0000221 Py_complex s1, s2, r;
222
223 SPECIAL_VALUE(z, asinh_special_values);
224
225 if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
226 if (z.imag >= 0.) {
227 r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
228 M_LN2*2., z.real);
229 } else {
230 r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
231 M_LN2*2., -z.real);
232 }
233 r.imag = atan2(z.imag, fabs(z.real));
234 } else {
235 s1.real = 1.+z.imag;
236 s1.imag = -z.real;
237 s1 = c_sqrt(s1);
238 s2.real = 1.-z.imag;
239 s2.imag = z.real;
240 s2 = c_sqrt(s2);
241 r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
242 r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
243 }
244 errno = 0;
245 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000246}
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(c_asinh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000249"asinh(x)\n"
250"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000251"Return the hyperbolic arc sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000252
253
Tim Peters14e26402001-02-20 20:15:19 +0000254static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000255c_atan(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000256{
Christian Heimes53876d92008-04-19 00:31:39 +0000257 /* atan(z) = -i atanh(iz) */
258 Py_complex s, r;
259 s.real = -z.imag;
260 s.imag = z.real;
261 s = c_atanh(s);
262 r.real = s.imag;
263 r.imag = -s.real;
264 return r;
265}
266
267/* Windows screws up atan2 for inf and nan */
268static double
269c_atan2(Py_complex z)
270{
271 if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
272 return Py_NAN;
273 if (Py_IS_INFINITY(z.imag)) {
274 if (Py_IS_INFINITY(z.real)) {
275 if (copysign(1., z.real) == 1.)
276 /* atan2(+-inf, +inf) == +-pi/4 */
277 return copysign(0.25*Py_MATH_PI, z.imag);
278 else
279 /* atan2(+-inf, -inf) == +-pi*3/4 */
280 return copysign(0.75*Py_MATH_PI, z.imag);
281 }
282 /* atan2(+-inf, x) == +-pi/2 for finite x */
283 return copysign(0.5*Py_MATH_PI, z.imag);
284 }
285 return atan2(z.imag, z.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(c_atan_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000289"atan(x)\n"
290"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291"Return the arc tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000292
293
Christian Heimesa342c012008-04-20 21:01:16 +0000294static Py_complex atanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000295
Tim Peters14e26402001-02-20 20:15:19 +0000296static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000297c_atanh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000298{
Christian Heimes53876d92008-04-19 00:31:39 +0000299 Py_complex r;
300 double ay, h;
301
302 SPECIAL_VALUE(z, atanh_special_values);
303
304 /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
305 if (z.real < 0.) {
306 return c_neg(c_atanh(c_neg(z)));
307 }
308
309 ay = fabs(z.imag);
310 if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
311 /*
312 if abs(z) is large then we use the approximation
313 atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
314 of z.imag)
315 */
316 h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
317 r.real = z.real/4./h/h;
318 /* the two negations in the next line cancel each other out
319 except when working with unsigned zeros: they're there to
320 ensure that the branch cut has the correct continuity on
321 systems that don't support signed zeros */
322 r.imag = -copysign(Py_MATH_PI/2., -z.imag);
323 errno = 0;
324 } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
325 /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
326 if (ay == 0.) {
327 r.real = INF;
328 r.imag = z.imag;
329 errno = EDOM;
330 } else {
331 r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
332 r.imag = copysign(atan2(2., -ay)/2, z.imag);
333 errno = 0;
334 }
335 } else {
336 r.real = log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
337 r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
338 errno = 0;
339 }
340 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000341}
342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343PyDoc_STRVAR(c_atanh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000344"atanh(x)\n"
345"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346"Return the hyperbolic arc tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000347
348
Tim Peters14e26402001-02-20 20:15:19 +0000349static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000350c_cos(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000351{
Christian Heimes53876d92008-04-19 00:31:39 +0000352 /* cos(z) = cosh(iz) */
Guido van Rossum9e720e31996-07-21 02:31:35 +0000353 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000354 r.real = -z.imag;
355 r.imag = z.real;
356 r = c_cosh(r);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000357 return r;
358}
359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000360PyDoc_STRVAR(c_cos_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000361"cos(x)\n"
362"n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363"Return the cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000364
365
Christian Heimes53876d92008-04-19 00:31:39 +0000366/* cosh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000367static Py_complex cosh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000368
Tim Peters14e26402001-02-20 20:15:19 +0000369static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000370c_cosh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000371{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000372 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000373 double x_minus_one;
374
375 /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
376 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
377 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
378 (z.imag != 0.)) {
379 if (z.real > 0) {
380 r.real = copysign(INF, cos(z.imag));
381 r.imag = copysign(INF, sin(z.imag));
382 }
383 else {
384 r.real = copysign(INF, cos(z.imag));
385 r.imag = -copysign(INF, sin(z.imag));
386 }
387 }
388 else {
389 r = cosh_special_values[special_type(z.real)]
390 [special_type(z.imag)];
391 }
392 /* need to set errno = EDOM if y is +/- infinity and x is not
393 a NaN */
394 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
395 errno = EDOM;
396 else
397 errno = 0;
398 return r;
399 }
400
401 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
402 /* deal correctly with cases where cosh(z.real) overflows but
403 cosh(z) does not. */
404 x_minus_one = z.real - copysign(1., z.real);
405 r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
406 r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
407 } else {
408 r.real = cos(z.imag) * cosh(z.real);
409 r.imag = sin(z.imag) * sinh(z.real);
410 }
411 /* detect overflow, and set errno accordingly */
412 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
413 errno = ERANGE;
414 else
415 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000416 return r;
417}
418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000419PyDoc_STRVAR(c_cosh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000420"cosh(x)\n"
421"n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422"Return the hyperbolic cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000423
424
Christian Heimes53876d92008-04-19 00:31:39 +0000425/* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for
426 finite y */
Christian Heimesa342c012008-04-20 21:01:16 +0000427static Py_complex exp_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000428
Tim Peters14e26402001-02-20 20:15:19 +0000429static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000430c_exp(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000431{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000432 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000433 double l;
434
435 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
436 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
437 && (z.imag != 0.)) {
438 if (z.real > 0) {
439 r.real = copysign(INF, cos(z.imag));
440 r.imag = copysign(INF, sin(z.imag));
441 }
442 else {
443 r.real = copysign(0., cos(z.imag));
444 r.imag = copysign(0., sin(z.imag));
445 }
446 }
447 else {
448 r = exp_special_values[special_type(z.real)]
449 [special_type(z.imag)];
450 }
451 /* need to set errno = EDOM if y is +/- infinity and x is not
452 a NaN and not -infinity */
453 if (Py_IS_INFINITY(z.imag) &&
454 (Py_IS_FINITE(z.real) ||
455 (Py_IS_INFINITY(z.real) && z.real > 0)))
456 errno = EDOM;
457 else
458 errno = 0;
459 return r;
460 }
461
462 if (z.real > CM_LOG_LARGE_DOUBLE) {
463 l = exp(z.real-1.);
464 r.real = l*cos(z.imag)*Py_MATH_E;
465 r.imag = l*sin(z.imag)*Py_MATH_E;
466 } else {
467 l = exp(z.real);
468 r.real = l*cos(z.imag);
469 r.imag = l*sin(z.imag);
470 }
471 /* detect overflow, and set errno accordingly */
472 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
473 errno = ERANGE;
474 else
475 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000476 return r;
477}
478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(c_exp_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000480"exp(x)\n"
481"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482"Return the exponential value e**x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000483
484
Christian Heimesa342c012008-04-20 21:01:16 +0000485static Py_complex log_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000486
Tim Peters14e26402001-02-20 20:15:19 +0000487static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000488c_log(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000489{
Christian Heimes53876d92008-04-19 00:31:39 +0000490 /*
491 The usual formula for the real part is log(hypot(z.real, z.imag)).
492 There are four situations where this formula is potentially
493 problematic:
494
495 (1) the absolute value of z is subnormal. Then hypot is subnormal,
496 so has fewer than the usual number of bits of accuracy, hence may
497 have large relative error. This then gives a large absolute error
498 in the log. This can be solved by rescaling z by a suitable power
499 of 2.
500
501 (2) the absolute value of z is greater than DBL_MAX (e.g. when both
502 z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
503 Again, rescaling solves this.
504
505 (3) the absolute value of z is close to 1. In this case it's
506 difficult to achieve good accuracy, at least in part because a
507 change of 1ulp in the real or imaginary part of z can result in a
508 change of billions of ulps in the correctly rounded answer.
509
510 (4) z = 0. The simplest thing to do here is to call the
511 floating-point log with an argument of 0, and let its behaviour
512 (returning -infinity, signaling a floating-point exception, setting
513 errno, or whatever) determine that of c_log. So the usual formula
514 is fine here.
515
516 */
517
Guido van Rossum9e720e31996-07-21 02:31:35 +0000518 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000519 double ax, ay, am, an, h;
520
521 SPECIAL_VALUE(z, log_special_values);
522
523 ax = fabs(z.real);
524 ay = fabs(z.imag);
525
526 if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
527 r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
528 } else if (ax < DBL_MIN && ay < DBL_MIN) {
529 if (ax > 0. || ay > 0.) {
530 /* catch cases where hypot(ax, ay) is subnormal */
531 r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
532 ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
533 }
534 else {
535 /* log(+/-0. +/- 0i) */
536 r.real = -INF;
537 r.imag = atan2(z.imag, z.real);
538 errno = EDOM;
539 return r;
540 }
541 } else {
542 h = hypot(ax, ay);
543 if (0.71 <= h && h <= 1.73) {
544 am = ax > ay ? ax : ay; /* max(ax, ay) */
545 an = ax > ay ? ay : ax; /* min(ax, ay) */
546 r.real = log1p((am-1)*(am+1)+an*an)/2.;
547 } else {
548 r.real = log(h);
549 }
550 }
551 r.imag = atan2(z.imag, z.real);
552 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000553 return r;
554}
555
Guido van Rossumc6e22901998-12-04 19:26:43 +0000556
Tim Peters14e26402001-02-20 20:15:19 +0000557static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000558c_log10(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000559{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000560 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000561 int errno_save;
562
563 r = c_log(z);
564 errno_save = errno; /* just in case the divisions affect errno */
565 r.real = r.real / M_LN10;
566 r.imag = r.imag / M_LN10;
567 errno = errno_save;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000568 return r;
569}
570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571PyDoc_STRVAR(c_log10_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000572"log10(x)\n"
573"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000574"Return the base-10 logarithm of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000575
576
Tim Peters14e26402001-02-20 20:15:19 +0000577static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000578c_sin(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000579{
Christian Heimes53876d92008-04-19 00:31:39 +0000580 /* sin(z) = -i sin(iz) */
581 Py_complex s, r;
582 s.real = -z.imag;
583 s.imag = z.real;
584 s = c_sinh(s);
585 r.real = s.imag;
586 r.imag = -s.real;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000587 return r;
588}
589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000590PyDoc_STRVAR(c_sin_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000591"sin(x)\n"
592"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000593"Return the sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000594
595
Christian Heimes53876d92008-04-19 00:31:39 +0000596/* sinh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000597static Py_complex sinh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000598
Tim Peters14e26402001-02-20 20:15:19 +0000599static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000600c_sinh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000601{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000602 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000603 double x_minus_one;
604
605 /* special treatment for sinh(+/-inf + iy) if y is finite and
606 nonzero */
607 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
608 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
609 && (z.imag != 0.)) {
610 if (z.real > 0) {
611 r.real = copysign(INF, cos(z.imag));
612 r.imag = copysign(INF, sin(z.imag));
613 }
614 else {
615 r.real = -copysign(INF, cos(z.imag));
616 r.imag = copysign(INF, sin(z.imag));
617 }
618 }
619 else {
620 r = sinh_special_values[special_type(z.real)]
621 [special_type(z.imag)];
622 }
623 /* need to set errno = EDOM if y is +/- infinity and x is not
624 a NaN */
625 if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
626 errno = EDOM;
627 else
628 errno = 0;
629 return r;
630 }
631
632 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
633 x_minus_one = z.real - copysign(1., z.real);
634 r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
635 r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
636 } else {
637 r.real = cos(z.imag) * sinh(z.real);
638 r.imag = sin(z.imag) * cosh(z.real);
639 }
640 /* detect overflow, and set errno accordingly */
641 if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
642 errno = ERANGE;
643 else
644 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000645 return r;
646}
647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(c_sinh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000649"sinh(x)\n"
650"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000651"Return the hyperbolic sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000652
653
Christian Heimesa342c012008-04-20 21:01:16 +0000654static Py_complex sqrt_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000655
Tim Peters14e26402001-02-20 20:15:19 +0000656static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000657c_sqrt(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000658{
Christian Heimes53876d92008-04-19 00:31:39 +0000659 /*
660 Method: use symmetries to reduce to the case when x = z.real and y
661 = z.imag are nonnegative. Then the real part of the result is
662 given by
663
664 s = sqrt((x + hypot(x, y))/2)
665
666 and the imaginary part is
667
668 d = (y/2)/s
669
670 If either x or y is very large then there's a risk of overflow in
671 computation of the expression x + hypot(x, y). We can avoid this
672 by rewriting the formula for s as:
673
674 s = 2*sqrt(x/8 + hypot(x/8, y/8))
675
676 This costs us two extra multiplications/divisions, but avoids the
677 overhead of checking for x and y large.
678
679 If both x and y are subnormal then hypot(x, y) may also be
680 subnormal, so will lack full precision. We solve this by rescaling
681 x and y by a sufficiently large power of 2 to ensure that x and y
682 are normal.
683 */
684
685
Guido van Rossum9e720e31996-07-21 02:31:35 +0000686 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000687 double s,d;
Christian Heimes53876d92008-04-19 00:31:39 +0000688 double ax, ay;
689
690 SPECIAL_VALUE(z, sqrt_special_values);
691
692 if (z.real == 0. && z.imag == 0.) {
693 r.real = 0.;
694 r.imag = z.imag;
695 return r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000696 }
Christian Heimes53876d92008-04-19 00:31:39 +0000697
698 ax = fabs(z.real);
699 ay = fabs(z.imag);
700
701 if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
702 /* here we catch cases where hypot(ax, ay) is subnormal */
703 ax = ldexp(ax, CM_SCALE_UP);
704 s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
705 CM_SCALE_DOWN);
706 } else {
707 ax /= 8.;
708 s = 2.*sqrt(ax + hypot(ax, ay/8.));
709 }
710 d = ay/(2.*s);
711
712 if (z.real >= 0.) {
713 r.real = s;
714 r.imag = copysign(d, z.imag);
715 } else {
716 r.real = d;
717 r.imag = copysign(s, z.imag);
718 }
719 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000720 return r;
721}
722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000723PyDoc_STRVAR(c_sqrt_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000724"sqrt(x)\n"
725"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726"Return the square root of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000727
728
Tim Peters14e26402001-02-20 20:15:19 +0000729static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000730c_tan(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000731{
Christian Heimes53876d92008-04-19 00:31:39 +0000732 /* tan(z) = -i tanh(iz) */
733 Py_complex s, r;
734 s.real = -z.imag;
735 s.imag = z.real;
736 s = c_tanh(s);
737 r.real = s.imag;
738 r.imag = -s.real;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000739 return r;
740}
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(c_tan_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000743"tan(x)\n"
744"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000745"Return the tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000746
747
Christian Heimes53876d92008-04-19 00:31:39 +0000748/* tanh(infinity + i*y) needs to be dealt with specially */
Christian Heimesa342c012008-04-20 21:01:16 +0000749static Py_complex tanh_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000750
Tim Peters14e26402001-02-20 20:15:19 +0000751static Py_complex
Christian Heimes53876d92008-04-19 00:31:39 +0000752c_tanh(Py_complex z)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000753{
Christian Heimes53876d92008-04-19 00:31:39 +0000754 /* Formula:
755
756 tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
757 (1+tan(y)^2 tanh(x)^2)
758
759 To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
760 as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
761 by 4 exp(-2*x) instead, to avoid possible overflow in the
762 computation of cosh(x).
763
764 */
765
Guido van Rossum9e720e31996-07-21 02:31:35 +0000766 Py_complex r;
Christian Heimes53876d92008-04-19 00:31:39 +0000767 double tx, ty, cx, txty, denom;
768
769 /* special treatment for tanh(+/-inf + iy) if y is finite and
770 nonzero */
771 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
772 if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
773 && (z.imag != 0.)) {
774 if (z.real > 0) {
775 r.real = 1.0;
776 r.imag = copysign(0.,
777 2.*sin(z.imag)*cos(z.imag));
778 }
779 else {
780 r.real = -1.0;
781 r.imag = copysign(0.,
782 2.*sin(z.imag)*cos(z.imag));
783 }
784 }
785 else {
786 r = tanh_special_values[special_type(z.real)]
787 [special_type(z.imag)];
788 }
789 /* need to set errno = EDOM if z.imag is +/-infinity and
790 z.real is finite */
791 if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
792 errno = EDOM;
793 else
794 errno = 0;
795 return r;
796 }
797
798 /* danger of overflow in 2.*z.imag !*/
799 if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
800 r.real = copysign(1., z.real);
801 r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
802 } else {
803 tx = tanh(z.real);
804 ty = tan(z.imag);
805 cx = 1./cosh(z.real);
806 txty = tx*ty;
807 denom = 1. + txty*txty;
808 r.real = tx*(1.+ty*ty)/denom;
809 r.imag = ((ty/denom)*cx)*cx;
810 }
811 errno = 0;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000812 return r;
813}
814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000815PyDoc_STRVAR(c_tanh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000816"tanh(x)\n"
817"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818"Return the hyperbolic tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000819
Christian Heimes53876d92008-04-19 00:31:39 +0000820
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000821static PyObject *
822cmath_log(PyObject *self, PyObject *args)
823{
824 Py_complex x;
825 Py_complex y;
826
827 if (!PyArg_ParseTuple(args, "D|D", &x, &y))
828 return NULL;
829
830 errno = 0;
831 PyFPE_START_PROTECT("complex function", return 0)
832 x = c_log(x);
833 if (PyTuple_GET_SIZE(args) == 2)
834 x = c_quot(x, c_log(y));
835 PyFPE_END_PROTECT(x)
836 if (errno != 0)
837 return math_error();
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000838 return PyComplex_FromCComplex(x);
839}
840
841PyDoc_STRVAR(cmath_log_doc,
842"log(x[, base]) -> the logarithm of x to the given base.\n\
843If the base not specified, returns the natural logarithm (base e) of x.");
844
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000845
846/* And now the glue to make them available from Python: */
847
Roger E. Masse24070ca1996-12-09 22:59:53 +0000848static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000849math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000850{
851 if (errno == EDOM)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000852 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000853 else if (errno == ERANGE)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000854 PyErr_SetString(PyExc_OverflowError, "math range error");
855 else /* Unexpected math error */
Tim Peters14e26402001-02-20 20:15:19 +0000856 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000857 return NULL;
858}
859
Roger E. Masse24070ca1996-12-09 22:59:53 +0000860static PyObject *
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000861math_1(PyObject *args, Py_complex (*func)(Py_complex))
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000862{
Christian Heimes53876d92008-04-19 00:31:39 +0000863 Py_complex x,r ;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000864 if (!PyArg_ParseTuple(args, "D", &x))
865 return NULL;
866 errno = 0;
Christian Heimes53876d92008-04-19 00:31:39 +0000867 PyFPE_START_PROTECT("complex function", return 0);
868 r = (*func)(x);
869 PyFPE_END_PROTECT(r);
870 if (errno == EDOM) {
871 PyErr_SetString(PyExc_ValueError, "math domain error");
872 return NULL;
873 }
874 else if (errno == ERANGE) {
875 PyErr_SetString(PyExc_OverflowError, "math range error");
876 return NULL;
877 }
878 else {
879 return PyComplex_FromCComplex(r);
880 }
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000881}
882
883#define FUNC1(stubname, func) \
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000884 static PyObject * stubname(PyObject *self, PyObject *args) { \
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000885 return math_1(args, func); \
886 }
887
888FUNC1(cmath_acos, c_acos)
889FUNC1(cmath_acosh, c_acosh)
890FUNC1(cmath_asin, c_asin)
891FUNC1(cmath_asinh, c_asinh)
892FUNC1(cmath_atan, c_atan)
893FUNC1(cmath_atanh, c_atanh)
894FUNC1(cmath_cos, c_cos)
895FUNC1(cmath_cosh, c_cosh)
896FUNC1(cmath_exp, c_exp)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000897FUNC1(cmath_log10, c_log10)
898FUNC1(cmath_sin, c_sin)
899FUNC1(cmath_sinh, c_sinh)
900FUNC1(cmath_sqrt, c_sqrt)
901FUNC1(cmath_tan, c_tan)
902FUNC1(cmath_tanh, c_tanh)
903
Christian Heimes53876d92008-04-19 00:31:39 +0000904static PyObject *
905cmath_phase(PyObject *self, PyObject *args)
906{
907 Py_complex z;
908 double phi;
909 if (!PyArg_ParseTuple(args, "D:phase", &z))
910 return NULL;
911 errno = 0;
912 PyFPE_START_PROTECT("arg function", return 0)
913 phi = c_atan2(z);
914 PyFPE_END_PROTECT(r)
915 if (errno != 0)
916 return math_error();
917 else
918 return PyFloat_FromDouble(phi);
919}
920
921PyDoc_STRVAR(cmath_phase_doc,
922"phase(z) -> float\n\n\
923Return argument, also known as the phase angle, of a complex.");
924
925static PyObject *
926cmath_polar(PyObject *self, PyObject *args)
927{
928 Py_complex z;
929 double r, phi;
930 if (!PyArg_ParseTuple(args, "D:polar", &z))
931 return NULL;
932 PyFPE_START_PROTECT("polar function", return 0)
933 phi = c_atan2(z); /* should not cause any exception */
934 r = c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */
935 PyFPE_END_PROTECT(r)
936 if (errno != 0)
937 return math_error();
938 else
939 return Py_BuildValue("dd", r, phi);
940}
941
942PyDoc_STRVAR(cmath_polar_doc,
943"polar(z) -> r: float, phi: float\n\n\
944Convert a complex from rectangular coordinates to polar coordinates. r is\n\
945the distance from 0 and phi the phase angle.");
946
947/*
948 rect() isn't covered by the C99 standard, but it's not too hard to
949 figure out 'spirit of C99' rules for special value handing:
950
951 rect(x, t) should behave like exp(log(x) + it) for positive-signed x
952 rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x
953 rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0)
954 gives nan +- i0 with the sign of the imaginary part unspecified.
955
956*/
957
Christian Heimesa342c012008-04-20 21:01:16 +0000958static Py_complex rect_special_values[7][7];
Christian Heimes53876d92008-04-19 00:31:39 +0000959
960static PyObject *
961cmath_rect(PyObject *self, PyObject *args)
962{
963 Py_complex z;
964 double r, phi;
965 if (!PyArg_ParseTuple(args, "dd:rect", &r, &phi))
966 return NULL;
967 errno = 0;
968 PyFPE_START_PROTECT("rect function", return 0)
969
970 /* deal with special values */
971 if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
972 /* if r is +/-infinity and phi is finite but nonzero then
973 result is (+-INF +-INF i), but we need to compute cos(phi)
974 and sin(phi) to figure out the signs. */
975 if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
976 && (phi != 0.))) {
977 if (r > 0) {
978 z.real = copysign(INF, cos(phi));
979 z.imag = copysign(INF, sin(phi));
980 }
981 else {
982 z.real = -copysign(INF, cos(phi));
983 z.imag = -copysign(INF, sin(phi));
984 }
985 }
986 else {
987 z = rect_special_values[special_type(r)]
988 [special_type(phi)];
989 }
990 /* need to set errno = EDOM if r is a nonzero number and phi
991 is infinite */
992 if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
993 errno = EDOM;
994 else
995 errno = 0;
996 }
997 else {
998 z.real = r * cos(phi);
999 z.imag = r * sin(phi);
1000 errno = 0;
1001 }
1002
1003 PyFPE_END_PROTECT(z)
1004 if (errno != 0)
1005 return math_error();
1006 else
1007 return PyComplex_FromCComplex(z);
1008}
1009
1010PyDoc_STRVAR(cmath_rect_doc,
1011"rect(r, phi) -> z: complex\n\n\
1012Convert from polar coordinates to rectangular coordinates.");
1013
1014static PyObject *
1015cmath_isnan(PyObject *self, PyObject *args)
1016{
1017 Py_complex z;
1018 if (!PyArg_ParseTuple(args, "D:isnan", &z))
1019 return NULL;
1020 return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
1021}
1022
1023PyDoc_STRVAR(cmath_isnan_doc,
1024"isnan(z) -> bool\n\
1025Checks if the real or imaginary part of z not a number (NaN)");
1026
1027static PyObject *
1028cmath_isinf(PyObject *self, PyObject *args)
1029{
1030 Py_complex z;
1031 if (!PyArg_ParseTuple(args, "D:isnan", &z))
1032 return NULL;
1033 return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
1034 Py_IS_INFINITY(z.imag));
1035}
1036
1037PyDoc_STRVAR(cmath_isinf_doc,
1038"isinf(z) -> bool\n\
1039Checks if the real or imaginary part of z is infinite.");
1040
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042PyDoc_STRVAR(module_doc,
Tim Peters14e26402001-02-20 20:15:19 +00001043"This module is always available. It provides access to mathematical\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001044"functions for complex numbers.");
Guido van Rossumc6e22901998-12-04 19:26:43 +00001045
Roger E. Masse24070ca1996-12-09 22:59:53 +00001046static PyMethodDef cmath_methods[] = {
Tim Peters14e26402001-02-20 20:15:19 +00001047 {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
1048 {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
1049 {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
1050 {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
1051 {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
1052 {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
1053 {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
1054 {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
1055 {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
Christian Heimes53876d92008-04-19 00:31:39 +00001056 {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
1057 {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +00001058 {"log", cmath_log, METH_VARARGS, cmath_log_doc},
Tim Peters14e26402001-02-20 20:15:19 +00001059 {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
Christian Heimes53876d92008-04-19 00:31:39 +00001060 {"phase", cmath_phase, METH_VARARGS, cmath_phase_doc},
1061 {"polar", cmath_polar, METH_VARARGS, cmath_polar_doc},
1062 {"rect", cmath_rect, METH_VARARGS, cmath_rect_doc},
Tim Peters14e26402001-02-20 20:15:19 +00001063 {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
1064 {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
1065 {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
1066 {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
1067 {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001068 {NULL, NULL} /* sentinel */
1069};
1070
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001071PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001072initcmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001073{
Fred Drakef4e34842002-04-01 03:45:06 +00001074 PyObject *m;
Tim Peters14e26402001-02-20 20:15:19 +00001075
Guido van Rossumc6e22901998-12-04 19:26:43 +00001076 m = Py_InitModule3("cmath", cmath_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001077 if (m == NULL)
1078 return;
Fred Drakef4e34842002-04-01 03:45:06 +00001079
1080 PyModule_AddObject(m, "pi",
Christian Heimes53876d92008-04-19 00:31:39 +00001081 PyFloat_FromDouble(Py_MATH_PI));
1082 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
Christian Heimesa342c012008-04-20 21:01:16 +00001083
1084 /* initialize special value tables */
1085
1086#define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
1087#define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
1088
1089 INIT_SPECIAL_VALUES(acos_special_values, {
1090 C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
1091 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1092 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1093 C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
1094 C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
1095 C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
1096 C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
1097 })
1098
1099 INIT_SPECIAL_VALUES(acosh_special_values, {
1100 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1101 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1102 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1103 C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
1104 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1105 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1106 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1107 })
1108
1109 INIT_SPECIAL_VALUES(asinh_special_values, {
1110 C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
1111 C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
1112 C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
1113 C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
1114 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1115 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1116 C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
1117 })
1118
1119 INIT_SPECIAL_VALUES(atanh_special_values, {
1120 C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
1121 C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
1122 C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
1123 C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
1124 C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
1125 C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
1126 C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
1127 })
1128
1129 INIT_SPECIAL_VALUES(cosh_special_values, {
1130 C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
1131 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1132 C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
1133 C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
1134 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1135 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1136 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1137 })
1138
1139 INIT_SPECIAL_VALUES(exp_special_values, {
1140 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1141 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1142 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1143 C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
1144 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1145 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1146 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1147 })
1148
1149 INIT_SPECIAL_VALUES(log_special_values, {
1150 C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
1151 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1152 C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
1153 C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
1154 C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
1155 C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
1156 C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
1157 })
1158
1159 INIT_SPECIAL_VALUES(sinh_special_values, {
1160 C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
1161 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1162 C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
1163 C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
1164 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1165 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1166 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1167 })
1168
1169 INIT_SPECIAL_VALUES(sqrt_special_values, {
1170 C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
1171 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1172 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1173 C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
1174 C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
1175 C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
1176 C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
1177 })
1178
1179 INIT_SPECIAL_VALUES(tanh_special_values, {
1180 C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
1181 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1182 C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
1183 C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
1184 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1185 C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
1186 C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1187 })
1188
1189 INIT_SPECIAL_VALUES(rect_special_values, {
1190 C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
1191 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1192 C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
1193 C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
1194 C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
1195 C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
1196 C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
1197 })
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001198}