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