Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 3334 | 1 /* Complex math module */ |
| 2 | |
| 3 /* much code borrowed from mathmodule.c */ | |
| 4 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
5 #include "Python.h" |
| 3334 | 6 |
| 7 #include "mymath.h" | |
| 8 | |
| 9 #ifdef i860 | |
| 10 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ | |
| 11 #undef HUGE_VAL | |
| 12 #endif | |
| 13 | |
| 14 #ifdef HUGE_VAL | |
| 15 #define CHECK(x) if (errno != 0) ; \ | |
| 16 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ | |
| 17 else errno = ERANGE | |
| 18 #else | |
| 19 #define CHECK(x) /* Don't know how to check */ | |
| 20 #endif | |
| 21 | |
| 22 #ifndef M_PI | |
| 23 #define M_PI (3.141592653589793239) | |
| 24 #endif | |
| 25 | |
| 26 /* First, the C functions that do the real work */ | |
| 27 | |
| 28 /* constants */ | |
| 3771 | 29 static Py_complex c_1 = {1., 0.}; |
| 30 static Py_complex c_half = {0.5, 0.}; | |
| 31 static Py_complex c_i = {0., 1.}; | |
| 32 static Py_complex c_i2 = {0., 0.5}; | |
| 4600 | 33 #if 0 |
| 3771 | 34 static Py_complex c_mi = {0., -1.}; |
| 35 static Py_complex c_pi2 = {M_PI/2., 0.}; | |
| 4600 | 36 #endif |
| 3334 | 37 |
| 38 /* forward declarations */ | |
| 3771 | 39 staticforward Py_complex c_log(); |
| 40 staticforward Py_complex c_prodi(); | |
| 41 staticforward Py_complex c_sqrt(); | |
| 3334 | 42 |
| 43 | |
| 3771 | 44 static Py_complex c_acos(x) |
| 45 Py_complex x; | |
| 3334 | 46 { |
| 47 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i, | |
| 48 c_sqrt(c_diff(c_1,c_prod(x,x)))))))); | |
| 49 } | |
| 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 | 57 static Py_complex c_acosh(x) |
| 58 Py_complex x; | |
| 3334 | 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 | 65 } |
| 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 | 73 static Py_complex c_asin(x) |
| 74 Py_complex x; | |
| 3334 | 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 | 81 } |
| 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 | 89 static Py_complex c_asinh(x) |
| 90 Py_complex x; | |
| 3334 | 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 | 96 } |
| 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 | 104 static Py_complex c_atan(x) |
| 105 Py_complex x; | |
| 3334 | 106 { |
| 107 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); | |
| 108 } | |
| 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 | 116 static Py_complex c_atanh(x) |
| 117 Py_complex x; | |
| 3334 | 118 { |
| 119 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); | |
| 120 } | |
| 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 | 128 static Py_complex c_cos(x) |
| 129 Py_complex x; | |
| 3334 | 130 { |
| 3771 | 131 Py_complex r; |
| 3334 | 132 r.real = cos(x.real)*cosh(x.imag); |
| 133 r.imag = -sin(x.real)*sinh(x.imag); | |
| 134 return r; | |
| 135 } | |
| 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 | 143 static Py_complex c_cosh(x) |
| 144 Py_complex x; | |
| 3334 | 145 { |
| 3771 | 146 Py_complex r; |
| 3334 | 147 r.real = cos(x.imag)*cosh(x.real); |
| 148 r.imag = sin(x.imag)*sinh(x.real); | |
| 149 return r; | |
| 150 } | |
| 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 | 158 static Py_complex c_exp(x) |
| 159 Py_complex x; | |
| 3334 | 160 { |
| 3771 | 161 Py_complex r; |
| 3334 | 162 double l = exp(x.real); |
| 163 r.real = l*cos(x.imag); | |
| 164 r.imag = l*sin(x.imag); | |
| 165 return r; | |
| 166 } | |
| 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 | 174 static Py_complex c_log(x) |
| 175 Py_complex x; | |
| 3334 | 176 { |
| 3771 | 177 Py_complex r; |
| 3334 | 178 double l = hypot(x.real,x.imag); |
| 179 r.imag = atan2(x.imag, x.real); | |
| 180 r.real = log(l); | |
| 181 return r; | |
| 182 } | |
| 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 | 190 static Py_complex c_log10(x) |
| 191 Py_complex x; | |
| 3334 | 192 { |
| 3771 | 193 Py_complex r; |
| 3334 | 194 double l = hypot(x.real,x.imag); |
| 195 r.imag = atan2(x.imag, x.real)/log(10.); | |
| 196 r.real = log10(l); | |
| 197 return r; | |
| 198 } | |
| 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 | 207 static Py_complex c_prodi(x) |
| 208 Py_complex x; | |
| 3334 | 209 { |
| 3771 | 210 Py_complex r; |
| 3334 | 211 r.real = -x.imag; |
| 212 r.imag = x.real; | |
| 213 return r; | |
| 214 } | |
| 215 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
216 |
| 3771 | 217 static Py_complex c_sin(x) |
| 218 Py_complex x; | |
| 3334 | 219 { |
| 3771 | 220 Py_complex r; |
| 3334 | 221 r.real = sin(x.real)*cosh(x.imag); |
| 222 r.imag = cos(x.real)*sinh(x.imag); | |
| 223 return r; | |
| 224 } | |
| 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 | 232 static Py_complex c_sinh(x) |
| 233 Py_complex x; | |
| 3334 | 234 { |
| 3771 | 235 Py_complex r; |
| 3334 | 236 r.real = cos(x.imag)*sinh(x.real); |
| 237 r.imag = sin(x.imag)*cosh(x.real); | |
| 238 return r; | |
| 239 } | |
| 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 | 247 static Py_complex c_sqrt(x) |
| 248 Py_complex x; | |
| 3334 | 249 { |
| 3771 | 250 Py_complex r; |
| 3334 | 251 double s,d; |
| 252 if (x.real == 0. && x.imag == 0.) | |
| 253 r = x; | |
| 254 else { | |
| 255 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag))); | |
| 256 d = 0.5*x.imag/s; | |
| 257 if (x.real > 0.) { | |
| 258 r.real = s; | |
| 259 r.imag = d; | |
| 260 } | |
| 261 else if (x.imag >= 0.) { | |
| 262 r.real = d; | |
| 263 r.imag = s; | |
| 264 } | |
| 265 else { | |
| 266 r.real = -d; | |
| 267 r.imag = -s; | |
| 268 } | |
| 269 } | |
| 270 return r; | |
| 271 } | |
| 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 | 279 static Py_complex c_tan(x) |
| 280 Py_complex x; | |
| 3334 | 281 { |
| 3771 | 282 Py_complex r; |
| 3334 | 283 double sr,cr,shi,chi; |
| 284 double rs,is,rc,ic; | |
| 285 double d; | |
| 286 sr = sin(x.real); | |
| 287 cr = cos(x.real); | |
| 288 shi = sinh(x.imag); | |
| 289 chi = cosh(x.imag); | |
| 290 rs = sr*chi; | |
| 291 is = cr*shi; | |
| 292 rc = cr*chi; | |
| 293 ic = -sr*shi; | |
| 294 d = rc*rc + ic*ic; | |
| 295 r.real = (rs*rc+is*ic)/d; | |
| 296 r.imag = (is*rc-rs*ic)/d; | |
| 297 return r; | |
| 298 } | |
| 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 | 306 static Py_complex c_tanh(x) |
| 307 Py_complex x; | |
| 3334 | 308 { |
| 3771 | 309 Py_complex r; |
| 3334 | 310 double si,ci,shr,chr; |
| 311 double rs,is,rc,ic; | |
| 312 double d; | |
| 313 si = sin(x.imag); | |
| 314 ci = cos(x.imag); | |
| 315 shr = sinh(x.real); | |
| 316 chr = cosh(x.real); | |
| 317 rs = ci*shr; | |
| 318 is = si*chr; | |
| 319 rc = ci*chr; | |
| 320 ic = si*shr; | |
| 321 d = rc*rc + ic*ic; | |
| 322 r.real = (rs*rc+is*ic)/d; | |
| 323 r.imag = (is*rc-rs*ic)/d; | |
| 324 return r; | |
| 325 } | |
| 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 | 332 |
| 333 /* And now the glue to make them available from Python: */ | |
| 334 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
335 static PyObject * |
| 3334 | 336 math_error() |
| 337 { | |
| 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 | 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 | 344 return NULL; |
| 345 } | |
| 346 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
347 static PyObject * |
| 3334 | 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 | 351 { |
| 3771 | 352 Py_complex x; |
| 3334 | 353 if (!PyArg_ParseTuple(args, "D", &x)) |
| 354 return NULL; | |
| 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 | 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 | 359 CHECK(x.real); |
| 360 CHECK(x.imag); | |
| 361 if (errno != 0) | |
| 362 return math_error(); | |
| 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 | 365 } |
| 366 | |
| 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 | 369 return math_1(args, func); \ |
| 370 } | |
| 371 | |
| 372 FUNC1(cmath_acos, c_acos) | |
| 373 FUNC1(cmath_acosh, c_acosh) | |
| 374 FUNC1(cmath_asin, c_asin) | |
| 375 FUNC1(cmath_asinh, c_asinh) | |
| 376 FUNC1(cmath_atan, c_atan) | |
| 377 FUNC1(cmath_atanh, c_atanh) | |
| 378 FUNC1(cmath_cos, c_cos) | |
| 379 FUNC1(cmath_cosh, c_cosh) | |
| 380 FUNC1(cmath_exp, c_exp) | |
| 381 FUNC1(cmath_log, c_log) | |
| 382 FUNC1(cmath_log10, c_log10) | |
| 383 FUNC1(cmath_sin, c_sin) | |
| 384 FUNC1(cmath_sinh, c_sinh) | |
| 385 FUNC1(cmath_sqrt, c_sqrt) | |
| 386 FUNC1(cmath_tan, c_tan) | |
| 387 FUNC1(cmath_tanh, c_tanh) | |
| 388 | |
| 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 | 412 {NULL, NULL} /* sentinel */ |
| 413 }; | |
| 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 | 416 initcmath() |
| 417 { | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
418 PyObject *m, *d, *v; |
| 3334 | 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 | 427 } |
