annotate Modules/cmathmodule.c @ 13184:efecbbf72765 legacy-trunk

Describe a bit about writing test cases for Python...
author Skip Montanaro <skip@pobox.com>
date Fri, 30 Jun 2000 06:08:35 +0000
parents b78b996e8ebc
children a89f7f268de4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Complex math module */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* much code borrowed from mathmodule.c */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
5 #include "Python.h"
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "mymath.h"
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #ifdef i860
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #undef HUGE_VAL
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #ifdef HUGE_VAL
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #define CHECK(x) if (errno != 0) ; \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 else errno = ERANGE
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #else
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #define CHECK(x) /* Don't know how to check */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #ifndef M_PI
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 #define M_PI (3.141592653589793239)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 /* First, the C functions that do the real work */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 /* constants */
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
29 static Py_complex c_1 = {1., 0.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
30 static Py_complex c_half = {0.5, 0.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
31 static Py_complex c_i = {0., 1.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
32 static Py_complex c_i2 = {0., 0.5};
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 3771
diff changeset
33 #if 0
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
34 static Py_complex c_mi = {0., -1.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
35 static Py_complex c_pi2 = {M_PI/2., 0.};
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 3771
diff changeset
36 #endif
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 /* forward declarations */
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
39 staticforward Py_complex c_log();
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
40 staticforward Py_complex c_prodi();
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
41 staticforward Py_complex c_sqrt();
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
44 static Py_complex c_acos(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
45 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 c_sqrt(c_diff(c_1,c_prod(x,x))))))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
51 static char c_acos_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
52 "acos(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
53 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
54 Return the arc cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
55
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
56
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
57 static Py_complex c_acosh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
58 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
60 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
61 z = c_sqrt(c_half);
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
62 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_1)),
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
63 c_sqrt(c_diff(x,c_1)))));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
64 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
67 static char c_acosh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
68 "acosh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
69 \n\
10403
9c6da30ae675 Docstring fix: acosh() returns the hyperbolic arccosine, not the
Fred Drake <fdrake@acm.org>
parents: 9886
diff changeset
70 Return the hyperbolic arccosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
71
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
72
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
73 static Py_complex c_asin(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
74 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
76 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
77 z = c_sqrt(c_half);
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
78 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_i)),
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
79 c_sqrt(c_diff(x,c_i)))));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
80 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
83 static char c_asin_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
84 "asin(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
85 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
86 Return the arc sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
87
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
88
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
89 static Py_complex c_asinh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
90 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 {
9886
fae6d24b897b Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
92 /* Break up long expression for WATCOM */
fae6d24b897b Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
93 Py_complex z;
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
94 z = c_sum(c_1,c_prod(x, x));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
95 return c_log(c_sum(c_sqrt(z), x));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
98 static char c_asinh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
99 "asinh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
100 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
101 Return the hyperbolic arc sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
102
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
103
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
104 static Py_complex c_atan(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
105 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
110 static char c_atan_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
111 "atan(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
112 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
113 Return the arc tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
114
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
115
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
116 static Py_complex c_atanh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
117 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
122 static char c_atanh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
123 "atanh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
124 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
125 Return the hyperbolic arc tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
126
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
127
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
128 static Py_complex c_cos(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
129 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
131 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 r.real = cos(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 r.imag = -sin(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
137 static char c_cos_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
138 "cos(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
139 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
140 Return the cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
141
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
142
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
143 static Py_complex c_cosh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
144 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
146 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 r.real = cos(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 r.imag = sin(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
152 static char c_cosh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
153 "cosh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
154 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
155 Return the hyperbolic cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
156
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
157
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
158 static Py_complex c_exp(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
159 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
161 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 double l = exp(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 r.real = l*cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 r.imag = l*sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
168 static char c_exp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
169 "exp(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
170 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
171 Return the exponential value e**x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
172
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
173
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
174 static Py_complex c_log(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
175 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
177 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 r.imag = atan2(x.imag, x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 r.real = log(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
184 static char c_log_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
185 "log(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
186 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
187 Return the natural logarithm of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
188
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
189
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
190 static Py_complex c_log10(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
191 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
193 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 r.imag = atan2(x.imag, x.real)/log(10.);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 r.real = log10(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
200 static char c_log10_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
201 "log10(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
202 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
203 Return the base-10 logarithm of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
204
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
205
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
206 /* internal function not available from Python */
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
207 static Py_complex c_prodi(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
208 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
210 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 r.real = -x.imag;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 r.imag = x.real;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
216
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
217 static Py_complex c_sin(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
218 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
220 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 r.real = sin(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 r.imag = cos(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
226 static char c_sin_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
227 "sin(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
228 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
229 Return the sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
230
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
231
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
232 static Py_complex c_sinh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
233 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
235 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 r.real = cos(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 r.imag = sin(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
241 static char c_sinh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
242 "sinh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
243 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
244 Return the hyperbolic sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
245
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
246
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
247 static Py_complex c_sqrt(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
248 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
250 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 double s,d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 if (x.real == 0. && x.imag == 0.)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 r = x;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 d = 0.5*x.imag/s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if (x.real > 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 r.real = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 r.imag = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 else if (x.imag >= 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 r.real = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 r.imag = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 r.real = -d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 r.imag = -s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
273 static char c_sqrt_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
274 "sqrt(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
275 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
276 Return the square root of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
277
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
278
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
279 static Py_complex c_tan(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
280 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
282 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 double sr,cr,shi,chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 sr = sin(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 cr = cos(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 shi = sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 chi = cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 rs = sr*chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 is = cr*shi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 rc = cr*chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 ic = -sr*shi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 d = rc*rc + ic*ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 r.real = (rs*rc+is*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 r.imag = (is*rc-rs*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
300 static char c_tan_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
301 "tan(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
302 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
303 Return the tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
304
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
305
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
306 static Py_complex c_tanh(x)
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
307 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
309 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 double si,ci,shr,chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 si = sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 ci = cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 shr = sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 chr = cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 rs = ci*shr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 is = si*chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 rc = ci*chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 ic = si*shr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 d = rc*rc + ic*ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 r.real = (rs*rc+is*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 r.imag = (is*rc-rs*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
327 static char c_tanh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
328 "tanh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
329 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
330 Return the hyperbolic tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
331
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 /* And now the glue to make them available from Python: */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
335 static PyObject *
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 math_error()
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 if (errno == EDOM)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
339 PyErr_SetString(PyExc_ValueError, "math domain error");
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 else if (errno == ERANGE)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
341 PyErr_SetString(PyExc_OverflowError, "math range error");
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
342 else /* Unexpected math error */
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
343 PyErr_SetFromErrno(PyExc_ValueError);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
347 static PyObject *
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 math_1(args, func)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
349 PyObject *args;
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
350 Py_complex (*func) Py_FPROTO((Py_complex));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
352 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 if (!PyArg_ParseTuple(args, "D", &x))
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4649
diff changeset
356 PyFPE_START_PROTECT("complex function", return 0)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
358 PyFPE_END_PROTECT(x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 CHECK(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 CHECK(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 if (errno != 0)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 return math_error();
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 else
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
364 return PyComplex_FromCComplex(x);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 #define FUNC1(stubname, func) \
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
368 static PyObject * stubname(self, args) PyObject *self, *args; { \
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 return math_1(args, func); \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
371
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 FUNC1(cmath_acos, c_acos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 FUNC1(cmath_acosh, c_acosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 FUNC1(cmath_asin, c_asin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 FUNC1(cmath_asinh, c_asinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 FUNC1(cmath_atan, c_atan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 FUNC1(cmath_atanh, c_atanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 FUNC1(cmath_cos, c_cos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 FUNC1(cmath_cosh, c_cosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 FUNC1(cmath_exp, c_exp)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 FUNC1(cmath_log, c_log)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 FUNC1(cmath_log10, c_log10)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 FUNC1(cmath_sin, c_sin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 FUNC1(cmath_sinh, c_sinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 FUNC1(cmath_sqrt, c_sqrt)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 FUNC1(cmath_tan, c_tan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 FUNC1(cmath_tanh, c_tanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
390 static char module_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
391 "This module is always available. It provides access to mathematical\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
392 functions for complex numbers.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
393
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
394
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
395 static PyMethodDef cmath_methods[] = {
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
396 {"acos", cmath_acos, 1, c_acos_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
397 {"acosh", cmath_acosh, 1, c_acosh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
398 {"asin", cmath_asin, 1, c_asin_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
399 {"asinh", cmath_asinh, 1, c_asinh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
400 {"atan", cmath_atan, 1, c_atan_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
401 {"atanh", cmath_atanh, 1, c_atanh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
402 {"cos", cmath_cos, 1, c_cos_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
403 {"cosh", cmath_cosh, 1, c_cosh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
404 {"exp", cmath_exp, 1, c_exp_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
405 {"log", cmath_log, 1, c_log_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
406 {"log10", cmath_log10, 1, c_log10_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
407 {"sin", cmath_sin, 1, c_sin_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
408 {"sinh", cmath_sinh, 1, c_sinh_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
409 {"sqrt", cmath_sqrt, 1, c_sqrt_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
410 {"tan", cmath_tan, 1, c_tan_doc},
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
411 {"tanh", cmath_tanh, 1, c_tanh_doc},
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 {NULL, NULL} /* sentinel */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 };
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
415 DL_EXPORT(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 initcmath()
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 {
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
418 PyObject *m, *d, *v;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
420 m = Py_InitModule3("cmath", cmath_methods, module_doc);
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
421 d = PyModule_GetDict(m);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
422 PyDict_SetItemString(d, "pi",
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
423 v = PyFloat_FromDouble(atan(1.0) * 4.0));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
424 Py_DECREF(v);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
425 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
426 Py_DECREF(v);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 }