Mercurial > cpython-lru_cache-weakref
annotate Modules/cmathmodule.c @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | 3c0f75a2198f |
| children | 9696c95d09b7 |
| 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 #ifdef i860 | |
| 8 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ | |
| 9 #undef HUGE_VAL | |
| 10 #endif | |
| 11 | |
| 12 #ifdef HUGE_VAL | |
| 13 #define CHECK(x) if (errno != 0) ; \ | |
| 14 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ | |
| 15 else errno = ERANGE | |
| 16 #else | |
| 17 #define CHECK(x) /* Don't know how to check */ | |
| 18 #endif | |
| 19 | |
| 20 #ifndef M_PI | |
| 21 #define M_PI (3.141592653589793239) | |
| 22 #endif | |
| 23 | |
| 24 /* First, the C functions that do the real work */ | |
| 25 | |
| 26 /* constants */ | |
| 3771 | 27 static Py_complex c_1 = {1., 0.}; |
| 28 static Py_complex c_half = {0.5, 0.}; | |
| 29 static Py_complex c_i = {0., 1.}; | |
| 30 static Py_complex c_i2 = {0., 0.5}; | |
| 4600 | 31 #if 0 |
| 3771 | 32 static Py_complex c_mi = {0., -1.}; |
| 33 static Py_complex c_pi2 = {M_PI/2., 0.}; | |
| 4600 | 34 #endif |
| 3334 | 35 |
| 36 /* forward declarations */ | |
|
13685
129c9775cc19
Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents:
13684
diff
changeset
|
37 staticforward Py_complex c_log(Py_complex); |
|
129c9775cc19
Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents:
13684
diff
changeset
|
38 staticforward Py_complex c_prodi(Py_complex); |
|
129c9775cc19
Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents:
13684
diff
changeset
|
39 staticforward Py_complex c_sqrt(Py_complex); |
| 3334 | 40 |
| 41 | |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
42 static Py_complex c_acos(Py_complex x) |
| 3334 | 43 { |
| 44 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i, | |
| 45 c_sqrt(c_diff(c_1,c_prod(x,x)))))))); | |
| 46 } | |
| 47 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
48 static char c_acos_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
49 "acos(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
50 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
51 Return the arc cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
52 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
53 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
54 static Py_complex c_acosh(Py_complex x) |
| 3334 | 55 { |
|
13169
b78b996e8ebc
Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents:
10403
diff
changeset
|
56 Py_complex z; |
|
b78b996e8ebc
Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents:
10403
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 return c_sum(z, z); |
| 3334 | 61 } |
| 62 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
63 static char c_acosh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
64 "acosh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
65 \n\ |
|
10403
9c6da30ae675
Docstring fix: acosh() returns the hyperbolic arccosine, not the
Fred Drake <fdrake@acm.org>
parents:
9886
diff
changeset
|
66 Return the hyperbolic arccosine of x."; |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
67 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
68 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
69 static Py_complex c_asin(Py_complex x) |
| 3334 | 70 { |
|
13169
b78b996e8ebc
Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents:
10403
diff
changeset
|
71 Py_complex z; |
|
b78b996e8ebc
Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents:
10403
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 return c_sum(z, z); |
| 3334 | 76 } |
| 77 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
78 static char c_asin_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
79 "asin(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
80 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
81 Return the arc sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
82 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
83 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
84 static Py_complex c_asinh(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; |
|
13169
b78b996e8ebc
Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents:
10403
diff
changeset
|
88 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
|
89 return c_log(c_sum(c_sqrt(z), x)); |
| 3334 | 90 } |
| 91 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
92 static char c_asinh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
93 "asinh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
94 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
95 Return the hyperbolic arc sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
96 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
97 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
98 static Py_complex c_atan(Py_complex x) |
| 3334 | 99 { |
| 100 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); | |
| 101 } | |
| 102 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
103 static char c_atan_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
104 "atan(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
105 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
106 Return the arc tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
107 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
108 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
109 static Py_complex c_atanh(Py_complex x) |
| 3334 | 110 { |
| 111 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); | |
| 112 } | |
| 113 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
114 static char c_atanh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
115 "atanh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
116 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
117 Return the hyperbolic arc tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
118 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
119 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
120 static Py_complex c_cos(Py_complex x) |
| 3334 | 121 { |
| 3771 | 122 Py_complex r; |
| 3334 | 123 r.real = cos(x.real)*cosh(x.imag); |
| 124 r.imag = -sin(x.real)*sinh(x.imag); | |
| 125 return r; | |
| 126 } | |
| 127 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
128 static char c_cos_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
129 "cos(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
130 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
131 Return the cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
132 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
133 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
134 static Py_complex c_cosh(Py_complex x) |
| 3334 | 135 { |
| 3771 | 136 Py_complex r; |
| 3334 | 137 r.real = cos(x.imag)*cosh(x.real); |
| 138 r.imag = sin(x.imag)*sinh(x.real); | |
| 139 return r; | |
| 140 } | |
| 141 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
142 static char c_cosh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
143 "cosh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
144 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
145 Return the hyperbolic cosine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
146 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
147 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
148 static Py_complex c_exp(Py_complex x) |
| 3334 | 149 { |
| 3771 | 150 Py_complex r; |
| 3334 | 151 double l = exp(x.real); |
| 152 r.real = l*cos(x.imag); | |
| 153 r.imag = l*sin(x.imag); | |
| 154 return r; | |
| 155 } | |
| 156 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
157 static char c_exp_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
158 "exp(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
159 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
160 Return the exponential value e**x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
161 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
162 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
163 static Py_complex c_log(Py_complex x) |
| 3334 | 164 { |
| 3771 | 165 Py_complex r; |
| 3334 | 166 double l = hypot(x.real,x.imag); |
| 167 r.imag = atan2(x.imag, x.real); | |
| 168 r.real = log(l); | |
| 169 return r; | |
| 170 } | |
| 171 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
172 static char c_log_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
173 "log(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
174 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
175 Return the natural logarithm of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
176 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
177 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
178 static Py_complex c_log10(Py_complex x) |
| 3334 | 179 { |
| 3771 | 180 Py_complex r; |
| 3334 | 181 double l = hypot(x.real,x.imag); |
| 182 r.imag = atan2(x.imag, x.real)/log(10.); | |
| 183 r.real = log10(l); | |
| 184 return r; | |
| 185 } | |
| 186 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
187 static char c_log10_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
188 "log10(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
189 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
190 Return the base-10 logarithm of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
191 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
192 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
193 /* internal function not available from Python */ |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
194 static Py_complex c_prodi(Py_complex x) |
| 3334 | 195 { |
| 3771 | 196 Py_complex r; |
| 3334 | 197 r.real = -x.imag; |
| 198 r.imag = x.real; | |
| 199 return r; | |
| 200 } | |
| 201 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
202 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
203 static Py_complex c_sin(Py_complex x) |
| 3334 | 204 { |
| 3771 | 205 Py_complex r; |
| 3334 | 206 r.real = sin(x.real)*cosh(x.imag); |
| 207 r.imag = cos(x.real)*sinh(x.imag); | |
| 208 return r; | |
| 209 } | |
| 210 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
211 static char c_sin_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
212 "sin(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
213 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
214 Return the sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
215 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
216 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
217 static Py_complex c_sinh(Py_complex x) |
| 3334 | 218 { |
| 3771 | 219 Py_complex r; |
| 3334 | 220 r.real = cos(x.imag)*sinh(x.real); |
| 221 r.imag = sin(x.imag)*cosh(x.real); | |
| 222 return r; | |
| 223 } | |
| 224 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
225 static char c_sinh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
226 "sinh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
227 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
228 Return the hyperbolic sine of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
229 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
230 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
231 static Py_complex c_sqrt(Py_complex x) |
| 3334 | 232 { |
| 3771 | 233 Py_complex r; |
| 3334 | 234 double s,d; |
| 235 if (x.real == 0. && x.imag == 0.) | |
| 236 r = x; | |
| 237 else { | |
| 238 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag))); | |
| 239 d = 0.5*x.imag/s; | |
| 240 if (x.real > 0.) { | |
| 241 r.real = s; | |
| 242 r.imag = d; | |
| 243 } | |
| 244 else if (x.imag >= 0.) { | |
| 245 r.real = d; | |
| 246 r.imag = s; | |
| 247 } | |
| 248 else { | |
| 249 r.real = -d; | |
| 250 r.imag = -s; | |
| 251 } | |
| 252 } | |
| 253 return r; | |
| 254 } | |
| 255 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
256 static char c_sqrt_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
257 "sqrt(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
258 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
259 Return the square root of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
260 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
261 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
262 static Py_complex c_tan(Py_complex x) |
| 3334 | 263 { |
| 3771 | 264 Py_complex r; |
| 3334 | 265 double sr,cr,shi,chi; |
| 266 double rs,is,rc,ic; | |
| 267 double d; | |
| 268 sr = sin(x.real); | |
| 269 cr = cos(x.real); | |
| 270 shi = sinh(x.imag); | |
| 271 chi = cosh(x.imag); | |
| 272 rs = sr*chi; | |
| 273 is = cr*shi; | |
| 274 rc = cr*chi; | |
| 275 ic = -sr*shi; | |
| 276 d = rc*rc + ic*ic; | |
| 277 r.real = (rs*rc+is*ic)/d; | |
| 278 r.imag = (is*rc-rs*ic)/d; | |
| 279 return r; | |
| 280 } | |
| 281 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
282 static char c_tan_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
283 "tan(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
284 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
285 Return the tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
286 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
287 |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
288 static Py_complex c_tanh(Py_complex x) |
| 3334 | 289 { |
| 3771 | 290 Py_complex r; |
| 3334 | 291 double si,ci,shr,chr; |
| 292 double rs,is,rc,ic; | |
| 293 double d; | |
| 294 si = sin(x.imag); | |
| 295 ci = cos(x.imag); | |
| 296 shr = sinh(x.real); | |
| 297 chr = cosh(x.real); | |
| 298 rs = ci*shr; | |
| 299 is = si*chr; | |
| 300 rc = ci*chr; | |
| 301 ic = si*shr; | |
| 302 d = rc*rc + ic*ic; | |
| 303 r.real = (rs*rc+is*ic)/d; | |
| 304 r.imag = (is*rc-rs*ic)/d; | |
| 305 return r; | |
| 306 } | |
| 307 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
308 static char c_tanh_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
309 "tanh(x)\n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
310 \n\ |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
311 Return the hyperbolic tangent of x."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
312 |
| 3334 | 313 |
| 314 /* And now the glue to make them available from Python: */ | |
| 315 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
316 static PyObject * |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13493
diff
changeset
|
317 math_error(void) |
| 3334 | 318 { |
| 319 if (errno == EDOM) | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
320 PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 3334 | 321 else if (errno == ERANGE) |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
322 PyErr_SetString(PyExc_OverflowError, "math range error"); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
323 else /* Unexpected math error */ |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
324 PyErr_SetFromErrno(PyExc_ValueError); |
| 3334 | 325 return NULL; |
| 326 } | |
| 327 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
328 static PyObject * |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
329 math_1(PyObject *args, Py_complex (*func)(Py_complex)) |
| 3334 | 330 { |
| 3771 | 331 Py_complex x; |
| 3334 | 332 if (!PyArg_ParseTuple(args, "D", &x)) |
| 333 return NULL; | |
| 334 errno = 0; | |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4649
diff
changeset
|
335 PyFPE_START_PROTECT("complex function", return 0) |
| 3334 | 336 x = (*func)(x); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
337 PyFPE_END_PROTECT(x) |
| 3334 | 338 CHECK(x.real); |
| 339 CHECK(x.imag); | |
| 340 if (errno != 0) | |
| 341 return math_error(); | |
| 342 else | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
343 return PyComplex_FromCComplex(x); |
| 3334 | 344 } |
| 345 | |
| 346 #define FUNC1(stubname, func) \ | |
|
13493
8bacf8631a11
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
347 static PyObject * stubname(PyObject *self, PyObject *args) { \ |
| 3334 | 348 return math_1(args, func); \ |
| 349 } | |
| 350 | |
| 351 FUNC1(cmath_acos, c_acos) | |
| 352 FUNC1(cmath_acosh, c_acosh) | |
| 353 FUNC1(cmath_asin, c_asin) | |
| 354 FUNC1(cmath_asinh, c_asinh) | |
| 355 FUNC1(cmath_atan, c_atan) | |
| 356 FUNC1(cmath_atanh, c_atanh) | |
| 357 FUNC1(cmath_cos, c_cos) | |
| 358 FUNC1(cmath_cosh, c_cosh) | |
| 359 FUNC1(cmath_exp, c_exp) | |
| 360 FUNC1(cmath_log, c_log) | |
| 361 FUNC1(cmath_log10, c_log10) | |
| 362 FUNC1(cmath_sin, c_sin) | |
| 363 FUNC1(cmath_sinh, c_sinh) | |
| 364 FUNC1(cmath_sqrt, c_sqrt) | |
| 365 FUNC1(cmath_tan, c_tan) | |
| 366 FUNC1(cmath_tanh, c_tanh) | |
| 367 | |
| 368 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
369 static char module_doc [] = |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
370 "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
|
371 functions for complex numbers."; |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
372 |
|
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
373 |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
374 static PyMethodDef cmath_methods[] = { |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
375 {"acos", cmath_acos, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
376 METH_VARARGS, c_acos_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
377 {"acosh", cmath_acosh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
378 METH_VARARGS, c_acosh_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
379 {"asin", cmath_asin, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
380 METH_VARARGS, c_asin_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
381 {"asinh", cmath_asinh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
382 METH_VARARGS, c_asinh_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
383 {"atan", cmath_atan, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
384 METH_VARARGS, c_atan_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
385 {"atanh", cmath_atanh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
386 METH_VARARGS, c_atanh_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
387 {"cos", cmath_cos, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
388 METH_VARARGS, c_cos_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
389 {"cosh", cmath_cosh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
390 METH_VARARGS, c_cosh_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
391 {"exp", cmath_exp, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
392 METH_VARARGS, c_exp_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
393 {"log", cmath_log, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
394 METH_VARARGS, c_log_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
395 {"log10", cmath_log10, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
396 METH_VARARGS, c_log10_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
397 {"sin", cmath_sin, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
398 METH_VARARGS, c_sin_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
399 {"sinh", cmath_sinh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
400 METH_VARARGS, c_sinh_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
401 {"sqrt", cmath_sqrt, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
402 METH_VARARGS, c_sqrt_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
403 {"tan", cmath_tan, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
404 METH_VARARGS, c_tan_doc}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
405 {"tanh", cmath_tanh, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13761
diff
changeset
|
406 METH_VARARGS, 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) |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13493
diff
changeset
|
411 initcmath(void) |
| 3334 | 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 } |
