blob: 78b9dd5f8fad55ec2122e3239bd56fdbcd3547f9 [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"
Guido van Rossum71aa32f1996-01-12 01:34:57 +00006
Guido van Rossum71aa32f1996-01-12 01:34:57 +00007#ifndef M_PI
8#define M_PI (3.141592653589793239)
9#endif
10
11/* First, the C functions that do the real work */
12
13/* constants */
Tim Peters8cb82bd2001-02-20 20:36:38 +000014static Py_complex c_one = {1., 0.};
Guido van Rossum9e720e31996-07-21 02:31:35 +000015static Py_complex c_half = {0.5, 0.};
16static Py_complex c_i = {0., 1.};
Tim Peters8cb82bd2001-02-20 20:36:38 +000017static Py_complex c_halfi = {0., 0.5};
Guido van Rossum71aa32f1996-01-12 01:34:57 +000018
19/* forward declarations */
Jeremy Hylton938ace62002-07-17 16:30:39 +000020static Py_complex c_log(Py_complex);
21static Py_complex c_prodi(Py_complex);
22static Py_complex c_sqrt(Py_complex);
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000023static PyObject * math_error(void);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000024
25
Tim Peters14e26402001-02-20 20:15:19 +000026static Py_complex
27c_acos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000028{
29 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
Tim Peters8cb82bd2001-02-20 20:36:38 +000030 c_sqrt(c_diff(c_one,c_prod(x,x))))))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +000031}
32
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033PyDoc_STRVAR(c_acos_doc,
Tim Peters14e26402001-02-20 20:15:19 +000034"acos(x)\n"
35"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000036"Return the arc cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +000037
38
Tim Peters14e26402001-02-20 20:15:19 +000039static Py_complex
40c_acosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000041{
Guido van Rossumf385c5e2000-06-30 02:29:22 +000042 Py_complex z;
43 z = c_sqrt(c_half);
Tim Peters8cb82bd2001-02-20 20:36:38 +000044 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_one)),
45 c_sqrt(c_diff(x,c_one)))));
Guido van Rossumf385c5e2000-06-30 02:29:22 +000046 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000047}
48
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049PyDoc_STRVAR(c_acosh_doc,
Tim Peters14e26402001-02-20 20:15:19 +000050"acosh(x)\n"
51"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052"Return the hyperbolic arccosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +000053
54
Tim Peters14e26402001-02-20 20:15:19 +000055static Py_complex
56c_asin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000057{
Tim Petersedf22102001-02-21 03:22:39 +000058 /* -i * log[(sqrt(1-x**2) + i*x] */
59 const Py_complex squared = c_prod(x, x);
60 const Py_complex sqrt_1_minus_x_sq = c_sqrt(c_diff(c_one, squared));
Tim Peters1c8e1f02001-02-22 19:51:56 +000061 return c_neg(c_prodi(c_log(
62 c_sum(sqrt_1_minus_x_sq, c_prodi(x))
63 ) ) );
Guido van Rossum71aa32f1996-01-12 01:34:57 +000064}
65
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(c_asin_doc,
Tim Peters14e26402001-02-20 20:15:19 +000067"asin(x)\n"
68"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069"Return the arc sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +000070
71
Tim Peters14e26402001-02-20 20:15:19 +000072static Py_complex
73c_asinh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000074{
Guido van Rossum11a50711999-01-14 19:11:11 +000075 Py_complex z;
Tim Petersedf22102001-02-21 03:22:39 +000076 z = c_sqrt(c_half);
77 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x, c_i)),
78 c_sqrt(c_diff(x, c_i)))));
79 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000080}
81
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(c_asinh_doc,
Tim Peters14e26402001-02-20 20:15:19 +000083"asinh(x)\n"
84"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085"Return the hyperbolic arc sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +000086
87
Tim Peters14e26402001-02-20 20:15:19 +000088static Py_complex
89c_atan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000090{
Tim Peters8cb82bd2001-02-20 20:36:38 +000091 return c_prod(c_halfi,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +000092}
93
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094PyDoc_STRVAR(c_atan_doc,
Tim Peters14e26402001-02-20 20:15:19 +000095"atan(x)\n"
96"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097"Return the arc tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +000098
99
Tim Peters14e26402001-02-20 20:15:19 +0000100static Py_complex
101c_atanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000102{
Tim Peters8cb82bd2001-02-20 20:36:38 +0000103 return c_prod(c_half,c_log(c_quot(c_sum(c_one,x),c_diff(c_one,x))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000104}
105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(c_atanh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000107"atanh(x)\n"
108"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109"Return the hyperbolic arc tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000110
111
Tim Peters14e26402001-02-20 20:15:19 +0000112static Py_complex
113c_cos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000114{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000115 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000116 r.real = cos(x.real)*cosh(x.imag);
117 r.imag = -sin(x.real)*sinh(x.imag);
118 return r;
119}
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(c_cos_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000122"cos(x)\n"
123"n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124"Return the cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000125
126
Tim Peters14e26402001-02-20 20:15:19 +0000127static Py_complex
128c_cosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000129{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000130 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000131 r.real = cos(x.imag)*cosh(x.real);
132 r.imag = sin(x.imag)*sinh(x.real);
133 return r;
134}
135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000136PyDoc_STRVAR(c_cosh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000137"cosh(x)\n"
138"n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139"Return the hyperbolic cosine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000140
141
Tim Peters14e26402001-02-20 20:15:19 +0000142static Py_complex
143c_exp(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000144{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000145 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000146 double l = exp(x.real);
147 r.real = l*cos(x.imag);
148 r.imag = l*sin(x.imag);
149 return r;
150}
151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152PyDoc_STRVAR(c_exp_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000153"exp(x)\n"
154"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155"Return the exponential value e**x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000156
157
Tim Peters14e26402001-02-20 20:15:19 +0000158static Py_complex
159c_log(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000160{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000161 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000162 double l = hypot(x.real,x.imag);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000163 r.imag = atan2(x.imag, x.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000164 r.real = log(l);
165 return r;
166}
167
Guido van Rossumc6e22901998-12-04 19:26:43 +0000168
Tim Peters14e26402001-02-20 20:15:19 +0000169static Py_complex
170c_log10(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000171{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000172 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000173 double l = hypot(x.real,x.imag);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000174 r.imag = atan2(x.imag, x.real)/log(10.);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000175 r.real = log10(l);
176 return r;
177}
178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000179PyDoc_STRVAR(c_log10_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000180"log10(x)\n"
181"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182"Return the base-10 logarithm of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000183
184
185/* internal function not available from Python */
Tim Peters14e26402001-02-20 20:15:19 +0000186static Py_complex
187c_prodi(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000188{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000189 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000190 r.real = -x.imag;
191 r.imag = x.real;
192 return r;
193}
194
Guido van Rossumc6e22901998-12-04 19:26:43 +0000195
Tim Peters14e26402001-02-20 20:15:19 +0000196static Py_complex
197c_sin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000198{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000199 Py_complex r;
Tim Peters14e26402001-02-20 20:15:19 +0000200 r.real = sin(x.real) * cosh(x.imag);
201 r.imag = cos(x.real) * sinh(x.imag);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000202 return r;
203}
204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(c_sin_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000206"sin(x)\n"
207"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208"Return the sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000209
210
Tim Peters14e26402001-02-20 20:15:19 +0000211static Py_complex
212c_sinh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000213{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000214 Py_complex r;
Tim Peters14e26402001-02-20 20:15:19 +0000215 r.real = cos(x.imag) * sinh(x.real);
216 r.imag = sin(x.imag) * cosh(x.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000217 return r;
218}
219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220PyDoc_STRVAR(c_sinh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000221"sinh(x)\n"
222"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000223"Return the hyperbolic sine of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000224
225
Tim Peters14e26402001-02-20 20:15:19 +0000226static Py_complex
227c_sqrt(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000228{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000229 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000230 double s,d;
231 if (x.real == 0. && x.imag == 0.)
232 r = x;
233 else {
234 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
235 d = 0.5*x.imag/s;
236 if (x.real > 0.) {
237 r.real = s;
238 r.imag = d;
239 }
240 else if (x.imag >= 0.) {
241 r.real = d;
242 r.imag = s;
243 }
244 else {
245 r.real = -d;
246 r.imag = -s;
247 }
248 }
249 return r;
250}
251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(c_sqrt_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000253"sqrt(x)\n"
254"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255"Return the square root of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000256
257
Tim Peters14e26402001-02-20 20:15:19 +0000258static Py_complex
259c_tan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000260{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000261 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000262 double sr,cr,shi,chi;
263 double rs,is,rc,ic;
264 double d;
265 sr = sin(x.real);
266 cr = cos(x.real);
267 shi = sinh(x.imag);
268 chi = cosh(x.imag);
Tim Peters14e26402001-02-20 20:15:19 +0000269 rs = sr * chi;
270 is = cr * shi;
271 rc = cr * chi;
272 ic = -sr * shi;
273 d = rc*rc + ic * ic;
274 r.real = (rs*rc + is*ic) / d;
275 r.imag = (is*rc - rs*ic) / d;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000276 return r;
277}
278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(c_tan_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000280"tan(x)\n"
281"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282"Return the tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000283
284
Tim Peters14e26402001-02-20 20:15:19 +0000285static Py_complex
286c_tanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000287{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000288 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000289 double si,ci,shr,chr;
290 double rs,is,rc,ic;
291 double d;
292 si = sin(x.imag);
293 ci = cos(x.imag);
294 shr = sinh(x.real);
295 chr = cosh(x.real);
Tim Peters14e26402001-02-20 20:15:19 +0000296 rs = ci * shr;
297 is = si * chr;
298 rc = ci * chr;
299 ic = si * shr;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000300 d = rc*rc + ic*ic;
Tim Peters14e26402001-02-20 20:15:19 +0000301 r.real = (rs*rc + is*ic) / d;
302 r.imag = (is*rc - rs*ic) / d;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000303 return r;
304}
305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000306PyDoc_STRVAR(c_tanh_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000307"tanh(x)\n"
308"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309"Return the hyperbolic tangent of x.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000310
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000311static PyObject *
312cmath_log(PyObject *self, PyObject *args)
313{
314 Py_complex x;
315 Py_complex y;
316
317 if (!PyArg_ParseTuple(args, "D|D", &x, &y))
318 return NULL;
319
320 errno = 0;
321 PyFPE_START_PROTECT("complex function", return 0)
322 x = c_log(x);
323 if (PyTuple_GET_SIZE(args) == 2)
324 x = c_quot(x, c_log(y));
325 PyFPE_END_PROTECT(x)
326 if (errno != 0)
327 return math_error();
328 Py_ADJUST_ERANGE2(x.real, x.imag);
329 return PyComplex_FromCComplex(x);
330}
331
332PyDoc_STRVAR(cmath_log_doc,
333"log(x[, base]) -> the logarithm of x to the given base.\n\
334If the base not specified, returns the natural logarithm (base e) of x.");
335
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000336
337/* And now the glue to make them available from Python: */
338
Roger E. Masse24070ca1996-12-09 22:59:53 +0000339static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000340math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000341{
342 if (errno == EDOM)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000343 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000344 else if (errno == ERANGE)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000345 PyErr_SetString(PyExc_OverflowError, "math range error");
346 else /* Unexpected math error */
Tim Peters14e26402001-02-20 20:15:19 +0000347 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000348 return NULL;
349}
350
Roger E. Masse24070ca1996-12-09 22:59:53 +0000351static PyObject *
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000352math_1(PyObject *args, Py_complex (*func)(Py_complex))
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000353{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000354 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000355 if (!PyArg_ParseTuple(args, "D", &x))
356 return NULL;
357 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000358 PyFPE_START_PROTECT("complex function", return 0)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000359 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +0000360 PyFPE_END_PROTECT(x)
Tim Petersdc5a5082002-03-09 04:58:24 +0000361 Py_ADJUST_ERANGE2(x.real, x.imag);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000362 if (errno != 0)
363 return math_error();
364 else
Roger E. Masse24070ca1996-12-09 22:59:53 +0000365 return PyComplex_FromCComplex(x);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000366}
367
368#define FUNC1(stubname, func) \
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000369 static PyObject * stubname(PyObject *self, PyObject *args) { \
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000370 return math_1(args, func); \
371 }
372
373FUNC1(cmath_acos, c_acos)
374FUNC1(cmath_acosh, c_acosh)
375FUNC1(cmath_asin, c_asin)
376FUNC1(cmath_asinh, c_asinh)
377FUNC1(cmath_atan, c_atan)
378FUNC1(cmath_atanh, c_atanh)
379FUNC1(cmath_cos, c_cos)
380FUNC1(cmath_cosh, c_cosh)
381FUNC1(cmath_exp, c_exp)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000382FUNC1(cmath_log10, c_log10)
383FUNC1(cmath_sin, c_sin)
384FUNC1(cmath_sinh, c_sinh)
385FUNC1(cmath_sqrt, c_sqrt)
386FUNC1(cmath_tan, c_tan)
387FUNC1(cmath_tanh, c_tanh)
388
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(module_doc,
Tim Peters14e26402001-02-20 20:15:19 +0000391"This module is always available. It provides access to mathematical\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392"functions for complex numbers.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000393
Roger E. Masse24070ca1996-12-09 22:59:53 +0000394static PyMethodDef cmath_methods[] = {
Tim Peters14e26402001-02-20 20:15:19 +0000395 {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
396 {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
397 {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
398 {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
399 {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
400 {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
401 {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
402 {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
403 {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000404 {"log", cmath_log, METH_VARARGS, cmath_log_doc},
Tim Peters14e26402001-02-20 20:15:19 +0000405 {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
406 {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
407 {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
408 {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
409 {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
410 {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000411 {NULL, NULL} /* sentinel */
412};
413
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000414PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000415initcmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000416{
Fred Drakef4e34842002-04-01 03:45:06 +0000417 PyObject *m;
Tim Peters14e26402001-02-20 20:15:19 +0000418
Guido van Rossumc6e22901998-12-04 19:26:43 +0000419 m = Py_InitModule3("cmath", cmath_methods, module_doc);
Fred Drakef4e34842002-04-01 03:45:06 +0000420
421 PyModule_AddObject(m, "pi",
422 PyFloat_FromDouble(atan(1.0) * 4.0));
423 PyModule_AddObject(m, "e", PyFloat_FromDouble(exp(1.0)));
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000424}