Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(math_gcd__doc__, |
| 6 | "gcd($module, x, y, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "greatest common divisor of x and y"); |
| 10 | |
| 11 | #define MATH_GCD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 12 | {"gcd", (PyCFunction)(void(*)(void))math_gcd, METH_FASTCALL, math_gcd__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 13 | |
| 14 | static PyObject * |
| 15 | math_gcd_impl(PyObject *module, PyObject *a, PyObject *b); |
| 16 | |
| 17 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 18 | math_gcd(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 19 | { |
| 20 | PyObject *return_value = NULL; |
| 21 | PyObject *a; |
| 22 | PyObject *b; |
| 23 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 24 | if (!_PyArg_CheckPositional("gcd", nargs, 2, 2)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 25 | goto exit; |
| 26 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 27 | a = args[0]; |
| 28 | b = args[1]; |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 29 | return_value = math_gcd_impl(module, a, b); |
| 30 | |
| 31 | exit: |
| 32 | return return_value; |
| 33 | } |
| 34 | |
| 35 | PyDoc_STRVAR(math_ceil__doc__, |
| 36 | "ceil($module, x, /)\n" |
| 37 | "--\n" |
| 38 | "\n" |
| 39 | "Return the ceiling of x as an Integral.\n" |
| 40 | "\n" |
| 41 | "This is the smallest integer >= x."); |
| 42 | |
| 43 | #define MATH_CEIL_METHODDEF \ |
| 44 | {"ceil", (PyCFunction)math_ceil, METH_O, math_ceil__doc__}, |
| 45 | |
| 46 | PyDoc_STRVAR(math_floor__doc__, |
| 47 | "floor($module, x, /)\n" |
| 48 | "--\n" |
| 49 | "\n" |
| 50 | "Return the floor of x as an Integral.\n" |
| 51 | "\n" |
| 52 | "This is the largest integer <= x."); |
| 53 | |
| 54 | #define MATH_FLOOR_METHODDEF \ |
| 55 | {"floor", (PyCFunction)math_floor, METH_O, math_floor__doc__}, |
| 56 | |
| 57 | PyDoc_STRVAR(math_fsum__doc__, |
| 58 | "fsum($module, seq, /)\n" |
| 59 | "--\n" |
| 60 | "\n" |
| 61 | "Return an accurate floating point sum of values in the iterable seq.\n" |
| 62 | "\n" |
| 63 | "Assumes IEEE-754 floating point arithmetic."); |
| 64 | |
| 65 | #define MATH_FSUM_METHODDEF \ |
| 66 | {"fsum", (PyCFunction)math_fsum, METH_O, math_fsum__doc__}, |
| 67 | |
Mark Dickinson | 73934b9 | 2019-05-18 12:29:50 +0100 | [diff] [blame^] | 68 | PyDoc_STRVAR(math_isqrt__doc__, |
| 69 | "isqrt($module, n, /)\n" |
| 70 | "--\n" |
| 71 | "\n" |
| 72 | "Return the integer part of the square root of the input."); |
| 73 | |
| 74 | #define MATH_ISQRT_METHODDEF \ |
| 75 | {"isqrt", (PyCFunction)math_isqrt, METH_O, math_isqrt__doc__}, |
| 76 | |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 77 | PyDoc_STRVAR(math_factorial__doc__, |
| 78 | "factorial($module, x, /)\n" |
| 79 | "--\n" |
| 80 | "\n" |
| 81 | "Find x!.\n" |
| 82 | "\n" |
| 83 | "Raise a ValueError if x is negative or non-integral."); |
| 84 | |
| 85 | #define MATH_FACTORIAL_METHODDEF \ |
| 86 | {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__}, |
| 87 | |
| 88 | PyDoc_STRVAR(math_trunc__doc__, |
| 89 | "trunc($module, x, /)\n" |
| 90 | "--\n" |
| 91 | "\n" |
| 92 | "Truncates the Real x to the nearest Integral toward 0.\n" |
| 93 | "\n" |
| 94 | "Uses the __trunc__ magic method."); |
| 95 | |
| 96 | #define MATH_TRUNC_METHODDEF \ |
| 97 | {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__}, |
| 98 | |
| 99 | PyDoc_STRVAR(math_frexp__doc__, |
| 100 | "frexp($module, x, /)\n" |
| 101 | "--\n" |
| 102 | "\n" |
| 103 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 104 | "\n" |
| 105 | "m is a float and e is an int, such that x = m * 2.**e.\n" |
| 106 | "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
| 107 | |
| 108 | #define MATH_FREXP_METHODDEF \ |
| 109 | {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__}, |
| 110 | |
| 111 | static PyObject * |
| 112 | math_frexp_impl(PyObject *module, double x); |
| 113 | |
| 114 | static PyObject * |
| 115 | math_frexp(PyObject *module, PyObject *arg) |
| 116 | { |
| 117 | PyObject *return_value = NULL; |
| 118 | double x; |
| 119 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 120 | x = PyFloat_AsDouble(arg); |
| 121 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 122 | goto exit; |
| 123 | } |
| 124 | return_value = math_frexp_impl(module, x); |
| 125 | |
| 126 | exit: |
| 127 | return return_value; |
| 128 | } |
| 129 | |
| 130 | PyDoc_STRVAR(math_ldexp__doc__, |
| 131 | "ldexp($module, x, i, /)\n" |
| 132 | "--\n" |
| 133 | "\n" |
| 134 | "Return x * (2**i).\n" |
| 135 | "\n" |
| 136 | "This is essentially the inverse of frexp()."); |
| 137 | |
| 138 | #define MATH_LDEXP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 139 | {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 140 | |
| 141 | static PyObject * |
| 142 | math_ldexp_impl(PyObject *module, double x, PyObject *i); |
| 143 | |
| 144 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 145 | math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 146 | { |
| 147 | PyObject *return_value = NULL; |
| 148 | double x; |
| 149 | PyObject *i; |
| 150 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 151 | if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 152 | goto exit; |
| 153 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 154 | x = PyFloat_AsDouble(args[0]); |
| 155 | if (PyErr_Occurred()) { |
| 156 | goto exit; |
| 157 | } |
| 158 | i = args[1]; |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 159 | return_value = math_ldexp_impl(module, x, i); |
| 160 | |
| 161 | exit: |
| 162 | return return_value; |
| 163 | } |
| 164 | |
| 165 | PyDoc_STRVAR(math_modf__doc__, |
| 166 | "modf($module, x, /)\n" |
| 167 | "--\n" |
| 168 | "\n" |
| 169 | "Return the fractional and integer parts of x.\n" |
| 170 | "\n" |
| 171 | "Both results carry the sign of x and are floats."); |
| 172 | |
| 173 | #define MATH_MODF_METHODDEF \ |
| 174 | {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__}, |
| 175 | |
| 176 | static PyObject * |
| 177 | math_modf_impl(PyObject *module, double x); |
| 178 | |
| 179 | static PyObject * |
| 180 | math_modf(PyObject *module, PyObject *arg) |
| 181 | { |
| 182 | PyObject *return_value = NULL; |
| 183 | double x; |
| 184 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 185 | x = PyFloat_AsDouble(arg); |
| 186 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 187 | goto exit; |
| 188 | } |
| 189 | return_value = math_modf_impl(module, x); |
| 190 | |
| 191 | exit: |
| 192 | return return_value; |
| 193 | } |
| 194 | |
| 195 | PyDoc_STRVAR(math_log__doc__, |
| 196 | "log(x, [base=math.e])\n" |
| 197 | "Return the logarithm of x to the given base.\n" |
| 198 | "\n" |
| 199 | "If the base not specified, returns the natural logarithm (base e) of x."); |
| 200 | |
| 201 | #define MATH_LOG_METHODDEF \ |
| 202 | {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__}, |
| 203 | |
| 204 | static PyObject * |
| 205 | math_log_impl(PyObject *module, PyObject *x, int group_right_1, |
| 206 | PyObject *base); |
| 207 | |
| 208 | static PyObject * |
| 209 | math_log(PyObject *module, PyObject *args) |
| 210 | { |
| 211 | PyObject *return_value = NULL; |
| 212 | PyObject *x; |
| 213 | int group_right_1 = 0; |
| 214 | PyObject *base = NULL; |
| 215 | |
| 216 | switch (PyTuple_GET_SIZE(args)) { |
| 217 | case 1: |
| 218 | if (!PyArg_ParseTuple(args, "O:log", &x)) { |
| 219 | goto exit; |
| 220 | } |
| 221 | break; |
| 222 | case 2: |
| 223 | if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) { |
| 224 | goto exit; |
| 225 | } |
| 226 | group_right_1 = 1; |
| 227 | break; |
| 228 | default: |
| 229 | PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments"); |
| 230 | goto exit; |
| 231 | } |
| 232 | return_value = math_log_impl(module, x, group_right_1, base); |
| 233 | |
| 234 | exit: |
| 235 | return return_value; |
| 236 | } |
| 237 | |
| 238 | PyDoc_STRVAR(math_log2__doc__, |
| 239 | "log2($module, x, /)\n" |
| 240 | "--\n" |
| 241 | "\n" |
| 242 | "Return the base 2 logarithm of x."); |
| 243 | |
| 244 | #define MATH_LOG2_METHODDEF \ |
| 245 | {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__}, |
| 246 | |
| 247 | PyDoc_STRVAR(math_log10__doc__, |
| 248 | "log10($module, x, /)\n" |
| 249 | "--\n" |
| 250 | "\n" |
| 251 | "Return the base 10 logarithm of x."); |
| 252 | |
| 253 | #define MATH_LOG10_METHODDEF \ |
| 254 | {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__}, |
| 255 | |
| 256 | PyDoc_STRVAR(math_fmod__doc__, |
| 257 | "fmod($module, x, y, /)\n" |
| 258 | "--\n" |
| 259 | "\n" |
| 260 | "Return fmod(x, y), according to platform C.\n" |
| 261 | "\n" |
| 262 | "x % y may differ."); |
| 263 | |
| 264 | #define MATH_FMOD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 265 | {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 266 | |
| 267 | static PyObject * |
| 268 | math_fmod_impl(PyObject *module, double x, double y); |
| 269 | |
| 270 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 271 | math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 272 | { |
| 273 | PyObject *return_value = NULL; |
| 274 | double x; |
| 275 | double y; |
| 276 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 277 | if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) { |
| 278 | goto exit; |
| 279 | } |
| 280 | x = PyFloat_AsDouble(args[0]); |
| 281 | if (PyErr_Occurred()) { |
| 282 | goto exit; |
| 283 | } |
| 284 | y = PyFloat_AsDouble(args[1]); |
| 285 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 286 | goto exit; |
| 287 | } |
| 288 | return_value = math_fmod_impl(module, x, y); |
| 289 | |
| 290 | exit: |
| 291 | return return_value; |
| 292 | } |
| 293 | |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 294 | PyDoc_STRVAR(math_dist__doc__, |
| 295 | "dist($module, p, q, /)\n" |
| 296 | "--\n" |
| 297 | "\n" |
| 298 | "Return the Euclidean distance between two points p and q.\n" |
| 299 | "\n" |
| 300 | "The points should be specified as tuples of coordinates.\n" |
| 301 | "Both tuples must be the same size.\n" |
| 302 | "\n" |
| 303 | "Roughly equivalent to:\n" |
| 304 | " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))"); |
| 305 | |
| 306 | #define MATH_DIST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 307 | {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__}, |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 308 | |
| 309 | static PyObject * |
| 310 | math_dist_impl(PyObject *module, PyObject *p, PyObject *q); |
| 311 | |
| 312 | static PyObject * |
| 313 | math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 314 | { |
| 315 | PyObject *return_value = NULL; |
| 316 | PyObject *p; |
| 317 | PyObject *q; |
| 318 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 319 | if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) { |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 320 | goto exit; |
| 321 | } |
Ammar Askar | cb08a71 | 2019-01-12 01:23:41 -0500 | [diff] [blame] | 322 | if (!PyTuple_Check(args[0])) { |
| 323 | _PyArg_BadArgument("dist", 1, "tuple", args[0]); |
| 324 | goto exit; |
| 325 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 326 | p = args[0]; |
Ammar Askar | cb08a71 | 2019-01-12 01:23:41 -0500 | [diff] [blame] | 327 | if (!PyTuple_Check(args[1])) { |
| 328 | _PyArg_BadArgument("dist", 2, "tuple", args[1]); |
| 329 | goto exit; |
| 330 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 331 | q = args[1]; |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 332 | return_value = math_dist_impl(module, p, q); |
| 333 | |
| 334 | exit: |
| 335 | return return_value; |
| 336 | } |
| 337 | |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 338 | PyDoc_STRVAR(math_pow__doc__, |
| 339 | "pow($module, x, y, /)\n" |
| 340 | "--\n" |
| 341 | "\n" |
| 342 | "Return x**y (x to the power of y)."); |
| 343 | |
| 344 | #define MATH_POW_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 345 | {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 346 | |
| 347 | static PyObject * |
| 348 | math_pow_impl(PyObject *module, double x, double y); |
| 349 | |
| 350 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 351 | math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 352 | { |
| 353 | PyObject *return_value = NULL; |
| 354 | double x; |
| 355 | double y; |
| 356 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 357 | if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) { |
| 358 | goto exit; |
| 359 | } |
| 360 | x = PyFloat_AsDouble(args[0]); |
| 361 | if (PyErr_Occurred()) { |
| 362 | goto exit; |
| 363 | } |
| 364 | y = PyFloat_AsDouble(args[1]); |
| 365 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 366 | goto exit; |
| 367 | } |
| 368 | return_value = math_pow_impl(module, x, y); |
| 369 | |
| 370 | exit: |
| 371 | return return_value; |
| 372 | } |
| 373 | |
| 374 | PyDoc_STRVAR(math_degrees__doc__, |
| 375 | "degrees($module, x, /)\n" |
| 376 | "--\n" |
| 377 | "\n" |
| 378 | "Convert angle x from radians to degrees."); |
| 379 | |
| 380 | #define MATH_DEGREES_METHODDEF \ |
| 381 | {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__}, |
| 382 | |
| 383 | static PyObject * |
| 384 | math_degrees_impl(PyObject *module, double x); |
| 385 | |
| 386 | static PyObject * |
| 387 | math_degrees(PyObject *module, PyObject *arg) |
| 388 | { |
| 389 | PyObject *return_value = NULL; |
| 390 | double x; |
| 391 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 392 | x = PyFloat_AsDouble(arg); |
| 393 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 394 | goto exit; |
| 395 | } |
| 396 | return_value = math_degrees_impl(module, x); |
| 397 | |
| 398 | exit: |
| 399 | return return_value; |
| 400 | } |
| 401 | |
| 402 | PyDoc_STRVAR(math_radians__doc__, |
| 403 | "radians($module, x, /)\n" |
| 404 | "--\n" |
| 405 | "\n" |
| 406 | "Convert angle x from degrees to radians."); |
| 407 | |
| 408 | #define MATH_RADIANS_METHODDEF \ |
| 409 | {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__}, |
| 410 | |
| 411 | static PyObject * |
| 412 | math_radians_impl(PyObject *module, double x); |
| 413 | |
| 414 | static PyObject * |
| 415 | math_radians(PyObject *module, PyObject *arg) |
| 416 | { |
| 417 | PyObject *return_value = NULL; |
| 418 | double x; |
| 419 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 420 | x = PyFloat_AsDouble(arg); |
| 421 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 422 | goto exit; |
| 423 | } |
| 424 | return_value = math_radians_impl(module, x); |
| 425 | |
| 426 | exit: |
| 427 | return return_value; |
| 428 | } |
| 429 | |
| 430 | PyDoc_STRVAR(math_isfinite__doc__, |
| 431 | "isfinite($module, x, /)\n" |
| 432 | "--\n" |
| 433 | "\n" |
| 434 | "Return True if x is neither an infinity nor a NaN, and False otherwise."); |
| 435 | |
| 436 | #define MATH_ISFINITE_METHODDEF \ |
| 437 | {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__}, |
| 438 | |
| 439 | static PyObject * |
| 440 | math_isfinite_impl(PyObject *module, double x); |
| 441 | |
| 442 | static PyObject * |
| 443 | math_isfinite(PyObject *module, PyObject *arg) |
| 444 | { |
| 445 | PyObject *return_value = NULL; |
| 446 | double x; |
| 447 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 448 | x = PyFloat_AsDouble(arg); |
| 449 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 450 | goto exit; |
| 451 | } |
| 452 | return_value = math_isfinite_impl(module, x); |
| 453 | |
| 454 | exit: |
| 455 | return return_value; |
| 456 | } |
| 457 | |
| 458 | PyDoc_STRVAR(math_isnan__doc__, |
| 459 | "isnan($module, x, /)\n" |
| 460 | "--\n" |
| 461 | "\n" |
| 462 | "Return True if x is a NaN (not a number), and False otherwise."); |
| 463 | |
| 464 | #define MATH_ISNAN_METHODDEF \ |
| 465 | {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__}, |
| 466 | |
| 467 | static PyObject * |
| 468 | math_isnan_impl(PyObject *module, double x); |
| 469 | |
| 470 | static PyObject * |
| 471 | math_isnan(PyObject *module, PyObject *arg) |
| 472 | { |
| 473 | PyObject *return_value = NULL; |
| 474 | double x; |
| 475 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 476 | x = PyFloat_AsDouble(arg); |
| 477 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 478 | goto exit; |
| 479 | } |
| 480 | return_value = math_isnan_impl(module, x); |
| 481 | |
| 482 | exit: |
| 483 | return return_value; |
| 484 | } |
| 485 | |
| 486 | PyDoc_STRVAR(math_isinf__doc__, |
| 487 | "isinf($module, x, /)\n" |
| 488 | "--\n" |
| 489 | "\n" |
| 490 | "Return True if x is a positive or negative infinity, and False otherwise."); |
| 491 | |
| 492 | #define MATH_ISINF_METHODDEF \ |
| 493 | {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__}, |
| 494 | |
| 495 | static PyObject * |
| 496 | math_isinf_impl(PyObject *module, double x); |
| 497 | |
| 498 | static PyObject * |
| 499 | math_isinf(PyObject *module, PyObject *arg) |
| 500 | { |
| 501 | PyObject *return_value = NULL; |
| 502 | double x; |
| 503 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 504 | x = PyFloat_AsDouble(arg); |
| 505 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 506 | goto exit; |
| 507 | } |
| 508 | return_value = math_isinf_impl(module, x); |
| 509 | |
| 510 | exit: |
| 511 | return return_value; |
| 512 | } |
| 513 | |
| 514 | PyDoc_STRVAR(math_isclose__doc__, |
| 515 | "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n" |
| 516 | "--\n" |
| 517 | "\n" |
| 518 | "Determine whether two floating point numbers are close in value.\n" |
| 519 | "\n" |
| 520 | " rel_tol\n" |
| 521 | " maximum difference for being considered \"close\", relative to the\n" |
| 522 | " magnitude of the input values\n" |
| 523 | " abs_tol\n" |
| 524 | " maximum difference for being considered \"close\", regardless of the\n" |
| 525 | " magnitude of the input values\n" |
| 526 | "\n" |
| 527 | "Return True if a is close in value to b, and False otherwise.\n" |
| 528 | "\n" |
| 529 | "For the values to be considered close, the difference between them\n" |
| 530 | "must be smaller than at least one of the tolerances.\n" |
| 531 | "\n" |
| 532 | "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n" |
| 533 | "is, NaN is not close to anything, even itself. inf and -inf are\n" |
| 534 | "only close to themselves."); |
| 535 | |
| 536 | #define MATH_ISCLOSE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 537 | {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 538 | |
| 539 | static int |
| 540 | math_isclose_impl(PyObject *module, double a, double b, double rel_tol, |
| 541 | double abs_tol); |
| 542 | |
| 543 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 544 | math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 545 | { |
| 546 | PyObject *return_value = NULL; |
| 547 | static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 548 | static _PyArg_Parser _parser = {NULL, _keywords, "isclose", 0}; |
| 549 | PyObject *argsbuf[4]; |
| 550 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 551 | double a; |
| 552 | double b; |
| 553 | double rel_tol = 1e-09; |
| 554 | double abs_tol = 0.0; |
| 555 | int _return_value; |
| 556 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 557 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 558 | if (!args) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 559 | goto exit; |
| 560 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 561 | a = PyFloat_AsDouble(args[0]); |
| 562 | if (PyErr_Occurred()) { |
| 563 | goto exit; |
| 564 | } |
| 565 | b = PyFloat_AsDouble(args[1]); |
| 566 | if (PyErr_Occurred()) { |
| 567 | goto exit; |
| 568 | } |
| 569 | if (!noptargs) { |
| 570 | goto skip_optional_kwonly; |
| 571 | } |
| 572 | if (args[2]) { |
| 573 | rel_tol = PyFloat_AsDouble(args[2]); |
| 574 | if (PyErr_Occurred()) { |
| 575 | goto exit; |
| 576 | } |
| 577 | if (!--noptargs) { |
| 578 | goto skip_optional_kwonly; |
| 579 | } |
| 580 | } |
| 581 | abs_tol = PyFloat_AsDouble(args[3]); |
| 582 | if (PyErr_Occurred()) { |
| 583 | goto exit; |
| 584 | } |
| 585 | skip_optional_kwonly: |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 586 | _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol); |
| 587 | if ((_return_value == -1) && PyErr_Occurred()) { |
| 588 | goto exit; |
| 589 | } |
| 590 | return_value = PyBool_FromLong((long)_return_value); |
| 591 | |
| 592 | exit: |
| 593 | return return_value; |
| 594 | } |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 595 | |
| 596 | PyDoc_STRVAR(math_prod__doc__, |
| 597 | "prod($module, iterable, /, *, start=1)\n" |
| 598 | "--\n" |
| 599 | "\n" |
| 600 | "Calculate the product of all the elements in the input iterable.\n" |
| 601 | "\n" |
| 602 | "The default start value for the product is 1.\n" |
| 603 | "\n" |
| 604 | "When the iterable is empty, return the start value. This function is\n" |
| 605 | "intended specifically for use with numeric values and may reject\n" |
| 606 | "non-numeric types."); |
| 607 | |
| 608 | #define MATH_PROD_METHODDEF \ |
| 609 | {"prod", (PyCFunction)(void(*)(void))math_prod, METH_FASTCALL|METH_KEYWORDS, math_prod__doc__}, |
| 610 | |
| 611 | static PyObject * |
| 612 | math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start); |
| 613 | |
| 614 | static PyObject * |
| 615 | math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 616 | { |
| 617 | PyObject *return_value = NULL; |
| 618 | static const char * const _keywords[] = {"", "start", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 619 | static _PyArg_Parser _parser = {NULL, _keywords, "prod", 0}; |
| 620 | PyObject *argsbuf[2]; |
| 621 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 622 | PyObject *iterable; |
| 623 | PyObject *start = NULL; |
| 624 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 625 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); |
| 626 | if (!args) { |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 627 | goto exit; |
| 628 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 629 | iterable = args[0]; |
| 630 | if (!noptargs) { |
| 631 | goto skip_optional_kwonly; |
| 632 | } |
| 633 | start = args[1]; |
| 634 | skip_optional_kwonly: |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 635 | return_value = math_prod_impl(module, iterable, start); |
| 636 | |
| 637 | exit: |
| 638 | return return_value; |
| 639 | } |
Mark Dickinson | 73934b9 | 2019-05-18 12:29:50 +0100 | [diff] [blame^] | 640 | /*[clinic end generated code: output=aeed62f403b90199 input=a9049054013a1b77]*/ |