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 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 120 | if (PyFloat_CheckExact(arg)) { |
| 121 | x = PyFloat_AS_DOUBLE(arg); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | x = PyFloat_AsDouble(arg); |
| 126 | if (x == -1.0 && PyErr_Occurred()) { |
| 127 | goto exit; |
| 128 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 129 | } |
| 130 | return_value = math_frexp_impl(module, x); |
| 131 | |
| 132 | exit: |
| 133 | return return_value; |
| 134 | } |
| 135 | |
| 136 | PyDoc_STRVAR(math_ldexp__doc__, |
| 137 | "ldexp($module, x, i, /)\n" |
| 138 | "--\n" |
| 139 | "\n" |
| 140 | "Return x * (2**i).\n" |
| 141 | "\n" |
| 142 | "This is essentially the inverse of frexp()."); |
| 143 | |
| 144 | #define MATH_LDEXP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 145 | {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 146 | |
| 147 | static PyObject * |
| 148 | math_ldexp_impl(PyObject *module, double x, PyObject *i); |
| 149 | |
| 150 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 151 | math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 152 | { |
| 153 | PyObject *return_value = NULL; |
| 154 | double x; |
| 155 | PyObject *i; |
| 156 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 157 | if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 158 | goto exit; |
| 159 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 160 | if (PyFloat_CheckExact(args[0])) { |
| 161 | x = PyFloat_AS_DOUBLE(args[0]); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | x = PyFloat_AsDouble(args[0]); |
| 166 | if (x == -1.0 && PyErr_Occurred()) { |
| 167 | goto exit; |
| 168 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 169 | } |
| 170 | i = args[1]; |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 171 | return_value = math_ldexp_impl(module, x, i); |
| 172 | |
| 173 | exit: |
| 174 | return return_value; |
| 175 | } |
| 176 | |
| 177 | PyDoc_STRVAR(math_modf__doc__, |
| 178 | "modf($module, x, /)\n" |
| 179 | "--\n" |
| 180 | "\n" |
| 181 | "Return the fractional and integer parts of x.\n" |
| 182 | "\n" |
| 183 | "Both results carry the sign of x and are floats."); |
| 184 | |
| 185 | #define MATH_MODF_METHODDEF \ |
| 186 | {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__}, |
| 187 | |
| 188 | static PyObject * |
| 189 | math_modf_impl(PyObject *module, double x); |
| 190 | |
| 191 | static PyObject * |
| 192 | math_modf(PyObject *module, PyObject *arg) |
| 193 | { |
| 194 | PyObject *return_value = NULL; |
| 195 | double x; |
| 196 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 197 | if (PyFloat_CheckExact(arg)) { |
| 198 | x = PyFloat_AS_DOUBLE(arg); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | x = PyFloat_AsDouble(arg); |
| 203 | if (x == -1.0 && PyErr_Occurred()) { |
| 204 | goto exit; |
| 205 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 206 | } |
| 207 | return_value = math_modf_impl(module, x); |
| 208 | |
| 209 | exit: |
| 210 | return return_value; |
| 211 | } |
| 212 | |
| 213 | PyDoc_STRVAR(math_log__doc__, |
| 214 | "log(x, [base=math.e])\n" |
| 215 | "Return the logarithm of x to the given base.\n" |
| 216 | "\n" |
| 217 | "If the base not specified, returns the natural logarithm (base e) of x."); |
| 218 | |
| 219 | #define MATH_LOG_METHODDEF \ |
| 220 | {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__}, |
| 221 | |
| 222 | static PyObject * |
| 223 | math_log_impl(PyObject *module, PyObject *x, int group_right_1, |
| 224 | PyObject *base); |
| 225 | |
| 226 | static PyObject * |
| 227 | math_log(PyObject *module, PyObject *args) |
| 228 | { |
| 229 | PyObject *return_value = NULL; |
| 230 | PyObject *x; |
| 231 | int group_right_1 = 0; |
| 232 | PyObject *base = NULL; |
| 233 | |
| 234 | switch (PyTuple_GET_SIZE(args)) { |
| 235 | case 1: |
| 236 | if (!PyArg_ParseTuple(args, "O:log", &x)) { |
| 237 | goto exit; |
| 238 | } |
| 239 | break; |
| 240 | case 2: |
| 241 | if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) { |
| 242 | goto exit; |
| 243 | } |
| 244 | group_right_1 = 1; |
| 245 | break; |
| 246 | default: |
| 247 | PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments"); |
| 248 | goto exit; |
| 249 | } |
| 250 | return_value = math_log_impl(module, x, group_right_1, base); |
| 251 | |
| 252 | exit: |
| 253 | return return_value; |
| 254 | } |
| 255 | |
| 256 | PyDoc_STRVAR(math_log2__doc__, |
| 257 | "log2($module, x, /)\n" |
| 258 | "--\n" |
| 259 | "\n" |
| 260 | "Return the base 2 logarithm of x."); |
| 261 | |
| 262 | #define MATH_LOG2_METHODDEF \ |
| 263 | {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__}, |
| 264 | |
| 265 | PyDoc_STRVAR(math_log10__doc__, |
| 266 | "log10($module, x, /)\n" |
| 267 | "--\n" |
| 268 | "\n" |
| 269 | "Return the base 10 logarithm of x."); |
| 270 | |
| 271 | #define MATH_LOG10_METHODDEF \ |
| 272 | {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__}, |
| 273 | |
| 274 | PyDoc_STRVAR(math_fmod__doc__, |
| 275 | "fmod($module, x, y, /)\n" |
| 276 | "--\n" |
| 277 | "\n" |
| 278 | "Return fmod(x, y), according to platform C.\n" |
| 279 | "\n" |
| 280 | "x % y may differ."); |
| 281 | |
| 282 | #define MATH_FMOD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 283 | {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 284 | |
| 285 | static PyObject * |
| 286 | math_fmod_impl(PyObject *module, double x, double y); |
| 287 | |
| 288 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 289 | math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 290 | { |
| 291 | PyObject *return_value = NULL; |
| 292 | double x; |
| 293 | double y; |
| 294 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 295 | if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) { |
| 296 | goto exit; |
| 297 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 298 | if (PyFloat_CheckExact(args[0])) { |
| 299 | x = PyFloat_AS_DOUBLE(args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 300 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 301 | else |
| 302 | { |
| 303 | x = PyFloat_AsDouble(args[0]); |
| 304 | if (x == -1.0 && PyErr_Occurred()) { |
| 305 | goto exit; |
| 306 | } |
| 307 | } |
| 308 | if (PyFloat_CheckExact(args[1])) { |
| 309 | y = PyFloat_AS_DOUBLE(args[1]); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | y = PyFloat_AsDouble(args[1]); |
| 314 | if (y == -1.0 && PyErr_Occurred()) { |
| 315 | goto exit; |
| 316 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 317 | } |
| 318 | return_value = math_fmod_impl(module, x, y); |
| 319 | |
| 320 | exit: |
| 321 | return return_value; |
| 322 | } |
| 323 | |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 324 | PyDoc_STRVAR(math_dist__doc__, |
| 325 | "dist($module, p, q, /)\n" |
| 326 | "--\n" |
| 327 | "\n" |
| 328 | "Return the Euclidean distance between two points p and q.\n" |
| 329 | "\n" |
Raymond Hettinger | 6b5f1b4 | 2019-07-27 14:04:29 -0700 | [diff] [blame] | 330 | "The points should be specified as sequences (or iterables) of\n" |
| 331 | "coordinates. Both inputs must have the same dimension.\n" |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 332 | "\n" |
| 333 | "Roughly equivalent to:\n" |
| 334 | " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))"); |
| 335 | |
| 336 | #define MATH_DIST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 337 | {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__}, |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 338 | |
| 339 | static PyObject * |
| 340 | math_dist_impl(PyObject *module, PyObject *p, PyObject *q); |
| 341 | |
| 342 | static PyObject * |
| 343 | math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 344 | { |
| 345 | PyObject *return_value = NULL; |
| 346 | PyObject *p; |
| 347 | PyObject *q; |
| 348 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 349 | if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) { |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 350 | goto exit; |
| 351 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 352 | p = args[0]; |
| 353 | q = args[1]; |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 354 | return_value = math_dist_impl(module, p, q); |
| 355 | |
| 356 | exit: |
| 357 | return return_value; |
| 358 | } |
| 359 | |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 360 | PyDoc_STRVAR(math_pow__doc__, |
| 361 | "pow($module, x, y, /)\n" |
| 362 | "--\n" |
| 363 | "\n" |
| 364 | "Return x**y (x to the power of y)."); |
| 365 | |
| 366 | #define MATH_POW_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 367 | {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 368 | |
| 369 | static PyObject * |
| 370 | math_pow_impl(PyObject *module, double x, double y); |
| 371 | |
| 372 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 373 | math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 374 | { |
| 375 | PyObject *return_value = NULL; |
| 376 | double x; |
| 377 | double y; |
| 378 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 379 | if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) { |
| 380 | goto exit; |
| 381 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 382 | if (PyFloat_CheckExact(args[0])) { |
| 383 | x = PyFloat_AS_DOUBLE(args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 384 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 385 | else |
| 386 | { |
| 387 | x = PyFloat_AsDouble(args[0]); |
| 388 | if (x == -1.0 && PyErr_Occurred()) { |
| 389 | goto exit; |
| 390 | } |
| 391 | } |
| 392 | if (PyFloat_CheckExact(args[1])) { |
| 393 | y = PyFloat_AS_DOUBLE(args[1]); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | y = PyFloat_AsDouble(args[1]); |
| 398 | if (y == -1.0 && PyErr_Occurred()) { |
| 399 | goto exit; |
| 400 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 401 | } |
| 402 | return_value = math_pow_impl(module, x, y); |
| 403 | |
| 404 | exit: |
| 405 | return return_value; |
| 406 | } |
| 407 | |
| 408 | PyDoc_STRVAR(math_degrees__doc__, |
| 409 | "degrees($module, x, /)\n" |
| 410 | "--\n" |
| 411 | "\n" |
| 412 | "Convert angle x from radians to degrees."); |
| 413 | |
| 414 | #define MATH_DEGREES_METHODDEF \ |
| 415 | {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__}, |
| 416 | |
| 417 | static PyObject * |
| 418 | math_degrees_impl(PyObject *module, double x); |
| 419 | |
| 420 | static PyObject * |
| 421 | math_degrees(PyObject *module, PyObject *arg) |
| 422 | { |
| 423 | PyObject *return_value = NULL; |
| 424 | double x; |
| 425 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 426 | if (PyFloat_CheckExact(arg)) { |
| 427 | x = PyFloat_AS_DOUBLE(arg); |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | x = PyFloat_AsDouble(arg); |
| 432 | if (x == -1.0 && PyErr_Occurred()) { |
| 433 | goto exit; |
| 434 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 435 | } |
| 436 | return_value = math_degrees_impl(module, x); |
| 437 | |
| 438 | exit: |
| 439 | return return_value; |
| 440 | } |
| 441 | |
| 442 | PyDoc_STRVAR(math_radians__doc__, |
| 443 | "radians($module, x, /)\n" |
| 444 | "--\n" |
| 445 | "\n" |
| 446 | "Convert angle x from degrees to radians."); |
| 447 | |
| 448 | #define MATH_RADIANS_METHODDEF \ |
| 449 | {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__}, |
| 450 | |
| 451 | static PyObject * |
| 452 | math_radians_impl(PyObject *module, double x); |
| 453 | |
| 454 | static PyObject * |
| 455 | math_radians(PyObject *module, PyObject *arg) |
| 456 | { |
| 457 | PyObject *return_value = NULL; |
| 458 | double x; |
| 459 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 460 | if (PyFloat_CheckExact(arg)) { |
| 461 | x = PyFloat_AS_DOUBLE(arg); |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | x = PyFloat_AsDouble(arg); |
| 466 | if (x == -1.0 && PyErr_Occurred()) { |
| 467 | goto exit; |
| 468 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 469 | } |
| 470 | return_value = math_radians_impl(module, x); |
| 471 | |
| 472 | exit: |
| 473 | return return_value; |
| 474 | } |
| 475 | |
| 476 | PyDoc_STRVAR(math_isfinite__doc__, |
| 477 | "isfinite($module, x, /)\n" |
| 478 | "--\n" |
| 479 | "\n" |
| 480 | "Return True if x is neither an infinity nor a NaN, and False otherwise."); |
| 481 | |
| 482 | #define MATH_ISFINITE_METHODDEF \ |
| 483 | {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__}, |
| 484 | |
| 485 | static PyObject * |
| 486 | math_isfinite_impl(PyObject *module, double x); |
| 487 | |
| 488 | static PyObject * |
| 489 | math_isfinite(PyObject *module, PyObject *arg) |
| 490 | { |
| 491 | PyObject *return_value = NULL; |
| 492 | double x; |
| 493 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 494 | if (PyFloat_CheckExact(arg)) { |
| 495 | x = PyFloat_AS_DOUBLE(arg); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | x = PyFloat_AsDouble(arg); |
| 500 | if (x == -1.0 && PyErr_Occurred()) { |
| 501 | goto exit; |
| 502 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 503 | } |
| 504 | return_value = math_isfinite_impl(module, x); |
| 505 | |
| 506 | exit: |
| 507 | return return_value; |
| 508 | } |
| 509 | |
| 510 | PyDoc_STRVAR(math_isnan__doc__, |
| 511 | "isnan($module, x, /)\n" |
| 512 | "--\n" |
| 513 | "\n" |
| 514 | "Return True if x is a NaN (not a number), and False otherwise."); |
| 515 | |
| 516 | #define MATH_ISNAN_METHODDEF \ |
| 517 | {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__}, |
| 518 | |
| 519 | static PyObject * |
| 520 | math_isnan_impl(PyObject *module, double x); |
| 521 | |
| 522 | static PyObject * |
| 523 | math_isnan(PyObject *module, PyObject *arg) |
| 524 | { |
| 525 | PyObject *return_value = NULL; |
| 526 | double x; |
| 527 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 528 | if (PyFloat_CheckExact(arg)) { |
| 529 | x = PyFloat_AS_DOUBLE(arg); |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | x = PyFloat_AsDouble(arg); |
| 534 | if (x == -1.0 && PyErr_Occurred()) { |
| 535 | goto exit; |
| 536 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 537 | } |
| 538 | return_value = math_isnan_impl(module, x); |
| 539 | |
| 540 | exit: |
| 541 | return return_value; |
| 542 | } |
| 543 | |
| 544 | PyDoc_STRVAR(math_isinf__doc__, |
| 545 | "isinf($module, x, /)\n" |
| 546 | "--\n" |
| 547 | "\n" |
| 548 | "Return True if x is a positive or negative infinity, and False otherwise."); |
| 549 | |
| 550 | #define MATH_ISINF_METHODDEF \ |
| 551 | {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__}, |
| 552 | |
| 553 | static PyObject * |
| 554 | math_isinf_impl(PyObject *module, double x); |
| 555 | |
| 556 | static PyObject * |
| 557 | math_isinf(PyObject *module, PyObject *arg) |
| 558 | { |
| 559 | PyObject *return_value = NULL; |
| 560 | double x; |
| 561 | |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 562 | if (PyFloat_CheckExact(arg)) { |
| 563 | x = PyFloat_AS_DOUBLE(arg); |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | x = PyFloat_AsDouble(arg); |
| 568 | if (x == -1.0 && PyErr_Occurred()) { |
| 569 | goto exit; |
| 570 | } |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 571 | } |
| 572 | return_value = math_isinf_impl(module, x); |
| 573 | |
| 574 | exit: |
| 575 | return return_value; |
| 576 | } |
| 577 | |
| 578 | PyDoc_STRVAR(math_isclose__doc__, |
| 579 | "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n" |
| 580 | "--\n" |
| 581 | "\n" |
| 582 | "Determine whether two floating point numbers are close in value.\n" |
| 583 | "\n" |
| 584 | " rel_tol\n" |
| 585 | " maximum difference for being considered \"close\", relative to the\n" |
| 586 | " magnitude of the input values\n" |
| 587 | " abs_tol\n" |
| 588 | " maximum difference for being considered \"close\", regardless of the\n" |
| 589 | " magnitude of the input values\n" |
| 590 | "\n" |
| 591 | "Return True if a is close in value to b, and False otherwise.\n" |
| 592 | "\n" |
| 593 | "For the values to be considered close, the difference between them\n" |
| 594 | "must be smaller than at least one of the tolerances.\n" |
| 595 | "\n" |
| 596 | "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n" |
| 597 | "is, NaN is not close to anything, even itself. inf and -inf are\n" |
| 598 | "only close to themselves."); |
| 599 | |
| 600 | #define MATH_ISCLOSE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 601 | {"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] | 602 | |
| 603 | static int |
| 604 | math_isclose_impl(PyObject *module, double a, double b, double rel_tol, |
| 605 | double abs_tol); |
| 606 | |
| 607 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 608 | 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] | 609 | { |
| 610 | PyObject *return_value = NULL; |
| 611 | static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 612 | static _PyArg_Parser _parser = {NULL, _keywords, "isclose", 0}; |
| 613 | PyObject *argsbuf[4]; |
| 614 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 615 | double a; |
| 616 | double b; |
| 617 | double rel_tol = 1e-09; |
| 618 | double abs_tol = 0.0; |
| 619 | int _return_value; |
| 620 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 621 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); |
| 622 | if (!args) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 623 | goto exit; |
| 624 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 625 | if (PyFloat_CheckExact(args[0])) { |
| 626 | a = PyFloat_AS_DOUBLE(args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 627 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 628 | else |
| 629 | { |
| 630 | a = PyFloat_AsDouble(args[0]); |
| 631 | if (a == -1.0 && PyErr_Occurred()) { |
| 632 | goto exit; |
| 633 | } |
| 634 | } |
| 635 | if (PyFloat_CheckExact(args[1])) { |
| 636 | b = PyFloat_AS_DOUBLE(args[1]); |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | b = PyFloat_AsDouble(args[1]); |
| 641 | if (b == -1.0 && PyErr_Occurred()) { |
| 642 | goto exit; |
| 643 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 644 | } |
| 645 | if (!noptargs) { |
| 646 | goto skip_optional_kwonly; |
| 647 | } |
| 648 | if (args[2]) { |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 649 | if (PyFloat_CheckExact(args[2])) { |
| 650 | rel_tol = PyFloat_AS_DOUBLE(args[2]); |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | rel_tol = PyFloat_AsDouble(args[2]); |
| 655 | if (rel_tol == -1.0 && PyErr_Occurred()) { |
| 656 | goto exit; |
| 657 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 658 | } |
| 659 | if (!--noptargs) { |
| 660 | goto skip_optional_kwonly; |
| 661 | } |
| 662 | } |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 663 | if (PyFloat_CheckExact(args[3])) { |
| 664 | abs_tol = PyFloat_AS_DOUBLE(args[3]); |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | abs_tol = PyFloat_AsDouble(args[3]); |
| 669 | if (abs_tol == -1.0 && PyErr_Occurred()) { |
| 670 | goto exit; |
| 671 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 672 | } |
| 673 | skip_optional_kwonly: |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 674 | _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol); |
| 675 | if ((_return_value == -1) && PyErr_Occurred()) { |
| 676 | goto exit; |
| 677 | } |
| 678 | return_value = PyBool_FromLong((long)_return_value); |
| 679 | |
| 680 | exit: |
| 681 | return return_value; |
| 682 | } |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 683 | |
| 684 | PyDoc_STRVAR(math_prod__doc__, |
| 685 | "prod($module, iterable, /, *, start=1)\n" |
| 686 | "--\n" |
| 687 | "\n" |
| 688 | "Calculate the product of all the elements in the input iterable.\n" |
| 689 | "\n" |
| 690 | "The default start value for the product is 1.\n" |
| 691 | "\n" |
| 692 | "When the iterable is empty, return the start value. This function is\n" |
| 693 | "intended specifically for use with numeric values and may reject\n" |
| 694 | "non-numeric types."); |
| 695 | |
| 696 | #define MATH_PROD_METHODDEF \ |
| 697 | {"prod", (PyCFunction)(void(*)(void))math_prod, METH_FASTCALL|METH_KEYWORDS, math_prod__doc__}, |
| 698 | |
| 699 | static PyObject * |
| 700 | math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start); |
| 701 | |
| 702 | static PyObject * |
| 703 | math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 704 | { |
| 705 | PyObject *return_value = NULL; |
| 706 | static const char * const _keywords[] = {"", "start", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 707 | static _PyArg_Parser _parser = {NULL, _keywords, "prod", 0}; |
| 708 | PyObject *argsbuf[2]; |
| 709 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 710 | PyObject *iterable; |
| 711 | PyObject *start = NULL; |
| 712 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 713 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); |
| 714 | if (!args) { |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 715 | goto exit; |
| 716 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 717 | iterable = args[0]; |
| 718 | if (!noptargs) { |
| 719 | goto skip_optional_kwonly; |
| 720 | } |
| 721 | start = args[1]; |
| 722 | skip_optional_kwonly: |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 723 | return_value = math_prod_impl(module, iterable, start); |
| 724 | |
| 725 | exit: |
| 726 | return return_value; |
| 727 | } |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 728 | |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 729 | PyDoc_STRVAR(math_perm__doc__, |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 730 | "perm($module, n, k=None, /)\n" |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 731 | "--\n" |
| 732 | "\n" |
| 733 | "Number of ways to choose k items from n items without repetition and with order.\n" |
| 734 | "\n" |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 735 | "Evaluates to n! / (n - k)! when k <= n and evaluates\n" |
| 736 | "to zero when k > n.\n" |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 737 | "\n" |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 738 | "If k is not specified or is None, then k defaults to n\n" |
| 739 | "and the function returns n!.\n" |
| 740 | "\n" |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 741 | "Raises TypeError if either of the arguments are not integers.\n" |
| 742 | "Raises ValueError if either of the arguments are negative."); |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 743 | |
| 744 | #define MATH_PERM_METHODDEF \ |
| 745 | {"perm", (PyCFunction)(void(*)(void))math_perm, METH_FASTCALL, math_perm__doc__}, |
| 746 | |
| 747 | static PyObject * |
| 748 | math_perm_impl(PyObject *module, PyObject *n, PyObject *k); |
| 749 | |
| 750 | static PyObject * |
| 751 | math_perm(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 752 | { |
| 753 | PyObject *return_value = NULL; |
| 754 | PyObject *n; |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 755 | PyObject *k = Py_None; |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 756 | |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 757 | if (!_PyArg_CheckPositional("perm", nargs, 1, 2)) { |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 758 | goto exit; |
| 759 | } |
| 760 | n = args[0]; |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 761 | if (nargs < 2) { |
| 762 | goto skip_optional; |
| 763 | } |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 764 | k = args[1]; |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 765 | skip_optional: |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 766 | return_value = math_perm_impl(module, n, k); |
| 767 | |
| 768 | exit: |
| 769 | return return_value; |
| 770 | } |
| 771 | |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 772 | PyDoc_STRVAR(math_comb__doc__, |
Serhiy Storchaka | 2b843ac | 2019-06-01 22:09:02 +0300 | [diff] [blame] | 773 | "comb($module, n, k, /)\n" |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 774 | "--\n" |
| 775 | "\n" |
Serhiy Storchaka | 2b843ac | 2019-06-01 22:09:02 +0300 | [diff] [blame] | 776 | "Number of ways to choose k items from n items without repetition and without order.\n" |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 777 | "\n" |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 778 | "Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n" |
| 779 | "to zero when k > n.\n" |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 780 | "\n" |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 781 | "Also called the binomial coefficient because it is equivalent\n" |
| 782 | "to the coefficient of k-th term in polynomial expansion of the\n" |
| 783 | "expression (1 + x)**n.\n" |
| 784 | "\n" |
| 785 | "Raises TypeError if either of the arguments are not integers.\n" |
| 786 | "Raises ValueError if either of the arguments are negative."); |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 787 | |
| 788 | #define MATH_COMB_METHODDEF \ |
Serhiy Storchaka | 2b843ac | 2019-06-01 22:09:02 +0300 | [diff] [blame] | 789 | {"comb", (PyCFunction)(void(*)(void))math_comb, METH_FASTCALL, math_comb__doc__}, |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 790 | |
| 791 | static PyObject * |
| 792 | math_comb_impl(PyObject *module, PyObject *n, PyObject *k); |
| 793 | |
| 794 | static PyObject * |
Serhiy Storchaka | 2b843ac | 2019-06-01 22:09:02 +0300 | [diff] [blame] | 795 | math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 796 | { |
| 797 | PyObject *return_value = NULL; |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 798 | PyObject *n; |
| 799 | PyObject *k; |
| 800 | |
Serhiy Storchaka | 2b843ac | 2019-06-01 22:09:02 +0300 | [diff] [blame] | 801 | if (!_PyArg_CheckPositional("comb", nargs, 2, 2)) { |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 802 | goto exit; |
| 803 | } |
| 804 | n = args[0]; |
Yash Aggarwal | 4a68650 | 2019-06-01 12:51:27 +0530 | [diff] [blame] | 805 | k = args[1]; |
| 806 | return_value = math_comb_impl(module, n, k); |
| 807 | |
| 808 | exit: |
| 809 | return return_value; |
| 810 | } |
Victor Stinner | 100fafc | 2020-01-12 02:15:42 +0100 | [diff] [blame] | 811 | |
| 812 | PyDoc_STRVAR(math_nextafter__doc__, |
| 813 | "nextafter($module, x, y, /)\n" |
| 814 | "--\n" |
| 815 | "\n" |
| 816 | "Return the next floating-point value after x towards y."); |
| 817 | |
| 818 | #define MATH_NEXTAFTER_METHODDEF \ |
| 819 | {"nextafter", (PyCFunction)(void(*)(void))math_nextafter, METH_FASTCALL, math_nextafter__doc__}, |
| 820 | |
| 821 | static PyObject * |
| 822 | math_nextafter_impl(PyObject *module, double x, double y); |
| 823 | |
| 824 | static PyObject * |
| 825 | math_nextafter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 826 | { |
| 827 | PyObject *return_value = NULL; |
| 828 | double x; |
| 829 | double y; |
| 830 | |
| 831 | if (!_PyArg_CheckPositional("nextafter", nargs, 2, 2)) { |
| 832 | goto exit; |
| 833 | } |
| 834 | if (PyFloat_CheckExact(args[0])) { |
| 835 | x = PyFloat_AS_DOUBLE(args[0]); |
| 836 | } |
| 837 | else |
| 838 | { |
| 839 | x = PyFloat_AsDouble(args[0]); |
| 840 | if (x == -1.0 && PyErr_Occurred()) { |
| 841 | goto exit; |
| 842 | } |
| 843 | } |
| 844 | if (PyFloat_CheckExact(args[1])) { |
| 845 | y = PyFloat_AS_DOUBLE(args[1]); |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | y = PyFloat_AsDouble(args[1]); |
| 850 | if (y == -1.0 && PyErr_Occurred()) { |
| 851 | goto exit; |
| 852 | } |
| 853 | } |
| 854 | return_value = math_nextafter_impl(module, x, y); |
| 855 | |
| 856 | exit: |
| 857 | return return_value; |
| 858 | } |
Victor Stinner | 0b2ab21 | 2020-01-13 12:44:35 +0100 | [diff] [blame] | 859 | |
| 860 | PyDoc_STRVAR(math_ulp__doc__, |
| 861 | "ulp($module, x, /)\n" |
| 862 | "--\n" |
| 863 | "\n" |
| 864 | "Return the value of the least significant bit of the float x."); |
| 865 | |
| 866 | #define MATH_ULP_METHODDEF \ |
| 867 | {"ulp", (PyCFunction)math_ulp, METH_O, math_ulp__doc__}, |
| 868 | |
| 869 | static double |
| 870 | math_ulp_impl(PyObject *module, double x); |
| 871 | |
| 872 | static PyObject * |
| 873 | math_ulp(PyObject *module, PyObject *arg) |
| 874 | { |
| 875 | PyObject *return_value = NULL; |
| 876 | double x; |
| 877 | double _return_value; |
| 878 | |
| 879 | if (PyFloat_CheckExact(arg)) { |
| 880 | x = PyFloat_AS_DOUBLE(arg); |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | x = PyFloat_AsDouble(arg); |
| 885 | if (x == -1.0 && PyErr_Occurred()) { |
| 886 | goto exit; |
| 887 | } |
| 888 | } |
| 889 | _return_value = math_ulp_impl(module, x); |
| 890 | if ((_return_value == -1.0) && PyErr_Occurred()) { |
| 891 | goto exit; |
| 892 | } |
| 893 | return_value = PyFloat_FromDouble(_return_value); |
| 894 | |
| 895 | exit: |
| 896 | return return_value; |
| 897 | } |
| 898 | /*[clinic end generated code: output=9b51d215dbcac060 input=a9049054013a1b77]*/ |