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