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 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 24 | if (!_PyArg_UnpackStack(args, nargs, "gcd", |
| 25 | 2, 2, |
| 26 | &a, &b)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 27 | goto exit; |
| 28 | } |
| 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 | |
| 68 | PyDoc_STRVAR(math_factorial__doc__, |
| 69 | "factorial($module, x, /)\n" |
| 70 | "--\n" |
| 71 | "\n" |
| 72 | "Find x!.\n" |
| 73 | "\n" |
| 74 | "Raise a ValueError if x is negative or non-integral."); |
| 75 | |
| 76 | #define MATH_FACTORIAL_METHODDEF \ |
| 77 | {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__}, |
| 78 | |
| 79 | PyDoc_STRVAR(math_trunc__doc__, |
| 80 | "trunc($module, x, /)\n" |
| 81 | "--\n" |
| 82 | "\n" |
| 83 | "Truncates the Real x to the nearest Integral toward 0.\n" |
| 84 | "\n" |
| 85 | "Uses the __trunc__ magic method."); |
| 86 | |
| 87 | #define MATH_TRUNC_METHODDEF \ |
| 88 | {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__}, |
| 89 | |
| 90 | PyDoc_STRVAR(math_frexp__doc__, |
| 91 | "frexp($module, x, /)\n" |
| 92 | "--\n" |
| 93 | "\n" |
| 94 | "Return the mantissa and exponent of x, as pair (m, e).\n" |
| 95 | "\n" |
| 96 | "m is a float and e is an int, such that x = m * 2.**e.\n" |
| 97 | "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
| 98 | |
| 99 | #define MATH_FREXP_METHODDEF \ |
| 100 | {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__}, |
| 101 | |
| 102 | static PyObject * |
| 103 | math_frexp_impl(PyObject *module, double x); |
| 104 | |
| 105 | static PyObject * |
| 106 | math_frexp(PyObject *module, PyObject *arg) |
| 107 | { |
| 108 | PyObject *return_value = NULL; |
| 109 | double x; |
| 110 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 111 | x = PyFloat_AsDouble(arg); |
| 112 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 113 | goto exit; |
| 114 | } |
| 115 | return_value = math_frexp_impl(module, x); |
| 116 | |
| 117 | exit: |
| 118 | return return_value; |
| 119 | } |
| 120 | |
| 121 | PyDoc_STRVAR(math_ldexp__doc__, |
| 122 | "ldexp($module, x, i, /)\n" |
| 123 | "--\n" |
| 124 | "\n" |
| 125 | "Return x * (2**i).\n" |
| 126 | "\n" |
| 127 | "This is essentially the inverse of frexp()."); |
| 128 | |
| 129 | #define MATH_LDEXP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 130 | {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 131 | |
| 132 | static PyObject * |
| 133 | math_ldexp_impl(PyObject *module, double x, PyObject *i); |
| 134 | |
| 135 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 136 | math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 137 | { |
| 138 | PyObject *return_value = NULL; |
| 139 | double x; |
| 140 | PyObject *i; |
| 141 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 142 | if (!_PyArg_ParseStack(args, nargs, "dO:ldexp", |
| 143 | &x, &i)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 144 | goto exit; |
| 145 | } |
| 146 | return_value = math_ldexp_impl(module, x, i); |
| 147 | |
| 148 | exit: |
| 149 | return return_value; |
| 150 | } |
| 151 | |
| 152 | PyDoc_STRVAR(math_modf__doc__, |
| 153 | "modf($module, x, /)\n" |
| 154 | "--\n" |
| 155 | "\n" |
| 156 | "Return the fractional and integer parts of x.\n" |
| 157 | "\n" |
| 158 | "Both results carry the sign of x and are floats."); |
| 159 | |
| 160 | #define MATH_MODF_METHODDEF \ |
| 161 | {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__}, |
| 162 | |
| 163 | static PyObject * |
| 164 | math_modf_impl(PyObject *module, double x); |
| 165 | |
| 166 | static PyObject * |
| 167 | math_modf(PyObject *module, PyObject *arg) |
| 168 | { |
| 169 | PyObject *return_value = NULL; |
| 170 | double x; |
| 171 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 172 | x = PyFloat_AsDouble(arg); |
| 173 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 174 | goto exit; |
| 175 | } |
| 176 | return_value = math_modf_impl(module, x); |
| 177 | |
| 178 | exit: |
| 179 | return return_value; |
| 180 | } |
| 181 | |
| 182 | PyDoc_STRVAR(math_log__doc__, |
| 183 | "log(x, [base=math.e])\n" |
| 184 | "Return the logarithm of x to the given base.\n" |
| 185 | "\n" |
| 186 | "If the base not specified, returns the natural logarithm (base e) of x."); |
| 187 | |
| 188 | #define MATH_LOG_METHODDEF \ |
| 189 | {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__}, |
| 190 | |
| 191 | static PyObject * |
| 192 | math_log_impl(PyObject *module, PyObject *x, int group_right_1, |
| 193 | PyObject *base); |
| 194 | |
| 195 | static PyObject * |
| 196 | math_log(PyObject *module, PyObject *args) |
| 197 | { |
| 198 | PyObject *return_value = NULL; |
| 199 | PyObject *x; |
| 200 | int group_right_1 = 0; |
| 201 | PyObject *base = NULL; |
| 202 | |
| 203 | switch (PyTuple_GET_SIZE(args)) { |
| 204 | case 1: |
| 205 | if (!PyArg_ParseTuple(args, "O:log", &x)) { |
| 206 | goto exit; |
| 207 | } |
| 208 | break; |
| 209 | case 2: |
| 210 | if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) { |
| 211 | goto exit; |
| 212 | } |
| 213 | group_right_1 = 1; |
| 214 | break; |
| 215 | default: |
| 216 | PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments"); |
| 217 | goto exit; |
| 218 | } |
| 219 | return_value = math_log_impl(module, x, group_right_1, base); |
| 220 | |
| 221 | exit: |
| 222 | return return_value; |
| 223 | } |
| 224 | |
| 225 | PyDoc_STRVAR(math_log2__doc__, |
| 226 | "log2($module, x, /)\n" |
| 227 | "--\n" |
| 228 | "\n" |
| 229 | "Return the base 2 logarithm of x."); |
| 230 | |
| 231 | #define MATH_LOG2_METHODDEF \ |
| 232 | {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__}, |
| 233 | |
| 234 | PyDoc_STRVAR(math_log10__doc__, |
| 235 | "log10($module, x, /)\n" |
| 236 | "--\n" |
| 237 | "\n" |
| 238 | "Return the base 10 logarithm of x."); |
| 239 | |
| 240 | #define MATH_LOG10_METHODDEF \ |
| 241 | {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__}, |
| 242 | |
| 243 | PyDoc_STRVAR(math_fmod__doc__, |
| 244 | "fmod($module, x, y, /)\n" |
| 245 | "--\n" |
| 246 | "\n" |
| 247 | "Return fmod(x, y), according to platform C.\n" |
| 248 | "\n" |
| 249 | "x % y may differ."); |
| 250 | |
| 251 | #define MATH_FMOD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 252 | {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 253 | |
| 254 | static PyObject * |
| 255 | math_fmod_impl(PyObject *module, double x, double y); |
| 256 | |
| 257 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 258 | math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 259 | { |
| 260 | PyObject *return_value = NULL; |
| 261 | double x; |
| 262 | double y; |
| 263 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 264 | if (!_PyArg_ParseStack(args, nargs, "dd:fmod", |
| 265 | &x, &y)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 266 | goto exit; |
| 267 | } |
| 268 | return_value = math_fmod_impl(module, x, y); |
| 269 | |
| 270 | exit: |
| 271 | return return_value; |
| 272 | } |
| 273 | |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 274 | PyDoc_STRVAR(math_dist__doc__, |
| 275 | "dist($module, p, q, /)\n" |
| 276 | "--\n" |
| 277 | "\n" |
| 278 | "Return the Euclidean distance between two points p and q.\n" |
| 279 | "\n" |
| 280 | "The points should be specified as tuples of coordinates.\n" |
| 281 | "Both tuples must be the same size.\n" |
| 282 | "\n" |
| 283 | "Roughly equivalent to:\n" |
| 284 | " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))"); |
| 285 | |
| 286 | #define MATH_DIST_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 287 | {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__}, |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 288 | |
| 289 | static PyObject * |
| 290 | math_dist_impl(PyObject *module, PyObject *p, PyObject *q); |
| 291 | |
| 292 | static PyObject * |
| 293 | math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 294 | { |
| 295 | PyObject *return_value = NULL; |
| 296 | PyObject *p; |
| 297 | PyObject *q; |
| 298 | |
Raymond Hettinger | df81015 | 2018-09-29 14:30:38 -0700 | [diff] [blame] | 299 | if (!_PyArg_UnpackStack(args, nargs, "dist", |
| 300 | 2, 2, |
| 301 | &p, &q)) { |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 302 | goto exit; |
| 303 | } |
| 304 | return_value = math_dist_impl(module, p, q); |
| 305 | |
| 306 | exit: |
| 307 | return return_value; |
| 308 | } |
| 309 | |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 310 | PyDoc_STRVAR(math_pow__doc__, |
| 311 | "pow($module, x, y, /)\n" |
| 312 | "--\n" |
| 313 | "\n" |
| 314 | "Return x**y (x to the power of y)."); |
| 315 | |
| 316 | #define MATH_POW_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 317 | {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__}, |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 318 | |
| 319 | static PyObject * |
| 320 | math_pow_impl(PyObject *module, double x, double y); |
| 321 | |
| 322 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 323 | math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 324 | { |
| 325 | PyObject *return_value = NULL; |
| 326 | double x; |
| 327 | double y; |
| 328 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 329 | if (!_PyArg_ParseStack(args, nargs, "dd:pow", |
| 330 | &x, &y)) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 331 | goto exit; |
| 332 | } |
| 333 | return_value = math_pow_impl(module, x, y); |
| 334 | |
| 335 | exit: |
| 336 | return return_value; |
| 337 | } |
| 338 | |
| 339 | PyDoc_STRVAR(math_degrees__doc__, |
| 340 | "degrees($module, x, /)\n" |
| 341 | "--\n" |
| 342 | "\n" |
| 343 | "Convert angle x from radians to degrees."); |
| 344 | |
| 345 | #define MATH_DEGREES_METHODDEF \ |
| 346 | {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__}, |
| 347 | |
| 348 | static PyObject * |
| 349 | math_degrees_impl(PyObject *module, double x); |
| 350 | |
| 351 | static PyObject * |
| 352 | math_degrees(PyObject *module, PyObject *arg) |
| 353 | { |
| 354 | PyObject *return_value = NULL; |
| 355 | double x; |
| 356 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 357 | x = PyFloat_AsDouble(arg); |
| 358 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 359 | goto exit; |
| 360 | } |
| 361 | return_value = math_degrees_impl(module, x); |
| 362 | |
| 363 | exit: |
| 364 | return return_value; |
| 365 | } |
| 366 | |
| 367 | PyDoc_STRVAR(math_radians__doc__, |
| 368 | "radians($module, x, /)\n" |
| 369 | "--\n" |
| 370 | "\n" |
| 371 | "Convert angle x from degrees to radians."); |
| 372 | |
| 373 | #define MATH_RADIANS_METHODDEF \ |
| 374 | {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__}, |
| 375 | |
| 376 | static PyObject * |
| 377 | math_radians_impl(PyObject *module, double x); |
| 378 | |
| 379 | static PyObject * |
| 380 | math_radians(PyObject *module, PyObject *arg) |
| 381 | { |
| 382 | PyObject *return_value = NULL; |
| 383 | double x; |
| 384 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 385 | x = PyFloat_AsDouble(arg); |
| 386 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 387 | goto exit; |
| 388 | } |
| 389 | return_value = math_radians_impl(module, x); |
| 390 | |
| 391 | exit: |
| 392 | return return_value; |
| 393 | } |
| 394 | |
| 395 | PyDoc_STRVAR(math_isfinite__doc__, |
| 396 | "isfinite($module, x, /)\n" |
| 397 | "--\n" |
| 398 | "\n" |
| 399 | "Return True if x is neither an infinity nor a NaN, and False otherwise."); |
| 400 | |
| 401 | #define MATH_ISFINITE_METHODDEF \ |
| 402 | {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__}, |
| 403 | |
| 404 | static PyObject * |
| 405 | math_isfinite_impl(PyObject *module, double x); |
| 406 | |
| 407 | static PyObject * |
| 408 | math_isfinite(PyObject *module, PyObject *arg) |
| 409 | { |
| 410 | PyObject *return_value = NULL; |
| 411 | double x; |
| 412 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 413 | x = PyFloat_AsDouble(arg); |
| 414 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 415 | goto exit; |
| 416 | } |
| 417 | return_value = math_isfinite_impl(module, x); |
| 418 | |
| 419 | exit: |
| 420 | return return_value; |
| 421 | } |
| 422 | |
| 423 | PyDoc_STRVAR(math_isnan__doc__, |
| 424 | "isnan($module, x, /)\n" |
| 425 | "--\n" |
| 426 | "\n" |
| 427 | "Return True if x is a NaN (not a number), and False otherwise."); |
| 428 | |
| 429 | #define MATH_ISNAN_METHODDEF \ |
| 430 | {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__}, |
| 431 | |
| 432 | static PyObject * |
| 433 | math_isnan_impl(PyObject *module, double x); |
| 434 | |
| 435 | static PyObject * |
| 436 | math_isnan(PyObject *module, PyObject *arg) |
| 437 | { |
| 438 | PyObject *return_value = NULL; |
| 439 | double x; |
| 440 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 441 | x = PyFloat_AsDouble(arg); |
| 442 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
| 445 | return_value = math_isnan_impl(module, x); |
| 446 | |
| 447 | exit: |
| 448 | return return_value; |
| 449 | } |
| 450 | |
| 451 | PyDoc_STRVAR(math_isinf__doc__, |
| 452 | "isinf($module, x, /)\n" |
| 453 | "--\n" |
| 454 | "\n" |
| 455 | "Return True if x is a positive or negative infinity, and False otherwise."); |
| 456 | |
| 457 | #define MATH_ISINF_METHODDEF \ |
| 458 | {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__}, |
| 459 | |
| 460 | static PyObject * |
| 461 | math_isinf_impl(PyObject *module, double x); |
| 462 | |
| 463 | static PyObject * |
| 464 | math_isinf(PyObject *module, PyObject *arg) |
| 465 | { |
| 466 | PyObject *return_value = NULL; |
| 467 | double x; |
| 468 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 469 | x = PyFloat_AsDouble(arg); |
| 470 | if (PyErr_Occurred()) { |
Serhiy Storchaka | c9ea933 | 2017-01-19 18:13:09 +0200 | [diff] [blame] | 471 | goto exit; |
| 472 | } |
| 473 | return_value = math_isinf_impl(module, x); |
| 474 | |
| 475 | exit: |
| 476 | return return_value; |
| 477 | } |
| 478 | |
| 479 | PyDoc_STRVAR(math_isclose__doc__, |
| 480 | "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n" |
| 481 | "--\n" |
| 482 | "\n" |
| 483 | "Determine whether two floating point numbers are close in value.\n" |
| 484 | "\n" |
| 485 | " rel_tol\n" |
| 486 | " maximum difference for being considered \"close\", relative to the\n" |
| 487 | " magnitude of the input values\n" |
| 488 | " abs_tol\n" |
| 489 | " maximum difference for being considered \"close\", regardless of the\n" |
| 490 | " magnitude of the input values\n" |
| 491 | "\n" |
| 492 | "Return True if a is close in value to b, and False otherwise.\n" |
| 493 | "\n" |
| 494 | "For the values to be considered close, the difference between them\n" |
| 495 | "must be smaller than at least one of the tolerances.\n" |
| 496 | "\n" |
| 497 | "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n" |
| 498 | "is, NaN is not close to anything, even itself. inf and -inf are\n" |
| 499 | "only close to themselves."); |
| 500 | |
| 501 | #define MATH_ISCLOSE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 502 | {"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] | 503 | |
| 504 | static int |
| 505 | math_isclose_impl(PyObject *module, double a, double b, double rel_tol, |
| 506 | double abs_tol); |
| 507 | |
| 508 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 509 | 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] | 510 | { |
| 511 | PyObject *return_value = NULL; |
| 512 | static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; |
| 513 | static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0}; |
| 514 | double a; |
| 515 | double b; |
| 516 | double rel_tol = 1e-09; |
| 517 | double abs_tol = 0.0; |
| 518 | int _return_value; |
| 519 | |
| 520 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
| 521 | &a, &b, &rel_tol, &abs_tol)) { |
| 522 | goto exit; |
| 523 | } |
| 524 | _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol); |
| 525 | if ((_return_value == -1) && PyErr_Occurred()) { |
| 526 | goto exit; |
| 527 | } |
| 528 | return_value = PyBool_FromLong((long)_return_value); |
| 529 | |
| 530 | exit: |
| 531 | return return_value; |
| 532 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame^] | 533 | /*[clinic end generated code: output=da4b9940a5cb0188 input=a9049054013a1b77]*/ |