Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1 | /* Complex math module */ |
| 2 | |
| 3 | /* much code borrowed from mathmodule.c */ |
| 4 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 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}; |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 33 | #if 0 |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 34 | static Py_complex c_mi = {0., -1.}; |
| 35 | static Py_complex c_pi2 = {M_PI/2., 0.}; |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 36 | #endif |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 37 | |
| 38 | /* forward declarations */ |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 39 | staticforward Py_complex c_log(); |
| 40 | staticforward Py_complex c_prodi(); |
| 41 | staticforward Py_complex c_sqrt(); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 44 | static Py_complex c_acos(x) |
| 45 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 51 | static char c_acos_doc [] = |
| 52 | "acos(x)\n\ |
| 53 | \n\ |
| 54 | Return the arc cosine of x."; |
| 55 | |
| 56 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 57 | static Py_complex c_acosh(x) |
| 58 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 59 | { |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame^] | 60 | Py_complex z; |
| 61 | z = c_sqrt(c_half); |
| 62 | z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_1)), |
| 63 | c_sqrt(c_diff(x,c_1))))); |
| 64 | return c_sum(z, z); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 67 | static char c_acosh_doc [] = |
| 68 | "acosh(x)\n\ |
| 69 | \n\ |
Fred Drake | 0e11c49 | 1999-03-16 14:17:48 +0000 | [diff] [blame] | 70 | Return the hyperbolic arccosine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 71 | |
| 72 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 73 | static Py_complex c_asin(x) |
| 74 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 75 | { |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame^] | 76 | Py_complex z; |
| 77 | z = c_sqrt(c_half); |
| 78 | z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_i)), |
| 79 | c_sqrt(c_diff(x,c_i))))); |
| 80 | return c_sum(z, z); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 83 | static char c_asin_doc [] = |
| 84 | "asin(x)\n\ |
| 85 | \n\ |
| 86 | Return the arc sine of x."; |
| 87 | |
| 88 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 89 | static Py_complex c_asinh(x) |
| 90 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 91 | { |
Guido van Rossum | 11a5071 | 1999-01-14 19:11:11 +0000 | [diff] [blame] | 92 | /* Break up long expression for WATCOM */ |
| 93 | Py_complex z; |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame^] | 94 | z = c_sum(c_1,c_prod(x, x)); |
| 95 | return c_log(c_sum(c_sqrt(z), x)); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 98 | static char c_asinh_doc [] = |
| 99 | "asinh(x)\n\ |
| 100 | \n\ |
| 101 | Return the hyperbolic arc sine of x."; |
| 102 | |
| 103 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 104 | static Py_complex c_atan(x) |
| 105 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 106 | { |
| 107 | return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); |
| 108 | } |
| 109 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 110 | static char c_atan_doc [] = |
| 111 | "atan(x)\n\ |
| 112 | \n\ |
| 113 | Return the arc tangent of x."; |
| 114 | |
| 115 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 116 | static Py_complex c_atanh(x) |
| 117 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 118 | { |
| 119 | return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); |
| 120 | } |
| 121 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 122 | static char c_atanh_doc [] = |
| 123 | "atanh(x)\n\ |
| 124 | \n\ |
| 125 | Return the hyperbolic arc tangent of x."; |
| 126 | |
| 127 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 128 | static Py_complex c_cos(x) |
| 129 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 130 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 131 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 132 | r.real = cos(x.real)*cosh(x.imag); |
| 133 | r.imag = -sin(x.real)*sinh(x.imag); |
| 134 | return r; |
| 135 | } |
| 136 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 137 | static char c_cos_doc [] = |
| 138 | "cos(x)\n\ |
| 139 | \n\ |
| 140 | Return the cosine of x."; |
| 141 | |
| 142 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 143 | static Py_complex c_cosh(x) |
| 144 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 145 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 146 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 147 | r.real = cos(x.imag)*cosh(x.real); |
| 148 | r.imag = sin(x.imag)*sinh(x.real); |
| 149 | return r; |
| 150 | } |
| 151 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 152 | static char c_cosh_doc [] = |
| 153 | "cosh(x)\n\ |
| 154 | \n\ |
| 155 | Return the hyperbolic cosine of x."; |
| 156 | |
| 157 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 158 | static Py_complex c_exp(x) |
| 159 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 160 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 161 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 168 | static char c_exp_doc [] = |
| 169 | "exp(x)\n\ |
| 170 | \n\ |
| 171 | Return the exponential value e**x."; |
| 172 | |
| 173 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 174 | static Py_complex c_log(x) |
| 175 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 176 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 177 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 184 | static char c_log_doc [] = |
| 185 | "log(x)\n\ |
| 186 | \n\ |
| 187 | Return the natural logarithm of x."; |
| 188 | |
| 189 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 190 | static Py_complex c_log10(x) |
| 191 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 193 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 200 | static char c_log10_doc [] = |
| 201 | "log10(x)\n\ |
| 202 | \n\ |
| 203 | Return the base-10 logarithm of x."; |
| 204 | |
| 205 | |
| 206 | /* internal function not available from Python */ |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 207 | static Py_complex c_prodi(x) |
| 208 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 209 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 210 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 211 | r.real = -x.imag; |
| 212 | r.imag = x.real; |
| 213 | return r; |
| 214 | } |
| 215 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 217 | static Py_complex c_sin(x) |
| 218 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 219 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 220 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 221 | r.real = sin(x.real)*cosh(x.imag); |
| 222 | r.imag = cos(x.real)*sinh(x.imag); |
| 223 | return r; |
| 224 | } |
| 225 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 226 | static char c_sin_doc [] = |
| 227 | "sin(x)\n\ |
| 228 | \n\ |
| 229 | Return the sine of x."; |
| 230 | |
| 231 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 232 | static Py_complex c_sinh(x) |
| 233 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 234 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 235 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 236 | r.real = cos(x.imag)*sinh(x.real); |
| 237 | r.imag = sin(x.imag)*cosh(x.real); |
| 238 | return r; |
| 239 | } |
| 240 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 241 | static char c_sinh_doc [] = |
| 242 | "sinh(x)\n\ |
| 243 | \n\ |
| 244 | Return the hyperbolic sine of x."; |
| 245 | |
| 246 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 247 | static Py_complex c_sqrt(x) |
| 248 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 249 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 250 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 273 | static char c_sqrt_doc [] = |
| 274 | "sqrt(x)\n\ |
| 275 | \n\ |
| 276 | Return the square root of x."; |
| 277 | |
| 278 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 279 | static Py_complex c_tan(x) |
| 280 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 281 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 282 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 300 | static char c_tan_doc [] = |
| 301 | "tan(x)\n\ |
| 302 | \n\ |
| 303 | Return the tangent of x."; |
| 304 | |
| 305 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 306 | static Py_complex c_tanh(x) |
| 307 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 308 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 309 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 327 | static char c_tanh_doc [] = |
| 328 | "tanh(x)\n\ |
| 329 | \n\ |
| 330 | Return the hyperbolic tangent of x."; |
| 331 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 332 | |
| 333 | /* And now the glue to make them available from Python: */ |
| 334 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 335 | static PyObject * |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 336 | math_error() |
| 337 | { |
| 338 | if (errno == EDOM) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 339 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 340 | else if (errno == ERANGE) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 341 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 342 | else /* Unexpected math error */ |
| 343 | PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 344 | return NULL; |
| 345 | } |
| 346 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 347 | static PyObject * |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 348 | math_1(args, func) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 349 | PyObject *args; |
| 350 | Py_complex (*func) Py_FPROTO((Py_complex)); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 351 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 352 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 353 | if (!PyArg_ParseTuple(args, "D", &x)) |
| 354 | return NULL; |
| 355 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 356 | PyFPE_START_PROTECT("complex function", return 0) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 357 | x = (*func)(x); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 358 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 359 | CHECK(x.real); |
| 360 | CHECK(x.imag); |
| 361 | if (errno != 0) |
| 362 | return math_error(); |
| 363 | else |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 364 | return PyComplex_FromCComplex(x); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | #define FUNC1(stubname, func) \ |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 368 | static PyObject * stubname(self, args) PyObject *self, *args; { \ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 390 | static char module_doc [] = |
| 391 | "This module is always available. It provides access to mathematical\n\ |
| 392 | functions for complex numbers."; |
| 393 | |
| 394 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 395 | static PyMethodDef cmath_methods[] = { |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 396 | {"acos", cmath_acos, 1, c_acos_doc}, |
| 397 | {"acosh", cmath_acosh, 1, c_acosh_doc}, |
| 398 | {"asin", cmath_asin, 1, c_asin_doc}, |
| 399 | {"asinh", cmath_asinh, 1, c_asinh_doc}, |
| 400 | {"atan", cmath_atan, 1, c_atan_doc}, |
| 401 | {"atanh", cmath_atanh, 1, c_atanh_doc}, |
| 402 | {"cos", cmath_cos, 1, c_cos_doc}, |
| 403 | {"cosh", cmath_cosh, 1, c_cosh_doc}, |
| 404 | {"exp", cmath_exp, 1, c_exp_doc}, |
| 405 | {"log", cmath_log, 1, c_log_doc}, |
| 406 | {"log10", cmath_log10, 1, c_log10_doc}, |
| 407 | {"sin", cmath_sin, 1, c_sin_doc}, |
| 408 | {"sinh", cmath_sinh, 1, c_sinh_doc}, |
| 409 | {"sqrt", cmath_sqrt, 1, c_sqrt_doc}, |
| 410 | {"tan", cmath_tan, 1, c_tan_doc}, |
| 411 | {"tanh", cmath_tanh, 1, c_tanh_doc}, |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 412 | {NULL, NULL} /* sentinel */ |
| 413 | }; |
| 414 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 415 | DL_EXPORT(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 416 | initcmath() |
| 417 | { |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 418 | PyObject *m, *d, *v; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 420 | m = Py_InitModule3("cmath", cmath_methods, module_doc); |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 421 | d = PyModule_GetDict(m); |
| 422 | PyDict_SetItemString(d, "pi", |
| 423 | v = PyFloat_FromDouble(atan(1.0) * 4.0)); |
| 424 | Py_DECREF(v); |
| 425 | PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0))); |
| 426 | Py_DECREF(v); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 427 | } |