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