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" |
Mark Dickinson | f371859 | 2009-12-21 15:27:41 +0000 | [diff] [blame] | 6 | #include "_math.h" |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 7 | /* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from |
| 8 | float.h. We assume that FLT_RADIX is either 2 or 16. */ |
| 9 | #include <float.h> |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 10 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 11 | #include "clinic/cmathmodule.c.h" |
| 12 | /*[clinic input] |
| 13 | output preset file |
| 14 | module cmath |
| 15 | [clinic start generated code]*/ |
| 16 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ef7e0fdd8a143c03]*/ |
| 17 | |
| 18 | /*[python input] |
| 19 | class Py_complex_protected_converter(Py_complex_converter): |
| 20 | def modify(self): |
| 21 | return 'errno = 0; PyFPE_START_PROTECT("complex function", goto exit);' |
| 22 | |
| 23 | |
| 24 | class Py_complex_protected_return_converter(CReturnConverter): |
| 25 | type = "Py_complex" |
| 26 | |
| 27 | def render(self, function, data): |
| 28 | self.declare(data) |
| 29 | data.return_conversion.append(""" |
| 30 | PyFPE_END_PROTECT(_return_value); |
| 31 | if (errno == EDOM) {{ |
| 32 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 33 | goto exit; |
| 34 | }} |
| 35 | else if (errno == ERANGE) {{ |
| 36 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 37 | goto exit; |
| 38 | }} |
| 39 | else {{ |
| 40 | return_value = PyComplex_FromCComplex(_return_value); |
| 41 | }} |
| 42 | """.strip()) |
| 43 | [python start generated code]*/ |
| 44 | /*[python end generated code: output=da39a3ee5e6b4b0d input=231019039a6fbb9a]*/ |
| 45 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 46 | #if (FLT_RADIX != 2 && FLT_RADIX != 16) |
| 47 | #error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16" |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 50 | #ifndef M_LN2 |
| 51 | #define M_LN2 (0.6931471805599453094) /* natural log of 2 */ |
| 52 | #endif |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 53 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 54 | #ifndef M_LN10 |
| 55 | #define M_LN10 (2.302585092994045684) /* natural log of 10 */ |
| 56 | #endif |
| 57 | |
| 58 | /* |
| 59 | CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log, |
| 60 | inverse trig and inverse hyperbolic trig functions. Its log is used in the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 61 | evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 62 | overflow. |
| 63 | */ |
| 64 | |
| 65 | #define CM_LARGE_DOUBLE (DBL_MAX/4.) |
| 66 | #define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE)) |
| 67 | #define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE)) |
| 68 | #define CM_SQRT_DBL_MIN (sqrt(DBL_MIN)) |
| 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | /* |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 71 | CM_SCALE_UP is an odd integer chosen such that multiplication by |
| 72 | 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal. |
| 73 | CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute |
| 74 | square roots accurately when the real and imaginary parts of the argument |
| 75 | are subnormal. |
| 76 | */ |
| 77 | |
| 78 | #if FLT_RADIX==2 |
| 79 | #define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1) |
| 80 | #elif FLT_RADIX==16 |
| 81 | #define CM_SCALE_UP (4*DBL_MANT_DIG+1) |
| 82 | #endif |
| 83 | #define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 84 | |
| 85 | /* forward declarations */ |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 86 | static Py_complex cmath_asinh_impl(PyModuleDef *, Py_complex); |
| 87 | static Py_complex cmath_atanh_impl(PyModuleDef *, Py_complex); |
| 88 | static Py_complex cmath_cosh_impl(PyModuleDef *, Py_complex); |
| 89 | static Py_complex cmath_sinh_impl(PyModuleDef *, Py_complex); |
| 90 | static Py_complex cmath_sqrt_impl(PyModuleDef *, Py_complex); |
| 91 | static Py_complex cmath_tanh_impl(PyModuleDef *, Py_complex); |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 92 | static PyObject * math_error(void); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 93 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 94 | /* Code to deal with special values (infinities, NaNs, etc.). */ |
| 95 | |
| 96 | /* special_type takes a double and returns an integer code indicating |
| 97 | the type of the double as follows: |
| 98 | */ |
| 99 | |
| 100 | enum special_types { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | ST_NINF, /* 0, negative infinity */ |
| 102 | ST_NEG, /* 1, negative finite number (nonzero) */ |
| 103 | ST_NZERO, /* 2, -0. */ |
| 104 | ST_PZERO, /* 3, +0. */ |
| 105 | ST_POS, /* 4, positive finite number (nonzero) */ |
| 106 | ST_PINF, /* 5, positive infinity */ |
| 107 | ST_NAN /* 6, Not a Number */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static enum special_types |
| 111 | special_type(double d) |
| 112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | if (Py_IS_FINITE(d)) { |
| 114 | if (d != 0) { |
| 115 | if (copysign(1., d) == 1.) |
| 116 | return ST_POS; |
| 117 | else |
| 118 | return ST_NEG; |
| 119 | } |
| 120 | else { |
| 121 | if (copysign(1., d) == 1.) |
| 122 | return ST_PZERO; |
| 123 | else |
| 124 | return ST_NZERO; |
| 125 | } |
| 126 | } |
| 127 | if (Py_IS_NAN(d)) |
| 128 | return ST_NAN; |
| 129 | if (copysign(1., d) == 1.) |
| 130 | return ST_PINF; |
| 131 | else |
| 132 | return ST_NINF; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | #define SPECIAL_VALUE(z, table) \ |
| 136 | if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \ |
| 137 | errno = 0; \ |
| 138 | return table[special_type((z).real)] \ |
| 139 | [special_type((z).imag)]; \ |
| 140 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 141 | |
| 142 | #define P Py_MATH_PI |
| 143 | #define P14 0.25*Py_MATH_PI |
| 144 | #define P12 0.5*Py_MATH_PI |
| 145 | #define P34 0.75*Py_MATH_PI |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 146 | #define INF Py_HUGE_VAL |
| 147 | #define N Py_NAN |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 148 | #define U -9.5426319407711027e33 /* unlikely value, used as placeholder */ |
| 149 | |
| 150 | /* First, the C functions that do the real work. Each of the c_* |
| 151 | functions computes and returns the C99 Annex G recommended result |
| 152 | and also sets errno as follows: errno = 0 if no floating-point |
| 153 | exception is associated with the result; errno = EDOM if C99 Annex |
| 154 | G recommends raising divide-by-zero or invalid for this result; and |
| 155 | errno = ERANGE where the overflow floating-point signal should be |
| 156 | raised. |
| 157 | */ |
| 158 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 159 | static Py_complex acos_special_values[7][7]; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 160 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 161 | /*[clinic input] |
| 162 | cmath.acos -> Py_complex_protected |
| 163 | |
| 164 | z: Py_complex_protected |
| 165 | / |
| 166 | |
| 167 | Return the arc cosine of z. |
| 168 | [clinic start generated code]*/ |
| 169 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 170 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 171 | cmath_acos_impl(PyModuleDef *module, Py_complex z) |
| 172 | /*[clinic end generated code: output=7c1dd21ff818db6b input=bd6cbd78ae851927]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | Py_complex s1, s2, r; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | SPECIAL_VALUE(z, acos_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
| 179 | /* avoid unnecessary overflow for large arguments */ |
| 180 | r.real = atan2(fabs(z.imag), z.real); |
| 181 | /* split into cases to make sure that the branch cut has the |
| 182 | correct continuity on systems with unsigned zeros */ |
| 183 | if (z.real < 0.) { |
| 184 | r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) + |
| 185 | M_LN2*2., z.imag); |
| 186 | } else { |
| 187 | r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) + |
| 188 | M_LN2*2., -z.imag); |
| 189 | } |
| 190 | } else { |
| 191 | s1.real = 1.-z.real; |
| 192 | s1.imag = -z.imag; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 193 | s1 = cmath_sqrt_impl(module, s1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | s2.real = 1.+z.real; |
| 195 | s2.imag = z.imag; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 196 | s2 = cmath_sqrt_impl(module, s2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | r.real = 2.*atan2(s1.real, s2.real); |
| 198 | r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real); |
| 199 | } |
| 200 | errno = 0; |
| 201 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 204 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 205 | static Py_complex acosh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 206 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 207 | /*[clinic input] |
| 208 | cmath.acosh = cmath.acos |
| 209 | |
| 210 | Return the hyperbolic arccosine of z. |
| 211 | [clinic start generated code]*/ |
| 212 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 213 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 214 | cmath_acosh_impl(PyModuleDef *module, Py_complex z) |
| 215 | /*[clinic end generated code: output=c23c776429def981 input=bc016412080bb3e9]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | Py_complex s1, s2, r; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | SPECIAL_VALUE(z, acosh_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
| 222 | /* avoid unnecessary overflow for large arguments */ |
| 223 | r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.; |
| 224 | r.imag = atan2(z.imag, z.real); |
| 225 | } else { |
| 226 | s1.real = z.real - 1.; |
| 227 | s1.imag = z.imag; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 228 | s1 = cmath_sqrt_impl(module, s1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | s2.real = z.real + 1.; |
| 230 | s2.imag = z.imag; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 231 | s2 = cmath_sqrt_impl(module, s2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag); |
| 233 | r.imag = 2.*atan2(s1.imag, s2.real); |
| 234 | } |
| 235 | errno = 0; |
| 236 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 239 | /*[clinic input] |
| 240 | cmath.asin = cmath.acos |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 241 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 242 | Return the arc sine of z. |
| 243 | [clinic start generated code]*/ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 244 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 245 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 246 | cmath_asin_impl(PyModuleDef *module, Py_complex z) |
| 247 | /*[clinic end generated code: output=42d2346d46690826 input=be0bf0cfdd5239c5]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | /* asin(z) = -i asinh(iz) */ |
| 250 | Py_complex s, r; |
| 251 | s.real = -z.imag; |
| 252 | s.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 253 | s = cmath_asinh_impl(module, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | r.real = s.imag; |
| 255 | r.imag = -s.real; |
| 256 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 259 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 260 | static Py_complex asinh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 261 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 262 | /*[clinic input] |
| 263 | cmath.asinh = cmath.acos |
| 264 | |
| 265 | Return the hyperbolic arc sine of z. |
| 266 | [clinic start generated code]*/ |
| 267 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 268 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 269 | cmath_asinh_impl(PyModuleDef *module, Py_complex z) |
| 270 | /*[clinic end generated code: output=0c6664823c7b1b35 input=5a21fa0242928c9b]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | Py_complex s1, s2, r; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | SPECIAL_VALUE(z, asinh_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
| 277 | if (z.imag >= 0.) { |
| 278 | r.real = copysign(log(hypot(z.real/2., z.imag/2.)) + |
| 279 | M_LN2*2., z.real); |
| 280 | } else { |
| 281 | r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) + |
| 282 | M_LN2*2., -z.real); |
| 283 | } |
| 284 | r.imag = atan2(z.imag, fabs(z.real)); |
| 285 | } else { |
| 286 | s1.real = 1.+z.imag; |
| 287 | s1.imag = -z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 288 | s1 = cmath_sqrt_impl(module, s1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | s2.real = 1.-z.imag; |
| 290 | s2.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 291 | s2 = cmath_sqrt_impl(module, s2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag); |
| 293 | r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag); |
| 294 | } |
| 295 | errno = 0; |
| 296 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 299 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 300 | /*[clinic input] |
| 301 | cmath.atan = cmath.acos |
| 302 | |
| 303 | Return the arc tangent of z. |
| 304 | [clinic start generated code]*/ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 305 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 306 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 307 | cmath_atan_impl(PyModuleDef *module, Py_complex z) |
| 308 | /*[clinic end generated code: output=b7d44f02c6a5c3b5 input=3b21ff7d5eac632a]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | /* atan(z) = -i atanh(iz) */ |
| 311 | Py_complex s, r; |
| 312 | s.real = -z.imag; |
| 313 | s.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 314 | s = cmath_atanh_impl(module, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | r.real = s.imag; |
| 316 | r.imag = -s.real; |
| 317 | return r; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 320 | /* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow |
| 321 | C99 for atan2(0., 0.). */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 322 | static double |
| 323 | c_atan2(Py_complex z) |
| 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag)) |
| 326 | return Py_NAN; |
| 327 | if (Py_IS_INFINITY(z.imag)) { |
| 328 | if (Py_IS_INFINITY(z.real)) { |
| 329 | if (copysign(1., z.real) == 1.) |
| 330 | /* atan2(+-inf, +inf) == +-pi/4 */ |
| 331 | return copysign(0.25*Py_MATH_PI, z.imag); |
| 332 | else |
| 333 | /* atan2(+-inf, -inf) == +-pi*3/4 */ |
| 334 | return copysign(0.75*Py_MATH_PI, z.imag); |
| 335 | } |
| 336 | /* atan2(+-inf, x) == +-pi/2 for finite x */ |
| 337 | return copysign(0.5*Py_MATH_PI, z.imag); |
| 338 | } |
| 339 | if (Py_IS_INFINITY(z.real) || z.imag == 0.) { |
| 340 | if (copysign(1., z.real) == 1.) |
| 341 | /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
| 342 | return copysign(0., z.imag); |
| 343 | else |
| 344 | /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
| 345 | return copysign(Py_MATH_PI, z.imag); |
| 346 | } |
| 347 | return atan2(z.imag, z.real); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 350 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 351 | static Py_complex atanh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 352 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 353 | /*[clinic input] |
| 354 | cmath.atanh = cmath.acos |
| 355 | |
| 356 | Return the hyperbolic arc tangent of z. |
| 357 | [clinic start generated code]*/ |
| 358 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 359 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 360 | cmath_atanh_impl(PyModuleDef *module, Py_complex z) |
| 361 | /*[clinic end generated code: output=279e0b9fefc8da7c input=df19cdc9f9d431c9]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | Py_complex r; |
| 364 | double ay, h; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | SPECIAL_VALUE(z, atanh_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */ |
| 369 | if (z.real < 0.) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 370 | return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | ay = fabs(z.imag); |
| 374 | if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) { |
| 375 | /* |
| 376 | if abs(z) is large then we use the approximation |
| 377 | atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign |
| 378 | of z.imag) |
| 379 | */ |
| 380 | h = hypot(z.real/2., z.imag/2.); /* safe from overflow */ |
| 381 | r.real = z.real/4./h/h; |
| 382 | /* the two negations in the next line cancel each other out |
| 383 | except when working with unsigned zeros: they're there to |
| 384 | ensure that the branch cut has the correct continuity on |
| 385 | systems that don't support signed zeros */ |
| 386 | r.imag = -copysign(Py_MATH_PI/2., -z.imag); |
| 387 | errno = 0; |
| 388 | } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) { |
| 389 | /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */ |
| 390 | if (ay == 0.) { |
| 391 | r.real = INF; |
| 392 | r.imag = z.imag; |
| 393 | errno = EDOM; |
| 394 | } else { |
| 395 | r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.))); |
| 396 | r.imag = copysign(atan2(2., -ay)/2, z.imag); |
| 397 | errno = 0; |
| 398 | } |
| 399 | } else { |
| 400 | r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.; |
| 401 | r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.; |
| 402 | errno = 0; |
| 403 | } |
| 404 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 407 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 408 | /*[clinic input] |
| 409 | cmath.cos = cmath.acos |
| 410 | |
| 411 | Return the cosine of z. |
| 412 | [clinic start generated code]*/ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 413 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 414 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 415 | cmath_cos_impl(PyModuleDef *module, Py_complex z) |
| 416 | /*[clinic end generated code: output=9d1cdc1b5e761667 input=6022e39b77127ac7]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | /* cos(z) = cosh(iz) */ |
| 419 | Py_complex r; |
| 420 | r.real = -z.imag; |
| 421 | r.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 422 | r = cmath_cosh_impl(module, r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 426 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 427 | /* cosh(infinity + i*y) needs to be dealt with specially */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 428 | static Py_complex cosh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 429 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 430 | /*[clinic input] |
| 431 | cmath.cosh = cmath.acos |
| 432 | |
| 433 | Return the hyperbolic cosine of z. |
| 434 | [clinic start generated code]*/ |
| 435 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 436 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 437 | cmath_cosh_impl(PyModuleDef *module, Py_complex z) |
| 438 | /*[clinic end generated code: output=f3b5d3282b3024d3 input=d6b66339e9cc332b]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | Py_complex r; |
| 441 | double x_minus_one; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | /* special treatment for cosh(+/-inf + iy) if y is not a NaN */ |
| 444 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 445 | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) && |
| 446 | (z.imag != 0.)) { |
| 447 | if (z.real > 0) { |
| 448 | r.real = copysign(INF, cos(z.imag)); |
| 449 | r.imag = copysign(INF, sin(z.imag)); |
| 450 | } |
| 451 | else { |
| 452 | r.real = copysign(INF, cos(z.imag)); |
| 453 | r.imag = -copysign(INF, sin(z.imag)); |
| 454 | } |
| 455 | } |
| 456 | else { |
| 457 | r = cosh_special_values[special_type(z.real)] |
| 458 | [special_type(z.imag)]; |
| 459 | } |
| 460 | /* need to set errno = EDOM if y is +/- infinity and x is not |
| 461 | a NaN */ |
| 462 | if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real)) |
| 463 | errno = EDOM; |
| 464 | else |
| 465 | errno = 0; |
| 466 | return r; |
| 467 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
| 470 | /* deal correctly with cases where cosh(z.real) overflows but |
| 471 | cosh(z) does not. */ |
| 472 | x_minus_one = z.real - copysign(1., z.real); |
| 473 | r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E; |
| 474 | r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E; |
| 475 | } else { |
| 476 | r.real = cos(z.imag) * cosh(z.real); |
| 477 | r.imag = sin(z.imag) * sinh(z.real); |
| 478 | } |
| 479 | /* detect overflow, and set errno accordingly */ |
| 480 | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
| 481 | errno = ERANGE; |
| 482 | else |
| 483 | errno = 0; |
| 484 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 487 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 488 | /* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for |
| 489 | finite y */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 490 | static Py_complex exp_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 491 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 492 | /*[clinic input] |
| 493 | cmath.exp = cmath.acos |
| 494 | |
| 495 | Return the exponential value e**z. |
| 496 | [clinic start generated code]*/ |
| 497 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 498 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 499 | cmath_exp_impl(PyModuleDef *module, Py_complex z) |
| 500 | /*[clinic end generated code: output=6f8825eb2bcad9ba input=8b9e6cf8a92174c3]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | Py_complex r; |
| 503 | double l; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 506 | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
| 507 | && (z.imag != 0.)) { |
| 508 | if (z.real > 0) { |
| 509 | r.real = copysign(INF, cos(z.imag)); |
| 510 | r.imag = copysign(INF, sin(z.imag)); |
| 511 | } |
| 512 | else { |
| 513 | r.real = copysign(0., cos(z.imag)); |
| 514 | r.imag = copysign(0., sin(z.imag)); |
| 515 | } |
| 516 | } |
| 517 | else { |
| 518 | r = exp_special_values[special_type(z.real)] |
| 519 | [special_type(z.imag)]; |
| 520 | } |
| 521 | /* need to set errno = EDOM if y is +/- infinity and x is not |
| 522 | a NaN and not -infinity */ |
| 523 | if (Py_IS_INFINITY(z.imag) && |
| 524 | (Py_IS_FINITE(z.real) || |
| 525 | (Py_IS_INFINITY(z.real) && z.real > 0))) |
| 526 | errno = EDOM; |
| 527 | else |
| 528 | errno = 0; |
| 529 | return r; |
| 530 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | if (z.real > CM_LOG_LARGE_DOUBLE) { |
| 533 | l = exp(z.real-1.); |
| 534 | r.real = l*cos(z.imag)*Py_MATH_E; |
| 535 | r.imag = l*sin(z.imag)*Py_MATH_E; |
| 536 | } else { |
| 537 | l = exp(z.real); |
| 538 | r.real = l*cos(z.imag); |
| 539 | r.imag = l*sin(z.imag); |
| 540 | } |
| 541 | /* detect overflow, and set errno accordingly */ |
| 542 | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
| 543 | errno = ERANGE; |
| 544 | else |
| 545 | errno = 0; |
| 546 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 549 | static Py_complex log_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 550 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 551 | static Py_complex |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 552 | c_log(Py_complex z) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | /* |
| 555 | The usual formula for the real part is log(hypot(z.real, z.imag)). |
| 556 | There are four situations where this formula is potentially |
| 557 | problematic: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | (1) the absolute value of z is subnormal. Then hypot is subnormal, |
| 560 | so has fewer than the usual number of bits of accuracy, hence may |
| 561 | have large relative error. This then gives a large absolute error |
| 562 | in the log. This can be solved by rescaling z by a suitable power |
| 563 | of 2. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | (2) the absolute value of z is greater than DBL_MAX (e.g. when both |
| 566 | z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX) |
| 567 | Again, rescaling solves this. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | (3) the absolute value of z is close to 1. In this case it's |
| 570 | difficult to achieve good accuracy, at least in part because a |
| 571 | change of 1ulp in the real or imaginary part of z can result in a |
| 572 | change of billions of ulps in the correctly rounded answer. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | (4) z = 0. The simplest thing to do here is to call the |
| 575 | floating-point log with an argument of 0, and let its behaviour |
| 576 | (returning -infinity, signaling a floating-point exception, setting |
| 577 | errno, or whatever) determine that of c_log. So the usual formula |
| 578 | is fine here. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | Py_complex r; |
| 583 | double ax, ay, am, an, h; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | SPECIAL_VALUE(z, log_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | ax = fabs(z.real); |
| 588 | ay = fabs(z.imag); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) { |
| 591 | r.real = log(hypot(ax/2., ay/2.)) + M_LN2; |
| 592 | } else if (ax < DBL_MIN && ay < DBL_MIN) { |
| 593 | if (ax > 0. || ay > 0.) { |
| 594 | /* catch cases where hypot(ax, ay) is subnormal */ |
| 595 | r.real = log(hypot(ldexp(ax, DBL_MANT_DIG), |
| 596 | ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2; |
| 597 | } |
| 598 | else { |
| 599 | /* log(+/-0. +/- 0i) */ |
| 600 | r.real = -INF; |
| 601 | r.imag = atan2(z.imag, z.real); |
| 602 | errno = EDOM; |
| 603 | return r; |
| 604 | } |
| 605 | } else { |
| 606 | h = hypot(ax, ay); |
| 607 | if (0.71 <= h && h <= 1.73) { |
| 608 | am = ax > ay ? ax : ay; /* max(ax, ay) */ |
| 609 | an = ax > ay ? ay : ax; /* min(ax, ay) */ |
| 610 | r.real = m_log1p((am-1)*(am+1)+an*an)/2.; |
| 611 | } else { |
| 612 | r.real = log(h); |
| 613 | } |
| 614 | } |
| 615 | r.imag = atan2(z.imag, z.real); |
| 616 | errno = 0; |
| 617 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 620 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 621 | /*[clinic input] |
| 622 | cmath.log10 = cmath.acos |
| 623 | |
| 624 | Return the base-10 logarithm of z. |
| 625 | [clinic start generated code]*/ |
| 626 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 627 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 628 | cmath_log10_impl(PyModuleDef *module, Py_complex z) |
| 629 | /*[clinic end generated code: output=c7c426ca0e782341 input=cff5644f73c1519c]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | Py_complex r; |
| 632 | int errno_save; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | r = c_log(z); |
| 635 | errno_save = errno; /* just in case the divisions affect errno */ |
| 636 | r.real = r.real / M_LN10; |
| 637 | r.imag = r.imag / M_LN10; |
| 638 | errno = errno_save; |
| 639 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 642 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 643 | /*[clinic input] |
| 644 | cmath.sin = cmath.acos |
| 645 | |
| 646 | Return the sine of z. |
| 647 | [clinic start generated code]*/ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 648 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 649 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 650 | cmath_sin_impl(PyModuleDef *module, Py_complex z) |
| 651 | /*[clinic end generated code: output=e7f5e2b253825ac7 input=2d3519842a8b4b85]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | /* sin(z) = -i sin(iz) */ |
| 654 | Py_complex s, r; |
| 655 | s.real = -z.imag; |
| 656 | s.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 657 | s = cmath_sinh_impl(module, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | r.real = s.imag; |
| 659 | r.imag = -s.real; |
| 660 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 663 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 664 | /* sinh(infinity + i*y) needs to be dealt with specially */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 665 | static Py_complex sinh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 666 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 667 | /*[clinic input] |
| 668 | cmath.sinh = cmath.acos |
| 669 | |
| 670 | Return the hyperbolic sine of z. |
| 671 | [clinic start generated code]*/ |
| 672 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 673 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 674 | cmath_sinh_impl(PyModuleDef *module, Py_complex z) |
| 675 | /*[clinic end generated code: output=d71fff8298043a95 input=d2d3fc8c1ddfd2dd]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | Py_complex r; |
| 678 | double x_minus_one; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | /* special treatment for sinh(+/-inf + iy) if y is finite and |
| 681 | nonzero */ |
| 682 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 683 | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
| 684 | && (z.imag != 0.)) { |
| 685 | if (z.real > 0) { |
| 686 | r.real = copysign(INF, cos(z.imag)); |
| 687 | r.imag = copysign(INF, sin(z.imag)); |
| 688 | } |
| 689 | else { |
| 690 | r.real = -copysign(INF, cos(z.imag)); |
| 691 | r.imag = copysign(INF, sin(z.imag)); |
| 692 | } |
| 693 | } |
| 694 | else { |
| 695 | r = sinh_special_values[special_type(z.real)] |
| 696 | [special_type(z.imag)]; |
| 697 | } |
| 698 | /* need to set errno = EDOM if y is +/- infinity and x is not |
| 699 | a NaN */ |
| 700 | if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real)) |
| 701 | errno = EDOM; |
| 702 | else |
| 703 | errno = 0; |
| 704 | return r; |
| 705 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
| 708 | x_minus_one = z.real - copysign(1., z.real); |
| 709 | r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E; |
| 710 | r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E; |
| 711 | } else { |
| 712 | r.real = cos(z.imag) * sinh(z.real); |
| 713 | r.imag = sin(z.imag) * cosh(z.real); |
| 714 | } |
| 715 | /* detect overflow, and set errno accordingly */ |
| 716 | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
| 717 | errno = ERANGE; |
| 718 | else |
| 719 | errno = 0; |
| 720 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 723 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 724 | static Py_complex sqrt_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 725 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 726 | /*[clinic input] |
| 727 | cmath.sqrt = cmath.acos |
| 728 | |
| 729 | Return the square root of z. |
| 730 | [clinic start generated code]*/ |
| 731 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 732 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 733 | cmath_sqrt_impl(PyModuleDef *module, Py_complex z) |
| 734 | /*[clinic end generated code: output=b6bda283d0c5a7b4 input=7088b166fc9a58c7]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | /* |
| 737 | Method: use symmetries to reduce to the case when x = z.real and y |
| 738 | = z.imag are nonnegative. Then the real part of the result is |
| 739 | given by |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | s = sqrt((x + hypot(x, y))/2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | and the imaginary part is |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | d = (y/2)/s |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | If either x or y is very large then there's a risk of overflow in |
| 748 | computation of the expression x + hypot(x, y). We can avoid this |
| 749 | by rewriting the formula for s as: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | s = 2*sqrt(x/8 + hypot(x/8, y/8)) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | This costs us two extra multiplications/divisions, but avoids the |
| 754 | overhead of checking for x and y large. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | If both x and y are subnormal then hypot(x, y) may also be |
| 757 | subnormal, so will lack full precision. We solve this by rescaling |
| 758 | x and y by a sufficiently large power of 2 to ensure that x and y |
| 759 | are normal. |
| 760 | */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 761 | |
| 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | Py_complex r; |
| 764 | double s,d; |
| 765 | double ax, ay; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | SPECIAL_VALUE(z, sqrt_special_values); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | if (z.real == 0. && z.imag == 0.) { |
| 770 | r.real = 0.; |
| 771 | r.imag = z.imag; |
| 772 | return r; |
| 773 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | ax = fabs(z.real); |
| 776 | ay = fabs(z.imag); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) { |
| 779 | /* here we catch cases where hypot(ax, ay) is subnormal */ |
| 780 | ax = ldexp(ax, CM_SCALE_UP); |
| 781 | s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))), |
| 782 | CM_SCALE_DOWN); |
| 783 | } else { |
| 784 | ax /= 8.; |
| 785 | s = 2.*sqrt(ax + hypot(ax, ay/8.)); |
| 786 | } |
| 787 | d = ay/(2.*s); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | if (z.real >= 0.) { |
| 790 | r.real = s; |
| 791 | r.imag = copysign(d, z.imag); |
| 792 | } else { |
| 793 | r.real = d; |
| 794 | r.imag = copysign(s, z.imag); |
| 795 | } |
| 796 | errno = 0; |
| 797 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 800 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 801 | /*[clinic input] |
| 802 | cmath.tan = cmath.acos |
| 803 | |
| 804 | Return the tangent of z. |
| 805 | [clinic start generated code]*/ |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 806 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 807 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 808 | cmath_tan_impl(PyModuleDef *module, Py_complex z) |
| 809 | /*[clinic end generated code: output=df374bacf36d99b4 input=fc167e528767888e]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | /* tan(z) = -i tanh(iz) */ |
| 812 | Py_complex s, r; |
| 813 | s.real = -z.imag; |
| 814 | s.imag = z.real; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 815 | s = cmath_tanh_impl(module, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | r.real = s.imag; |
| 817 | r.imag = -s.real; |
| 818 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 821 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 822 | /* tanh(infinity + i*y) needs to be dealt with specially */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 823 | static Py_complex tanh_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 824 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 825 | /*[clinic input] |
| 826 | cmath.tanh = cmath.acos |
| 827 | |
| 828 | Return the hyperbolic tangent of z. |
| 829 | [clinic start generated code]*/ |
| 830 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 831 | static Py_complex |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 832 | cmath_tanh_impl(PyModuleDef *module, Py_complex z) |
| 833 | /*[clinic end generated code: output=f578773d27a18e96 input=22f67f9dc6d29685]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | /* Formula: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) / |
| 838 | (1+tan(y)^2 tanh(x)^2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | To avoid excessive roundoff error, 1-tanh(x)^2 is better computed |
| 841 | as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2 |
| 842 | by 4 exp(-2*x) instead, to avoid possible overflow in the |
| 843 | computation of cosh(x). |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | */ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | Py_complex r; |
| 848 | double tx, ty, cx, txty, denom; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | /* special treatment for tanh(+/-inf + iy) if y is finite and |
| 851 | nonzero */ |
| 852 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 853 | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
| 854 | && (z.imag != 0.)) { |
| 855 | if (z.real > 0) { |
| 856 | r.real = 1.0; |
| 857 | r.imag = copysign(0., |
| 858 | 2.*sin(z.imag)*cos(z.imag)); |
| 859 | } |
| 860 | else { |
| 861 | r.real = -1.0; |
| 862 | r.imag = copysign(0., |
| 863 | 2.*sin(z.imag)*cos(z.imag)); |
| 864 | } |
| 865 | } |
| 866 | else { |
| 867 | r = tanh_special_values[special_type(z.real)] |
| 868 | [special_type(z.imag)]; |
| 869 | } |
| 870 | /* need to set errno = EDOM if z.imag is +/-infinity and |
| 871 | z.real is finite */ |
| 872 | if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real)) |
| 873 | errno = EDOM; |
| 874 | else |
| 875 | errno = 0; |
| 876 | return r; |
| 877 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | /* danger of overflow in 2.*z.imag !*/ |
| 880 | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
| 881 | r.real = copysign(1., z.real); |
| 882 | r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real)); |
| 883 | } else { |
| 884 | tx = tanh(z.real); |
| 885 | ty = tan(z.imag); |
| 886 | cx = 1./cosh(z.real); |
| 887 | txty = tx*ty; |
| 888 | denom = 1. + txty*txty; |
| 889 | r.real = tx*(1.+ty*ty)/denom; |
| 890 | r.imag = ((ty/denom)*cx)*cx; |
| 891 | } |
| 892 | errno = 0; |
| 893 | return r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 896 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 897 | /*[clinic input] |
| 898 | cmath.log |
| 899 | |
| 900 | x: Py_complex |
| 901 | y_obj: object = NULL |
| 902 | / |
| 903 | |
| 904 | The logarithm of z to the given base. |
| 905 | |
| 906 | If the base not specified, returns the natural logarithm (base e) of z. |
| 907 | [clinic start generated code]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 908 | |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 909 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 910 | cmath_log_impl(PyModuleDef *module, Py_complex x, PyObject *y_obj) |
| 911 | /*[clinic end generated code: output=35e2a1e5229b5a46 input=ee0e823a7c6e68ea]*/ |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | Py_complex y; |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | errno = 0; |
| 916 | PyFPE_START_PROTECT("complex function", return 0) |
| 917 | x = c_log(x); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 918 | if (y_obj != NULL) { |
| 919 | y = PyComplex_AsCComplex(y_obj); |
| 920 | if (PyErr_Occurred()) { |
| 921 | return NULL; |
| 922 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | y = c_log(y); |
Antoine Pitrou | de08cb6 | 2014-07-07 19:08:47 -0400 | [diff] [blame] | 924 | x = _Py_c_quot(x, y); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | } |
| 926 | PyFPE_END_PROTECT(x) |
| 927 | if (errno != 0) |
| 928 | return math_error(); |
| 929 | return PyComplex_FromCComplex(x); |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 932 | |
| 933 | /* And now the glue to make them available from Python: */ |
| 934 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 935 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 936 | math_error(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | if (errno == EDOM) |
| 939 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 940 | else if (errno == ERANGE) |
| 941 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 942 | else /* Unexpected math error */ |
| 943 | PyErr_SetFromErrno(PyExc_ValueError); |
| 944 | return NULL; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 947 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 948 | /*[clinic input] |
| 949 | cmath.phase |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 950 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 951 | z: Py_complex |
| 952 | / |
| 953 | |
| 954 | Return argument, also known as the phase angle, of a complex. |
| 955 | [clinic start generated code]*/ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 956 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 957 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 958 | cmath_phase_impl(PyModuleDef *module, Py_complex z) |
| 959 | /*[clinic end generated code: output=e09eaf373cb624c3 input=5cf75228ba94b69d]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | double phi; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | errno = 0; |
| 964 | PyFPE_START_PROTECT("arg function", return 0) |
| 965 | phi = c_atan2(z); |
| 966 | PyFPE_END_PROTECT(phi) |
| 967 | if (errno != 0) |
| 968 | return math_error(); |
| 969 | else |
| 970 | return PyFloat_FromDouble(phi); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 973 | /*[clinic input] |
| 974 | cmath.polar |
| 975 | |
| 976 | z: Py_complex |
| 977 | / |
| 978 | |
| 979 | Convert a complex from rectangular coordinates to polar coordinates. |
| 980 | |
| 981 | r is the distance from 0 and phi the phase angle. |
| 982 | [clinic start generated code]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 983 | |
| 984 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 985 | cmath_polar_impl(PyModuleDef *module, Py_complex z) |
| 986 | /*[clinic end generated code: output=07d41b16c877875a input=26c353574fd1a861]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | double r, phi; |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | PyFPE_START_PROTECT("polar function", return 0) |
| 991 | phi = c_atan2(z); /* should not cause any exception */ |
Antoine Pitrou | de08cb6 | 2014-07-07 19:08:47 -0400 | [diff] [blame] | 992 | r = _Py_c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | PyFPE_END_PROTECT(r) |
| 994 | if (errno != 0) |
| 995 | return math_error(); |
| 996 | else |
| 997 | return Py_BuildValue("dd", r, phi); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1000 | /* |
| 1001 | rect() isn't covered by the C99 standard, but it's not too hard to |
| 1002 | figure out 'spirit of C99' rules for special value handing: |
| 1003 | |
| 1004 | rect(x, t) should behave like exp(log(x) + it) for positive-signed x |
| 1005 | rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x |
| 1006 | rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0) |
| 1007 | gives nan +- i0 with the sign of the imaginary part unspecified. |
| 1008 | |
| 1009 | */ |
| 1010 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1011 | static Py_complex rect_special_values[7][7]; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1012 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1013 | /*[clinic input] |
| 1014 | cmath.rect |
| 1015 | |
| 1016 | r: double |
| 1017 | phi: double |
| 1018 | / |
| 1019 | |
| 1020 | Convert from polar coordinates to rectangular coordinates. |
| 1021 | [clinic start generated code]*/ |
| 1022 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1023 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1024 | cmath_rect_impl(PyModuleDef *module, double r, double phi) |
| 1025 | /*[clinic end generated code: output=d97a8749bd63e9d5 input=24c5646d147efd69]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | Py_complex z; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | errno = 0; |
| 1029 | PyFPE_START_PROTECT("rect function", return 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | /* deal with special values */ |
| 1032 | if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) { |
| 1033 | /* if r is +/-infinity and phi is finite but nonzero then |
| 1034 | result is (+-INF +-INF i), but we need to compute cos(phi) |
| 1035 | and sin(phi) to figure out the signs. */ |
| 1036 | if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi) |
| 1037 | && (phi != 0.))) { |
| 1038 | if (r > 0) { |
| 1039 | z.real = copysign(INF, cos(phi)); |
| 1040 | z.imag = copysign(INF, sin(phi)); |
| 1041 | } |
| 1042 | else { |
| 1043 | z.real = -copysign(INF, cos(phi)); |
| 1044 | z.imag = -copysign(INF, sin(phi)); |
| 1045 | } |
| 1046 | } |
| 1047 | else { |
| 1048 | z = rect_special_values[special_type(r)] |
| 1049 | [special_type(phi)]; |
| 1050 | } |
| 1051 | /* need to set errno = EDOM if r is a nonzero number and phi |
| 1052 | is infinite */ |
| 1053 | if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi)) |
| 1054 | errno = EDOM; |
| 1055 | else |
| 1056 | errno = 0; |
| 1057 | } |
Mark Dickinson | 58ceecf | 2013-07-20 17:59:13 +0100 | [diff] [blame] | 1058 | else if (phi == 0.0) { |
| 1059 | /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See |
| 1060 | bugs.python.org/issue18513. */ |
| 1061 | z.real = r; |
| 1062 | z.imag = r * phi; |
| 1063 | errno = 0; |
| 1064 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | else { |
| 1066 | z.real = r * cos(phi); |
| 1067 | z.imag = r * sin(phi); |
| 1068 | errno = 0; |
| 1069 | } |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | PyFPE_END_PROTECT(z) |
| 1072 | if (errno != 0) |
| 1073 | return math_error(); |
| 1074 | else |
| 1075 | return PyComplex_FromCComplex(z); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1078 | /*[clinic input] |
| 1079 | cmath.isfinite = cmath.polar |
| 1080 | |
| 1081 | Return True if both the real and imaginary parts of z are finite, else False. |
| 1082 | [clinic start generated code]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1083 | |
| 1084 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1085 | cmath_isfinite_impl(PyModuleDef *module, Py_complex z) |
| 1086 | /*[clinic end generated code: output=8f6682fa93de45d6 input=848e7ee701895815]*/ |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 1087 | { |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 1088 | return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag)); |
| 1089 | } |
| 1090 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1091 | /*[clinic input] |
| 1092 | cmath.isnan = cmath.polar |
| 1093 | |
| 1094 | Checks if the real or imaginary part of z not a number (NaN). |
| 1095 | [clinic start generated code]*/ |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 1096 | |
| 1097 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1098 | cmath_isnan_impl(PyModuleDef *module, Py_complex z) |
| 1099 | /*[clinic end generated code: output=b85fe8c2047718ee input=71799f5d284c9baf]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag)); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1104 | /*[clinic input] |
| 1105 | cmath.isinf = cmath.polar |
| 1106 | |
| 1107 | Checks if the real or imaginary part of z is infinite. |
| 1108 | [clinic start generated code]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1109 | |
| 1110 | static PyObject * |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1111 | cmath_isinf_impl(PyModuleDef *module, Py_complex z) |
| 1112 | /*[clinic end generated code: output=8ca9c6109e468bf4 input=363df155c7181329]*/ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | return PyBool_FromLong(Py_IS_INFINITY(z.real) || |
| 1115 | Py_IS_INFINITY(z.imag)); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1119 | PyDoc_STRVAR(module_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 1120 | "This module is always available. It provides access to mathematical\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1121 | "functions for complex numbers."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 1122 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 1123 | static PyMethodDef cmath_methods[] = { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame^] | 1124 | CMATH_ACOS_METHODDEF |
| 1125 | CMATH_ACOSH_METHODDEF |
| 1126 | CMATH_ASIN_METHODDEF |
| 1127 | CMATH_ASINH_METHODDEF |
| 1128 | CMATH_ATAN_METHODDEF |
| 1129 | CMATH_ATANH_METHODDEF |
| 1130 | CMATH_COS_METHODDEF |
| 1131 | CMATH_COSH_METHODDEF |
| 1132 | CMATH_EXP_METHODDEF |
| 1133 | CMATH_ISFINITE_METHODDEF |
| 1134 | CMATH_ISINF_METHODDEF |
| 1135 | CMATH_ISNAN_METHODDEF |
| 1136 | CMATH_LOG_METHODDEF |
| 1137 | CMATH_LOG10_METHODDEF |
| 1138 | CMATH_PHASE_METHODDEF |
| 1139 | CMATH_POLAR_METHODDEF |
| 1140 | CMATH_RECT_METHODDEF |
| 1141 | CMATH_SIN_METHODDEF |
| 1142 | CMATH_SINH_METHODDEF |
| 1143 | CMATH_SQRT_METHODDEF |
| 1144 | CMATH_TAN_METHODDEF |
| 1145 | CMATH_TANH_METHODDEF |
| 1146 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1147 | }; |
| 1148 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1149 | |
| 1150 | static struct PyModuleDef cmathmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | PyModuleDef_HEAD_INIT, |
| 1152 | "cmath", |
| 1153 | module_doc, |
| 1154 | -1, |
| 1155 | cmath_methods, |
| 1156 | NULL, |
| 1157 | NULL, |
| 1158 | NULL, |
| 1159 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1160 | }; |
| 1161 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1162 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1163 | PyInit_cmath(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | PyObject *m; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | m = PyModule_Create(&cmathmodule); |
| 1168 | if (m == NULL) |
| 1169 | return NULL; |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | PyModule_AddObject(m, "pi", |
| 1172 | PyFloat_FromDouble(Py_MATH_PI)); |
| 1173 | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | /* initialize special value tables */ |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1176 | |
| 1177 | #define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY } |
| 1178 | #define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p; |
| 1179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | INIT_SPECIAL_VALUES(acos_special_values, { |
| 1181 | C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF) |
| 1182 | C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N) |
| 1183 | C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N) |
| 1184 | C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N) |
| 1185 | C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N) |
| 1186 | C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF) |
| 1187 | C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N) |
| 1188 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | INIT_SPECIAL_VALUES(acosh_special_values, { |
| 1191 | C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N) |
| 1192 | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
| 1193 | C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N) |
| 1194 | C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N) |
| 1195 | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
| 1196 | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
| 1197 | C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N) |
| 1198 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | INIT_SPECIAL_VALUES(asinh_special_values, { |
| 1201 | C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N) |
| 1202 | C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N) |
| 1203 | C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N) |
| 1204 | C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N) |
| 1205 | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
| 1206 | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
| 1207 | C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N) |
| 1208 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | INIT_SPECIAL_VALUES(atanh_special_values, { |
| 1211 | C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N) |
| 1212 | C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N) |
| 1213 | C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N) |
| 1214 | C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N) |
| 1215 | C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N) |
| 1216 | C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N) |
| 1217 | C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N) |
| 1218 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | INIT_SPECIAL_VALUES(cosh_special_values, { |
| 1221 | C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N) |
| 1222 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1223 | C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.) |
| 1224 | C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.) |
| 1225 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1226 | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
| 1227 | C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
| 1228 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | INIT_SPECIAL_VALUES(exp_special_values, { |
| 1231 | C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.) |
| 1232 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1233 | C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N) |
| 1234 | C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N) |
| 1235 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1236 | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
| 1237 | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
| 1238 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | INIT_SPECIAL_VALUES(log_special_values, { |
| 1241 | C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N) |
| 1242 | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
| 1243 | C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N) |
| 1244 | C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N) |
| 1245 | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
| 1246 | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
| 1247 | C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N) |
| 1248 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | INIT_SPECIAL_VALUES(sinh_special_values, { |
| 1251 | C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N) |
| 1252 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1253 | C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N) |
| 1254 | C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N) |
| 1255 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1256 | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
| 1257 | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
| 1258 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | INIT_SPECIAL_VALUES(sqrt_special_values, { |
| 1261 | C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF) |
| 1262 | C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N) |
| 1263 | C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N) |
| 1264 | C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N) |
| 1265 | C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N) |
| 1266 | C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N) |
| 1267 | C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N) |
| 1268 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | INIT_SPECIAL_VALUES(tanh_special_values, { |
| 1271 | C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.) |
| 1272 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1273 | C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N) |
| 1274 | C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N) |
| 1275 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1276 | C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.) |
| 1277 | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
| 1278 | }) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | INIT_SPECIAL_VALUES(rect_special_values, { |
| 1281 | C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N) |
| 1282 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1283 | C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.) |
| 1284 | C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.) |
| 1285 | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
| 1286 | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
| 1287 | C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
| 1288 | }) |
| 1289 | return m; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1290 | } |