Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(cmath_acos__doc__, |
| 6 | "acos($module, z, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return the arc cosine of z."); |
| 10 | |
| 11 | #define CMATH_ACOS_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 12 | {"acos", (PyCFunction)cmath_acos, METH_O, cmath_acos__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 13 | |
| 14 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 15 | cmath_acos_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 16 | |
| 17 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 18 | cmath_acos(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 19 | { |
| 20 | PyObject *return_value = NULL; |
| 21 | Py_complex z; |
| 22 | Py_complex _return_value; |
| 23 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 24 | z = PyComplex_AsCComplex(arg); |
| 25 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 26 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 27 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 28 | /* modifications for z */ |
| 29 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 30 | _return_value = cmath_acos_impl(module, z); |
| 31 | PyFPE_END_PROTECT(_return_value); |
| 32 | if (errno == EDOM) { |
| 33 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 34 | goto exit; |
| 35 | } |
| 36 | else if (errno == ERANGE) { |
| 37 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 38 | goto exit; |
| 39 | } |
| 40 | else { |
| 41 | return_value = PyComplex_FromCComplex(_return_value); |
| 42 | } |
| 43 | |
| 44 | exit: |
| 45 | return return_value; |
| 46 | } |
| 47 | |
| 48 | PyDoc_STRVAR(cmath_acosh__doc__, |
| 49 | "acosh($module, z, /)\n" |
| 50 | "--\n" |
| 51 | "\n" |
Mark Dickinson | cc8617b | 2015-01-11 13:22:44 +0000 | [diff] [blame] | 52 | "Return the inverse hyperbolic cosine of z."); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 53 | |
| 54 | #define CMATH_ACOSH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 55 | {"acosh", (PyCFunction)cmath_acosh, METH_O, cmath_acosh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 56 | |
| 57 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 58 | cmath_acosh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 59 | |
| 60 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 61 | cmath_acosh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 62 | { |
| 63 | PyObject *return_value = NULL; |
| 64 | Py_complex z; |
| 65 | Py_complex _return_value; |
| 66 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 67 | z = PyComplex_AsCComplex(arg); |
| 68 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 69 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 70 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 71 | /* modifications for z */ |
| 72 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 73 | _return_value = cmath_acosh_impl(module, z); |
| 74 | PyFPE_END_PROTECT(_return_value); |
| 75 | if (errno == EDOM) { |
| 76 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 77 | goto exit; |
| 78 | } |
| 79 | else if (errno == ERANGE) { |
| 80 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 81 | goto exit; |
| 82 | } |
| 83 | else { |
| 84 | return_value = PyComplex_FromCComplex(_return_value); |
| 85 | } |
| 86 | |
| 87 | exit: |
| 88 | return return_value; |
| 89 | } |
| 90 | |
| 91 | PyDoc_STRVAR(cmath_asin__doc__, |
| 92 | "asin($module, z, /)\n" |
| 93 | "--\n" |
| 94 | "\n" |
| 95 | "Return the arc sine of z."); |
| 96 | |
| 97 | #define CMATH_ASIN_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 98 | {"asin", (PyCFunction)cmath_asin, METH_O, cmath_asin__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 99 | |
| 100 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 101 | cmath_asin_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 102 | |
| 103 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 104 | cmath_asin(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 105 | { |
| 106 | PyObject *return_value = NULL; |
| 107 | Py_complex z; |
| 108 | Py_complex _return_value; |
| 109 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 110 | z = PyComplex_AsCComplex(arg); |
| 111 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 112 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 113 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 114 | /* modifications for z */ |
| 115 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 116 | _return_value = cmath_asin_impl(module, z); |
| 117 | PyFPE_END_PROTECT(_return_value); |
| 118 | if (errno == EDOM) { |
| 119 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 120 | goto exit; |
| 121 | } |
| 122 | else if (errno == ERANGE) { |
| 123 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 124 | goto exit; |
| 125 | } |
| 126 | else { |
| 127 | return_value = PyComplex_FromCComplex(_return_value); |
| 128 | } |
| 129 | |
| 130 | exit: |
| 131 | return return_value; |
| 132 | } |
| 133 | |
| 134 | PyDoc_STRVAR(cmath_asinh__doc__, |
| 135 | "asinh($module, z, /)\n" |
| 136 | "--\n" |
| 137 | "\n" |
Mark Dickinson | cc8617b | 2015-01-11 13:22:44 +0000 | [diff] [blame] | 138 | "Return the inverse hyperbolic sine of z."); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 139 | |
| 140 | #define CMATH_ASINH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 141 | {"asinh", (PyCFunction)cmath_asinh, METH_O, cmath_asinh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 142 | |
| 143 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 144 | cmath_asinh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 145 | |
| 146 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 147 | cmath_asinh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 148 | { |
| 149 | PyObject *return_value = NULL; |
| 150 | Py_complex z; |
| 151 | Py_complex _return_value; |
| 152 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 153 | z = PyComplex_AsCComplex(arg); |
| 154 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 155 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 156 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 157 | /* modifications for z */ |
| 158 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 159 | _return_value = cmath_asinh_impl(module, z); |
| 160 | PyFPE_END_PROTECT(_return_value); |
| 161 | if (errno == EDOM) { |
| 162 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 163 | goto exit; |
| 164 | } |
| 165 | else if (errno == ERANGE) { |
| 166 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 167 | goto exit; |
| 168 | } |
| 169 | else { |
| 170 | return_value = PyComplex_FromCComplex(_return_value); |
| 171 | } |
| 172 | |
| 173 | exit: |
| 174 | return return_value; |
| 175 | } |
| 176 | |
| 177 | PyDoc_STRVAR(cmath_atan__doc__, |
| 178 | "atan($module, z, /)\n" |
| 179 | "--\n" |
| 180 | "\n" |
| 181 | "Return the arc tangent of z."); |
| 182 | |
| 183 | #define CMATH_ATAN_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 184 | {"atan", (PyCFunction)cmath_atan, METH_O, cmath_atan__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 185 | |
| 186 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 187 | cmath_atan_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 188 | |
| 189 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 190 | cmath_atan(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 191 | { |
| 192 | PyObject *return_value = NULL; |
| 193 | Py_complex z; |
| 194 | Py_complex _return_value; |
| 195 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 196 | z = PyComplex_AsCComplex(arg); |
| 197 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 198 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 199 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 200 | /* modifications for z */ |
| 201 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 202 | _return_value = cmath_atan_impl(module, z); |
| 203 | PyFPE_END_PROTECT(_return_value); |
| 204 | if (errno == EDOM) { |
| 205 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 206 | goto exit; |
| 207 | } |
| 208 | else if (errno == ERANGE) { |
| 209 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 210 | goto exit; |
| 211 | } |
| 212 | else { |
| 213 | return_value = PyComplex_FromCComplex(_return_value); |
| 214 | } |
| 215 | |
| 216 | exit: |
| 217 | return return_value; |
| 218 | } |
| 219 | |
| 220 | PyDoc_STRVAR(cmath_atanh__doc__, |
| 221 | "atanh($module, z, /)\n" |
| 222 | "--\n" |
| 223 | "\n" |
Mark Dickinson | cc8617b | 2015-01-11 13:22:44 +0000 | [diff] [blame] | 224 | "Return the inverse hyperbolic tangent of z."); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 225 | |
| 226 | #define CMATH_ATANH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 227 | {"atanh", (PyCFunction)cmath_atanh, METH_O, cmath_atanh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 228 | |
| 229 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 230 | cmath_atanh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 231 | |
| 232 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 233 | cmath_atanh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 234 | { |
| 235 | PyObject *return_value = NULL; |
| 236 | Py_complex z; |
| 237 | Py_complex _return_value; |
| 238 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 239 | z = PyComplex_AsCComplex(arg); |
| 240 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 241 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 242 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 243 | /* modifications for z */ |
| 244 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 245 | _return_value = cmath_atanh_impl(module, z); |
| 246 | PyFPE_END_PROTECT(_return_value); |
| 247 | if (errno == EDOM) { |
| 248 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 249 | goto exit; |
| 250 | } |
| 251 | else if (errno == ERANGE) { |
| 252 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 253 | goto exit; |
| 254 | } |
| 255 | else { |
| 256 | return_value = PyComplex_FromCComplex(_return_value); |
| 257 | } |
| 258 | |
| 259 | exit: |
| 260 | return return_value; |
| 261 | } |
| 262 | |
| 263 | PyDoc_STRVAR(cmath_cos__doc__, |
| 264 | "cos($module, z, /)\n" |
| 265 | "--\n" |
| 266 | "\n" |
| 267 | "Return the cosine of z."); |
| 268 | |
| 269 | #define CMATH_COS_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 270 | {"cos", (PyCFunction)cmath_cos, METH_O, cmath_cos__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 271 | |
| 272 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 273 | cmath_cos_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 274 | |
| 275 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 276 | cmath_cos(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 277 | { |
| 278 | PyObject *return_value = NULL; |
| 279 | Py_complex z; |
| 280 | Py_complex _return_value; |
| 281 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 282 | z = PyComplex_AsCComplex(arg); |
| 283 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 284 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 285 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 286 | /* modifications for z */ |
| 287 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 288 | _return_value = cmath_cos_impl(module, z); |
| 289 | PyFPE_END_PROTECT(_return_value); |
| 290 | if (errno == EDOM) { |
| 291 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 292 | goto exit; |
| 293 | } |
| 294 | else if (errno == ERANGE) { |
| 295 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 296 | goto exit; |
| 297 | } |
| 298 | else { |
| 299 | return_value = PyComplex_FromCComplex(_return_value); |
| 300 | } |
| 301 | |
| 302 | exit: |
| 303 | return return_value; |
| 304 | } |
| 305 | |
| 306 | PyDoc_STRVAR(cmath_cosh__doc__, |
| 307 | "cosh($module, z, /)\n" |
| 308 | "--\n" |
| 309 | "\n" |
| 310 | "Return the hyperbolic cosine of z."); |
| 311 | |
| 312 | #define CMATH_COSH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 313 | {"cosh", (PyCFunction)cmath_cosh, METH_O, cmath_cosh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 314 | |
| 315 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 316 | cmath_cosh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 317 | |
| 318 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 319 | cmath_cosh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 320 | { |
| 321 | PyObject *return_value = NULL; |
| 322 | Py_complex z; |
| 323 | Py_complex _return_value; |
| 324 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 325 | z = PyComplex_AsCComplex(arg); |
| 326 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 327 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 328 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 329 | /* modifications for z */ |
| 330 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 331 | _return_value = cmath_cosh_impl(module, z); |
| 332 | PyFPE_END_PROTECT(_return_value); |
| 333 | if (errno == EDOM) { |
| 334 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 335 | goto exit; |
| 336 | } |
| 337 | else if (errno == ERANGE) { |
| 338 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 339 | goto exit; |
| 340 | } |
| 341 | else { |
| 342 | return_value = PyComplex_FromCComplex(_return_value); |
| 343 | } |
| 344 | |
| 345 | exit: |
| 346 | return return_value; |
| 347 | } |
| 348 | |
| 349 | PyDoc_STRVAR(cmath_exp__doc__, |
| 350 | "exp($module, z, /)\n" |
| 351 | "--\n" |
| 352 | "\n" |
| 353 | "Return the exponential value e**z."); |
| 354 | |
| 355 | #define CMATH_EXP_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 356 | {"exp", (PyCFunction)cmath_exp, METH_O, cmath_exp__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 357 | |
| 358 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 359 | cmath_exp_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 360 | |
| 361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 362 | cmath_exp(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 363 | { |
| 364 | PyObject *return_value = NULL; |
| 365 | Py_complex z; |
| 366 | Py_complex _return_value; |
| 367 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 368 | z = PyComplex_AsCComplex(arg); |
| 369 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 370 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 371 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 372 | /* modifications for z */ |
| 373 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 374 | _return_value = cmath_exp_impl(module, z); |
| 375 | PyFPE_END_PROTECT(_return_value); |
| 376 | if (errno == EDOM) { |
| 377 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 378 | goto exit; |
| 379 | } |
| 380 | else if (errno == ERANGE) { |
| 381 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 382 | goto exit; |
| 383 | } |
| 384 | else { |
| 385 | return_value = PyComplex_FromCComplex(_return_value); |
| 386 | } |
| 387 | |
| 388 | exit: |
| 389 | return return_value; |
| 390 | } |
| 391 | |
| 392 | PyDoc_STRVAR(cmath_log10__doc__, |
| 393 | "log10($module, z, /)\n" |
| 394 | "--\n" |
| 395 | "\n" |
| 396 | "Return the base-10 logarithm of z."); |
| 397 | |
| 398 | #define CMATH_LOG10_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 399 | {"log10", (PyCFunction)cmath_log10, METH_O, cmath_log10__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 400 | |
| 401 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 402 | cmath_log10_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 403 | |
| 404 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 405 | cmath_log10(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 406 | { |
| 407 | PyObject *return_value = NULL; |
| 408 | Py_complex z; |
| 409 | Py_complex _return_value; |
| 410 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 411 | z = PyComplex_AsCComplex(arg); |
| 412 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 413 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 414 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 415 | /* modifications for z */ |
| 416 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 417 | _return_value = cmath_log10_impl(module, z); |
| 418 | PyFPE_END_PROTECT(_return_value); |
| 419 | if (errno == EDOM) { |
| 420 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 421 | goto exit; |
| 422 | } |
| 423 | else if (errno == ERANGE) { |
| 424 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 425 | goto exit; |
| 426 | } |
| 427 | else { |
| 428 | return_value = PyComplex_FromCComplex(_return_value); |
| 429 | } |
| 430 | |
| 431 | exit: |
| 432 | return return_value; |
| 433 | } |
| 434 | |
| 435 | PyDoc_STRVAR(cmath_sin__doc__, |
| 436 | "sin($module, z, /)\n" |
| 437 | "--\n" |
| 438 | "\n" |
| 439 | "Return the sine of z."); |
| 440 | |
| 441 | #define CMATH_SIN_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 442 | {"sin", (PyCFunction)cmath_sin, METH_O, cmath_sin__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 443 | |
| 444 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 445 | cmath_sin_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 446 | |
| 447 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 448 | cmath_sin(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 449 | { |
| 450 | PyObject *return_value = NULL; |
| 451 | Py_complex z; |
| 452 | Py_complex _return_value; |
| 453 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 454 | z = PyComplex_AsCComplex(arg); |
| 455 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 456 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 457 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 458 | /* modifications for z */ |
| 459 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 460 | _return_value = cmath_sin_impl(module, z); |
| 461 | PyFPE_END_PROTECT(_return_value); |
| 462 | if (errno == EDOM) { |
| 463 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 464 | goto exit; |
| 465 | } |
| 466 | else if (errno == ERANGE) { |
| 467 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 468 | goto exit; |
| 469 | } |
| 470 | else { |
| 471 | return_value = PyComplex_FromCComplex(_return_value); |
| 472 | } |
| 473 | |
| 474 | exit: |
| 475 | return return_value; |
| 476 | } |
| 477 | |
| 478 | PyDoc_STRVAR(cmath_sinh__doc__, |
| 479 | "sinh($module, z, /)\n" |
| 480 | "--\n" |
| 481 | "\n" |
| 482 | "Return the hyperbolic sine of z."); |
| 483 | |
| 484 | #define CMATH_SINH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 485 | {"sinh", (PyCFunction)cmath_sinh, METH_O, cmath_sinh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 486 | |
| 487 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 488 | cmath_sinh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 489 | |
| 490 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 491 | cmath_sinh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 492 | { |
| 493 | PyObject *return_value = NULL; |
| 494 | Py_complex z; |
| 495 | Py_complex _return_value; |
| 496 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 497 | z = PyComplex_AsCComplex(arg); |
| 498 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 499 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 500 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 501 | /* modifications for z */ |
| 502 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 503 | _return_value = cmath_sinh_impl(module, z); |
| 504 | PyFPE_END_PROTECT(_return_value); |
| 505 | if (errno == EDOM) { |
| 506 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 507 | goto exit; |
| 508 | } |
| 509 | else if (errno == ERANGE) { |
| 510 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 511 | goto exit; |
| 512 | } |
| 513 | else { |
| 514 | return_value = PyComplex_FromCComplex(_return_value); |
| 515 | } |
| 516 | |
| 517 | exit: |
| 518 | return return_value; |
| 519 | } |
| 520 | |
| 521 | PyDoc_STRVAR(cmath_sqrt__doc__, |
| 522 | "sqrt($module, z, /)\n" |
| 523 | "--\n" |
| 524 | "\n" |
| 525 | "Return the square root of z."); |
| 526 | |
| 527 | #define CMATH_SQRT_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 528 | {"sqrt", (PyCFunction)cmath_sqrt, METH_O, cmath_sqrt__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 529 | |
| 530 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 531 | cmath_sqrt_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 532 | |
| 533 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 534 | cmath_sqrt(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 535 | { |
| 536 | PyObject *return_value = NULL; |
| 537 | Py_complex z; |
| 538 | Py_complex _return_value; |
| 539 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 540 | z = PyComplex_AsCComplex(arg); |
| 541 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 542 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 543 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 544 | /* modifications for z */ |
| 545 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 546 | _return_value = cmath_sqrt_impl(module, z); |
| 547 | PyFPE_END_PROTECT(_return_value); |
| 548 | if (errno == EDOM) { |
| 549 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 550 | goto exit; |
| 551 | } |
| 552 | else if (errno == ERANGE) { |
| 553 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 554 | goto exit; |
| 555 | } |
| 556 | else { |
| 557 | return_value = PyComplex_FromCComplex(_return_value); |
| 558 | } |
| 559 | |
| 560 | exit: |
| 561 | return return_value; |
| 562 | } |
| 563 | |
| 564 | PyDoc_STRVAR(cmath_tan__doc__, |
| 565 | "tan($module, z, /)\n" |
| 566 | "--\n" |
| 567 | "\n" |
| 568 | "Return the tangent of z."); |
| 569 | |
| 570 | #define CMATH_TAN_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 571 | {"tan", (PyCFunction)cmath_tan, METH_O, cmath_tan__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 572 | |
| 573 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 574 | cmath_tan_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 575 | |
| 576 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 577 | cmath_tan(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 578 | { |
| 579 | PyObject *return_value = NULL; |
| 580 | Py_complex z; |
| 581 | Py_complex _return_value; |
| 582 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 583 | z = PyComplex_AsCComplex(arg); |
| 584 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 585 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 586 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 587 | /* modifications for z */ |
| 588 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 589 | _return_value = cmath_tan_impl(module, z); |
| 590 | PyFPE_END_PROTECT(_return_value); |
| 591 | if (errno == EDOM) { |
| 592 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 593 | goto exit; |
| 594 | } |
| 595 | else if (errno == ERANGE) { |
| 596 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 597 | goto exit; |
| 598 | } |
| 599 | else { |
| 600 | return_value = PyComplex_FromCComplex(_return_value); |
| 601 | } |
| 602 | |
| 603 | exit: |
| 604 | return return_value; |
| 605 | } |
| 606 | |
| 607 | PyDoc_STRVAR(cmath_tanh__doc__, |
| 608 | "tanh($module, z, /)\n" |
| 609 | "--\n" |
| 610 | "\n" |
| 611 | "Return the hyperbolic tangent of z."); |
| 612 | |
| 613 | #define CMATH_TANH_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 614 | {"tanh", (PyCFunction)cmath_tanh, METH_O, cmath_tanh__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 615 | |
| 616 | static Py_complex |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 617 | cmath_tanh_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 618 | |
| 619 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 620 | cmath_tanh(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 621 | { |
| 622 | PyObject *return_value = NULL; |
| 623 | Py_complex z; |
| 624 | Py_complex _return_value; |
| 625 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 626 | z = PyComplex_AsCComplex(arg); |
| 627 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 628 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 629 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 630 | /* modifications for z */ |
| 631 | errno = 0; PyFPE_START_PROTECT("complex function", goto exit); |
| 632 | _return_value = cmath_tanh_impl(module, z); |
| 633 | PyFPE_END_PROTECT(_return_value); |
| 634 | if (errno == EDOM) { |
| 635 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 636 | goto exit; |
| 637 | } |
| 638 | else if (errno == ERANGE) { |
| 639 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 640 | goto exit; |
| 641 | } |
| 642 | else { |
| 643 | return_value = PyComplex_FromCComplex(_return_value); |
| 644 | } |
| 645 | |
| 646 | exit: |
| 647 | return return_value; |
| 648 | } |
| 649 | |
| 650 | PyDoc_STRVAR(cmath_log__doc__, |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 651 | "log($module, z, base=<unrepresentable>, /)\n" |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 652 | "--\n" |
| 653 | "\n" |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 654 | "log(z[, base]) -> the logarithm of z to the given base.\n" |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 655 | "\n" |
| 656 | "If the base not specified, returns the natural logarithm (base e) of z."); |
| 657 | |
| 658 | #define CMATH_LOG_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 659 | {"log", (PyCFunction)(void(*)(void))cmath_log, METH_FASTCALL, cmath_log__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 660 | |
| 661 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 662 | cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 663 | |
| 664 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 665 | cmath_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 666 | { |
| 667 | PyObject *return_value = NULL; |
| 668 | Py_complex x; |
| 669 | PyObject *y_obj = NULL; |
| 670 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 671 | if (!_PyArg_CheckPositional("log", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 672 | goto exit; |
| 673 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 674 | x = PyComplex_AsCComplex(args[0]); |
| 675 | if (PyErr_Occurred()) { |
| 676 | goto exit; |
| 677 | } |
| 678 | if (nargs < 2) { |
| 679 | goto skip_optional; |
| 680 | } |
| 681 | y_obj = args[1]; |
| 682 | skip_optional: |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 683 | return_value = cmath_log_impl(module, x, y_obj); |
| 684 | |
| 685 | exit: |
| 686 | return return_value; |
| 687 | } |
| 688 | |
| 689 | PyDoc_STRVAR(cmath_phase__doc__, |
| 690 | "phase($module, z, /)\n" |
| 691 | "--\n" |
| 692 | "\n" |
| 693 | "Return argument, also known as the phase angle, of a complex."); |
| 694 | |
| 695 | #define CMATH_PHASE_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 696 | {"phase", (PyCFunction)cmath_phase, METH_O, cmath_phase__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 697 | |
| 698 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 699 | cmath_phase_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 700 | |
| 701 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 702 | cmath_phase(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 703 | { |
| 704 | PyObject *return_value = NULL; |
| 705 | Py_complex z; |
| 706 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 707 | z = PyComplex_AsCComplex(arg); |
| 708 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 709 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 710 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 711 | return_value = cmath_phase_impl(module, z); |
| 712 | |
| 713 | exit: |
| 714 | return return_value; |
| 715 | } |
| 716 | |
| 717 | PyDoc_STRVAR(cmath_polar__doc__, |
| 718 | "polar($module, z, /)\n" |
| 719 | "--\n" |
| 720 | "\n" |
| 721 | "Convert a complex from rectangular coordinates to polar coordinates.\n" |
| 722 | "\n" |
| 723 | "r is the distance from 0 and phi the phase angle."); |
| 724 | |
| 725 | #define CMATH_POLAR_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 726 | {"polar", (PyCFunction)cmath_polar, METH_O, cmath_polar__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 727 | |
| 728 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 729 | cmath_polar_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 730 | |
| 731 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 732 | cmath_polar(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 733 | { |
| 734 | PyObject *return_value = NULL; |
| 735 | Py_complex z; |
| 736 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 737 | z = PyComplex_AsCComplex(arg); |
| 738 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 739 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 740 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 741 | return_value = cmath_polar_impl(module, z); |
| 742 | |
| 743 | exit: |
| 744 | return return_value; |
| 745 | } |
| 746 | |
| 747 | PyDoc_STRVAR(cmath_rect__doc__, |
| 748 | "rect($module, r, phi, /)\n" |
| 749 | "--\n" |
| 750 | "\n" |
| 751 | "Convert from polar coordinates to rectangular coordinates."); |
| 752 | |
| 753 | #define CMATH_RECT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 754 | {"rect", (PyCFunction)(void(*)(void))cmath_rect, METH_FASTCALL, cmath_rect__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 755 | |
| 756 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 757 | cmath_rect_impl(PyObject *module, double r, double phi); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 758 | |
| 759 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 760 | cmath_rect(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 761 | { |
| 762 | PyObject *return_value = NULL; |
| 763 | double r; |
| 764 | double phi; |
| 765 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 766 | if (!_PyArg_CheckPositional("rect", nargs, 2, 2)) { |
| 767 | goto exit; |
| 768 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 769 | if (PyFloat_CheckExact(args[0])) { |
| 770 | r = PyFloat_AS_DOUBLE(args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 771 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 772 | else |
| 773 | { |
| 774 | r = PyFloat_AsDouble(args[0]); |
| 775 | if (r == -1.0 && PyErr_Occurred()) { |
| 776 | goto exit; |
| 777 | } |
| 778 | } |
| 779 | if (PyFloat_CheckExact(args[1])) { |
| 780 | phi = PyFloat_AS_DOUBLE(args[1]); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | phi = PyFloat_AsDouble(args[1]); |
| 785 | if (phi == -1.0 && PyErr_Occurred()) { |
| 786 | goto exit; |
| 787 | } |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 788 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 789 | return_value = cmath_rect_impl(module, r, phi); |
| 790 | |
| 791 | exit: |
| 792 | return return_value; |
| 793 | } |
| 794 | |
| 795 | PyDoc_STRVAR(cmath_isfinite__doc__, |
| 796 | "isfinite($module, z, /)\n" |
| 797 | "--\n" |
| 798 | "\n" |
| 799 | "Return True if both the real and imaginary parts of z are finite, else False."); |
| 800 | |
| 801 | #define CMATH_ISFINITE_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 802 | {"isfinite", (PyCFunction)cmath_isfinite, METH_O, cmath_isfinite__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 803 | |
| 804 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 805 | cmath_isfinite_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 806 | |
| 807 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 808 | cmath_isfinite(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 809 | { |
| 810 | PyObject *return_value = NULL; |
| 811 | Py_complex z; |
| 812 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 813 | z = PyComplex_AsCComplex(arg); |
| 814 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 815 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 816 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 817 | return_value = cmath_isfinite_impl(module, z); |
| 818 | |
| 819 | exit: |
| 820 | return return_value; |
| 821 | } |
| 822 | |
| 823 | PyDoc_STRVAR(cmath_isnan__doc__, |
| 824 | "isnan($module, z, /)\n" |
| 825 | "--\n" |
| 826 | "\n" |
| 827 | "Checks if the real or imaginary part of z not a number (NaN)."); |
| 828 | |
| 829 | #define CMATH_ISNAN_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 830 | {"isnan", (PyCFunction)cmath_isnan, METH_O, cmath_isnan__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 831 | |
| 832 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 833 | cmath_isnan_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 834 | |
| 835 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 836 | cmath_isnan(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 837 | { |
| 838 | PyObject *return_value = NULL; |
| 839 | Py_complex z; |
| 840 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 841 | z = PyComplex_AsCComplex(arg); |
| 842 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 843 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 844 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 845 | return_value = cmath_isnan_impl(module, z); |
| 846 | |
| 847 | exit: |
| 848 | return return_value; |
| 849 | } |
| 850 | |
| 851 | PyDoc_STRVAR(cmath_isinf__doc__, |
| 852 | "isinf($module, z, /)\n" |
| 853 | "--\n" |
| 854 | "\n" |
| 855 | "Checks if the real or imaginary part of z is infinite."); |
| 856 | |
| 857 | #define CMATH_ISINF_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 858 | {"isinf", (PyCFunction)cmath_isinf, METH_O, cmath_isinf__doc__}, |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 859 | |
| 860 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 861 | cmath_isinf_impl(PyObject *module, Py_complex z); |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 862 | |
| 863 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 864 | cmath_isinf(PyObject *module, PyObject *arg) |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 865 | { |
| 866 | PyObject *return_value = NULL; |
| 867 | Py_complex z; |
| 868 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 869 | z = PyComplex_AsCComplex(arg); |
| 870 | if (PyErr_Occurred()) { |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 871 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 872 | } |
Brett Cannon | b0fc490 | 2014-10-14 17:37:02 -0400 | [diff] [blame] | 873 | return_value = cmath_isinf_impl(module, z); |
| 874 | |
| 875 | exit: |
| 876 | return return_value; |
| 877 | } |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 878 | |
| 879 | PyDoc_STRVAR(cmath_isclose__doc__, |
| 880 | "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n" |
| 881 | "--\n" |
| 882 | "\n" |
| 883 | "Determine whether two complex numbers are close in value.\n" |
| 884 | "\n" |
| 885 | " rel_tol\n" |
| 886 | " maximum difference for being considered \"close\", relative to the\n" |
| 887 | " magnitude of the input values\n" |
| 888 | " abs_tol\n" |
| 889 | " maximum difference for being considered \"close\", regardless of the\n" |
| 890 | " magnitude of the input values\n" |
| 891 | "\n" |
| 892 | "Return True if a is close in value to b, and False otherwise.\n" |
| 893 | "\n" |
| 894 | "For the values to be considered close, the difference between them must be\n" |
| 895 | "smaller than at least one of the tolerances.\n" |
| 896 | "\n" |
| 897 | "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is\n" |
| 898 | "not close to anything, even itself. inf and -inf are only close to themselves."); |
| 899 | |
| 900 | #define CMATH_ISCLOSE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 901 | {"isclose", (PyCFunction)(void(*)(void))cmath_isclose, METH_FASTCALL|METH_KEYWORDS, cmath_isclose__doc__}, |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 902 | |
| 903 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 904 | cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b, |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 905 | double rel_tol, double abs_tol); |
| 906 | |
| 907 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 908 | cmath_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 909 | { |
| 910 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 911 | static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 912 | static _PyArg_Parser _parser = {NULL, _keywords, "isclose", 0}; |
| 913 | PyObject *argsbuf[4]; |
| 914 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 915 | Py_complex a; |
| 916 | Py_complex b; |
| 917 | double rel_tol = 1e-09; |
| 918 | double abs_tol = 0.0; |
| 919 | int _return_value; |
| 920 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 921 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 922 | if (!args) { |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 923 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 924 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 925 | a = PyComplex_AsCComplex(args[0]); |
| 926 | if (PyErr_Occurred()) { |
| 927 | goto exit; |
| 928 | } |
| 929 | b = PyComplex_AsCComplex(args[1]); |
| 930 | if (PyErr_Occurred()) { |
| 931 | goto exit; |
| 932 | } |
| 933 | if (!noptargs) { |
| 934 | goto skip_optional_kwonly; |
| 935 | } |
| 936 | if (args[2]) { |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 937 | if (PyFloat_CheckExact(args[2])) { |
| 938 | rel_tol = PyFloat_AS_DOUBLE(args[2]); |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | rel_tol = PyFloat_AsDouble(args[2]); |
| 943 | if (rel_tol == -1.0 && PyErr_Occurred()) { |
| 944 | goto exit; |
| 945 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 946 | } |
| 947 | if (!--noptargs) { |
| 948 | goto skip_optional_kwonly; |
| 949 | } |
| 950 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 951 | if (PyFloat_CheckExact(args[3])) { |
| 952 | abs_tol = PyFloat_AS_DOUBLE(args[3]); |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | abs_tol = PyFloat_AsDouble(args[3]); |
| 957 | if (abs_tol == -1.0 && PyErr_Occurred()) { |
| 958 | goto exit; |
| 959 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 960 | } |
| 961 | skip_optional_kwonly: |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 962 | _return_value = cmath_isclose_impl(module, a, b, rel_tol, abs_tol); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 963 | if ((_return_value == -1) && PyErr_Occurred()) { |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 964 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 965 | } |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 966 | return_value = PyBool_FromLong((long)_return_value); |
| 967 | |
| 968 | exit: |
| 969 | return return_value; |
| 970 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 971 | /*[clinic end generated code: output=3edc4484b10ae752 input=a9049054013a1b77]*/ |