Mercurial > cpython-lru_cache-weakref
annotate Modules/cmathmodule.c @ 10072:ecebcd84df37 legacy-trunk
put the preferences file in a folder called "Python" inside the prefs folder, just like the IDE does -- jvr
| author | Just van Rossum <just@lettererror.com> |
|---|---|
| date | Tue, 02 Feb 1999 15:49:03 +0000 |
| parents | fae6d24b897b |
| children | 9c6da30ae675 |
| 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 { |
| 60 return c_log(c_sum(x,c_prod(c_i, | |
| 61 c_sqrt(c_diff(c_1,c_prod(x,x)))))); | |
| 62 } | |
| 63 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
64 static char c_acosh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
65 "acosh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
66 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
67 Return the hyperbolic cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
68 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
69 |
| 3771 | 70 static Py_complex c_asin(x) |
| 71 Py_complex x; | |
| 3334 | 72 { |
| 73 return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x), | |
| 74 c_sqrt(c_diff(c_1,c_prod(x,x))))))); | |
| 75 } | |
| 76 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
77 static char c_asin_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
78 "asin(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
79 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
80 Return the arc sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
81 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
82 |
| 3771 | 83 static Py_complex c_asinh(x) |
| 84 Py_complex x; | |
| 3334 | 85 { |
|
9886
fae6d24b897b
Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents:
9593
diff
changeset
|
86 /* 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
|
87 Py_complex z; |
|
fae6d24b897b
Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents:
9593
diff
changeset
|
88 z = c_sum(c_1,c_prod(x,x)); |
|
fae6d24b897b
Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents:
9593
diff
changeset
|
89 z = c_diff(c_sqrt(z),x); |
|
fae6d24b897b
Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents:
9593
diff
changeset
|
90 return c_neg(c_log(z)); |
| 3334 | 91 } |
| 92 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
93 static char c_asinh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
94 "asinh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
95 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
96 Return the hyperbolic arc sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
97 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
98 |
| 3771 | 99 static Py_complex c_atan(x) |
| 100 Py_complex x; | |
| 3334 | 101 { |
| 102 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); | |
| 103 } | |
| 104 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
105 static char c_atan_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
106 "atan(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
107 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
108 Return the arc tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
109 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
110 |
| 3771 | 111 static Py_complex c_atanh(x) |
| 112 Py_complex x; | |
| 3334 | 113 { |
| 114 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); | |
| 115 } | |
| 116 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
117 static char c_atanh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
118 "atanh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
119 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
120 Return the hyperbolic arc tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
121 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
122 |
| 3771 | 123 static Py_complex c_cos(x) |
| 124 Py_complex x; | |
| 3334 | 125 { |
| 3771 | 126 Py_complex r; |
| 3334 | 127 r.real = cos(x.real)*cosh(x.imag); |
| 128 r.imag = -sin(x.real)*sinh(x.imag); | |
| 129 return r; | |
| 130 } | |
| 131 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
132 static char c_cos_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
133 "cos(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
134 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
135 Return the cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
136 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
137 |
| 3771 | 138 static Py_complex c_cosh(x) |
| 139 Py_complex x; | |
| 3334 | 140 { |
| 3771 | 141 Py_complex r; |
| 3334 | 142 r.real = cos(x.imag)*cosh(x.real); |
| 143 r.imag = sin(x.imag)*sinh(x.real); | |
| 144 return r; | |
| 145 } | |
| 146 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
147 static char c_cosh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
148 "cosh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
149 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
150 Return the hyperbolic cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
151 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
152 |
| 3771 | 153 static Py_complex c_exp(x) |
| 154 Py_complex x; | |
| 3334 | 155 { |
| 3771 | 156 Py_complex r; |
| 3334 | 157 double l = exp(x.real); |
| 158 r.real = l*cos(x.imag); | |
| 159 r.imag = l*sin(x.imag); | |
| 160 return r; | |
| 161 } | |
| 162 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
163 static char c_exp_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
164 "exp(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
165 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
166 Return the exponential value e**x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
167 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
168 |
| 3771 | 169 static Py_complex c_log(x) |
| 170 Py_complex x; | |
| 3334 | 171 { |
| 3771 | 172 Py_complex r; |
| 3334 | 173 double l = hypot(x.real,x.imag); |
| 174 r.imag = atan2(x.imag, x.real); | |
| 175 r.real = log(l); | |
| 176 return r; | |
| 177 } | |
| 178 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
179 static char c_log_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
180 "log(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
181 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
182 Return the natural logarithm of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
183 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
184 |
| 3771 | 185 static Py_complex c_log10(x) |
| 186 Py_complex x; | |
| 3334 | 187 { |
| 3771 | 188 Py_complex r; |
| 3334 | 189 double l = hypot(x.real,x.imag); |
| 190 r.imag = atan2(x.imag, x.real)/log(10.); | |
| 191 r.real = log10(l); | |
| 192 return r; | |
| 193 } | |
| 194 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
195 static char c_log10_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
196 "log10(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
197 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
198 Return the base-10 logarithm of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
199 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
200 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
201 /* internal function not available from Python */ |
| 3771 | 202 static Py_complex c_prodi(x) |
| 203 Py_complex x; | |
| 3334 | 204 { |
| 3771 | 205 Py_complex r; |
| 3334 | 206 r.real = -x.imag; |
| 207 r.imag = x.real; | |
| 208 return r; | |
| 209 } | |
| 210 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
211 |
| 3771 | 212 static Py_complex c_sin(x) |
| 213 Py_complex x; | |
| 3334 | 214 { |
| 3771 | 215 Py_complex r; |
| 3334 | 216 r.real = sin(x.real)*cosh(x.imag); |
| 217 r.imag = cos(x.real)*sinh(x.imag); | |
| 218 return r; | |
| 219 } | |
| 220 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
221 static char c_sin_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
222 "sin(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
223 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
224 Return the sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
225 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
226 |
| 3771 | 227 static Py_complex c_sinh(x) |
| 228 Py_complex x; | |
| 3334 | 229 { |
| 3771 | 230 Py_complex r; |
| 3334 | 231 r.real = cos(x.imag)*sinh(x.real); |
| 232 r.imag = sin(x.imag)*cosh(x.real); | |
| 233 return r; | |
| 234 } | |
| 235 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
236 static char c_sinh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
237 "sinh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
238 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
239 Return the hyperbolic sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
240 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
241 |
| 3771 | 242 static Py_complex c_sqrt(x) |
| 243 Py_complex x; | |
| 3334 | 244 { |
| 3771 | 245 Py_complex r; |
| 3334 | 246 double s,d; |
| 247 if (x.real == 0. && x.imag == 0.) | |
| 248 r = x; | |
| 249 else { | |
| 250 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag))); | |
| 251 d = 0.5*x.imag/s; | |
| 252 if (x.real > 0.) { | |
| 253 r.real = s; | |
| 254 r.imag = d; | |
| 255 } | |
| 256 else if (x.imag >= 0.) { | |
| 257 r.real = d; | |
| 258 r.imag = s; | |
| 259 } | |
| 260 else { | |
| 261 r.real = -d; | |
| 262 r.imag = -s; | |
| 263 } | |
| 264 } | |
| 265 return r; | |
| 266 } | |
| 267 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
268 static char c_sqrt_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
269 "sqrt(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
270 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
271 Return the square root of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
272 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
273 |
| 3771 | 274 static Py_complex c_tan(x) |
| 275 Py_complex x; | |
| 3334 | 276 { |
| 3771 | 277 Py_complex r; |
| 3334 | 278 double sr,cr,shi,chi; |
| 279 double rs,is,rc,ic; | |
| 280 double d; | |
| 281 sr = sin(x.real); | |
| 282 cr = cos(x.real); | |
| 283 shi = sinh(x.imag); | |
| 284 chi = cosh(x.imag); | |
| 285 rs = sr*chi; | |
| 286 is = cr*shi; | |
| 287 rc = cr*chi; | |
| 288 ic = -sr*shi; | |
| 289 d = rc*rc + ic*ic; | |
| 290 r.real = (rs*rc+is*ic)/d; | |
| 291 r.imag = (is*rc-rs*ic)/d; | |
| 292 return r; | |
| 293 } | |
| 294 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
295 static char c_tan_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
296 "tan(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
297 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
298 Return the tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
299 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
300 |
| 3771 | 301 static Py_complex c_tanh(x) |
| 302 Py_complex x; | |
| 3334 | 303 { |
| 3771 | 304 Py_complex r; |
| 3334 | 305 double si,ci,shr,chr; |
| 306 double rs,is,rc,ic; | |
| 307 double d; | |
| 308 si = sin(x.imag); | |
| 309 ci = cos(x.imag); | |
| 310 shr = sinh(x.real); | |
| 311 chr = cosh(x.real); | |
| 312 rs = ci*shr; | |
| 313 is = si*chr; | |
| 314 rc = ci*chr; | |
| 315 ic = si*shr; | |
| 316 d = rc*rc + ic*ic; | |
| 317 r.real = (rs*rc+is*ic)/d; | |
| 318 r.imag = (is*rc-rs*ic)/d; | |
| 319 return r; | |
| 320 } | |
| 321 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
322 static char c_tanh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
323 "tanh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
324 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
325 Return the hyperbolic tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
326 |
| 3334 | 327 |
| 328 /* And now the glue to make them available from Python: */ | |
| 329 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
330 static PyObject * |
| 3334 | 331 math_error() |
| 332 { | |
| 333 if (errno == EDOM) | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
334 PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 3334 | 335 else if (errno == ERANGE) |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
336 PyErr_SetString(PyExc_OverflowError, "math range error"); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
337 else /* Unexpected math error */ |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
338 PyErr_SetFromErrno(PyExc_ValueError); |
| 3334 | 339 return NULL; |
| 340 } | |
| 341 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
342 static PyObject * |
| 3334 | 343 math_1(args, func) |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
344 PyObject *args; |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
345 Py_complex (*func) Py_FPROTO((Py_complex)); |
| 3334 | 346 { |
| 3771 | 347 Py_complex x; |
| 3334 | 348 if (!PyArg_ParseTuple(args, "D", &x)) |
| 349 return NULL; | |
| 350 errno = 0; | |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4649
diff
changeset
|
351 PyFPE_START_PROTECT("complex function", return 0) |
| 3334 | 352 x = (*func)(x); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
353 PyFPE_END_PROTECT(x) |
| 3334 | 354 CHECK(x.real); |
| 355 CHECK(x.imag); | |
| 356 if (errno != 0) | |
| 357 return math_error(); | |
| 358 else | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
359 return PyComplex_FromCComplex(x); |
| 3334 | 360 } |
| 361 | |
| 362 #define FUNC1(stubname, func) \ | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
363 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 3334 | 364 return math_1(args, func); \ |
| 365 } | |
| 366 | |
| 367 FUNC1(cmath_acos, c_acos) | |
| 368 FUNC1(cmath_acosh, c_acosh) | |
| 369 FUNC1(cmath_asin, c_asin) | |
| 370 FUNC1(cmath_asinh, c_asinh) | |
| 371 FUNC1(cmath_atan, c_atan) | |
| 372 FUNC1(cmath_atanh, c_atanh) | |
| 373 FUNC1(cmath_cos, c_cos) | |
| 374 FUNC1(cmath_cosh, c_cosh) | |
| 375 FUNC1(cmath_exp, c_exp) | |
| 376 FUNC1(cmath_log, c_log) | |
| 377 FUNC1(cmath_log10, c_log10) | |
| 378 FUNC1(cmath_sin, c_sin) | |
| 379 FUNC1(cmath_sinh, c_sinh) | |
| 380 FUNC1(cmath_sqrt, c_sqrt) | |
| 381 FUNC1(cmath_tan, c_tan) | |
| 382 FUNC1(cmath_tanh, c_tanh) | |
| 383 | |
| 384 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
385 static char module_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
386 "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
|
387 functions for complex numbers."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
388 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
389 |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
390 static PyMethodDef cmath_methods[] = { |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
391 {"acos", cmath_acos, 1, c_acos_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
392 {"acosh", cmath_acosh, 1, c_acosh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
393 {"asin", cmath_asin, 1, c_asin_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
394 {"asinh", cmath_asinh, 1, c_asinh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
395 {"atan", cmath_atan, 1, c_atan_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
396 {"atanh", cmath_atanh, 1, c_atanh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
397 {"cos", cmath_cos, 1, c_cos_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
398 {"cosh", cmath_cosh, 1, c_cosh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
399 {"exp", cmath_exp, 1, c_exp_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
400 {"log", cmath_log, 1, c_log_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
401 {"log10", cmath_log10, 1, c_log10_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
402 {"sin", cmath_sin, 1, c_sin_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
403 {"sinh", cmath_sinh, 1, c_sinh_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
404 {"sqrt", cmath_sqrt, 1, c_sqrt_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
405 {"tan", cmath_tan, 1, c_tan_doc}, |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
406 {"tanh", cmath_tanh, 1, c_tanh_doc}, |
| 3334 | 407 {NULL, NULL} /* sentinel */ |
| 408 }; | |
| 409 | |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
410 DL_EXPORT(void) |
| 3334 | 411 initcmath() |
| 412 { | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
413 PyObject *m, *d, *v; |
| 3334 | 414 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
415 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
|
416 d = PyModule_GetDict(m); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
417 PyDict_SetItemString(d, "pi", |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
418 v = PyFloat_FromDouble(atan(1.0) * 4.0)); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
419 Py_DECREF(v); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
420 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
|
421 Py_DECREF(v); |
| 3334 | 422 } |
