Debian Codegraph @

Modules/clinic/mathmodule.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_CheckPositional()
10
11 PyDoc_STRVAR(math_gcd__doc__,
12 "gcd($module, /, *integers)\n"
13 "--\n"
14 "\n"
15 "Greatest Common Divisor.");
16
17 #define MATH_GCD_METHODDEF \
18 {"gcd", _PyCFunction_CAST(math_gcd), METH_FASTCALL, math_gcd__doc__},
19
20 static PyObject *
21 math_gcd_impl(PyObject *module, PyObject * const *args,
22 Py_ssize_t args_length);
23
24 static PyObject *
25 math_gcd(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
26 {
27 PyObject *return_value = NULL;
28 PyObject * const *__clinic_args;
29 Py_ssize_t args_length;
30
31 __clinic_args = args;
32 args_length = nargs;
33 return_value = math_gcd_impl(module, __clinic_args, args_length);
34
35 return return_value;
36 }
37
38 PyDoc_STRVAR(math_lcm__doc__,
39 "lcm($module, /, *integers)\n"
40 "--\n"
41 "\n"
42 "Least Common Multiple.");
43
44 #define MATH_LCM_METHODDEF \
45 {"lcm", _PyCFunction_CAST(math_lcm), METH_FASTCALL, math_lcm__doc__},
46
47 static PyObject *
48 math_lcm_impl(PyObject *module, PyObject * const *args,
49 Py_ssize_t args_length);
50
51 static PyObject *
52 math_lcm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
53 {
54 PyObject *return_value = NULL;
55 PyObject * const *__clinic_args;
56 Py_ssize_t args_length;
57
58 __clinic_args = args;
59 args_length = nargs;
60 return_value = math_lcm_impl(module, __clinic_args, args_length);
61
62 return return_value;
63 }
64
65 PyDoc_STRVAR(math_ceil__doc__,
66 "ceil($module, x, /)\n"
67 "--\n"
68 "\n"
69 "Return the ceiling of x as an Integral.\n"
70 "\n"
71 "This is the smallest integer >= x.");
72
73 #define MATH_CEIL_METHODDEF \
74 {"ceil", (PyCFunction)math_ceil, METH_O, math_ceil__doc__},
75
76 PyDoc_STRVAR(math_floor__doc__,
77 "floor($module, x, /)\n"
78 "--\n"
79 "\n"
80 "Return the floor of x as an Integral.\n"
81 "\n"
82 "This is the largest integer <= x.");
83
84 #define MATH_FLOOR_METHODDEF \
85 {"floor", (PyCFunction)math_floor, METH_O, math_floor__doc__},
86
87 PyDoc_STRVAR(math_fsum__doc__,
88 "fsum($module, seq, /)\n"
89 "--\n"
90 "\n"
91 "Return an accurate floating-point sum of values in the iterable seq.\n"
92 "\n"
93 "Assumes IEEE-754 floating-point arithmetic.");
94
95 #define MATH_FSUM_METHODDEF \
96 {"fsum", (PyCFunction)math_fsum, METH_O, math_fsum__doc__},
97
98 PyDoc_STRVAR(math_isqrt__doc__,
99 "isqrt($module, n, /)\n"
100 "--\n"
101 "\n"
102 "Return the integer part of the square root of the input.");
103
104 #define MATH_ISQRT_METHODDEF \
105 {"isqrt", (PyCFunction)math_isqrt, METH_O, math_isqrt__doc__},
106
107 PyDoc_STRVAR(math_factorial__doc__,
108 "factorial($module, n, /)\n"
109 "--\n"
110 "\n"
111 "Find n!.");
112
113 #define MATH_FACTORIAL_METHODDEF \
114 {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__},
115
116 PyDoc_STRVAR(math_trunc__doc__,
117 "trunc($module, x, /)\n"
118 "--\n"
119 "\n"
120 "Truncates the Real x to the nearest Integral toward 0.\n"
121 "\n"
122 "Uses the __trunc__ magic method.");
123
124 #define MATH_TRUNC_METHODDEF \
125 {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
126
127 PyDoc_STRVAR(math_frexp__doc__,
128 "frexp($module, x, /)\n"
129 "--\n"
130 "\n"
131 "Return the mantissa and exponent of x, as pair (m, e).\n"
132 "\n"
133 "If x is a finite nonzero number, then m is a float with\n"
134 "0.5 <= abs(m) < 1.0 and an integer e is such that\n"
135 "x == m * 2**e exactly. Else, return (x, 0).");
136
137 #define MATH_FREXP_METHODDEF \
138 {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
139
140 static PyObject *
141 math_frexp_impl(PyObject *module, double x);
142
143 static PyObject *
144 math_frexp(PyObject *module, PyObject *arg)
145 {
146 PyObject *return_value = NULL;
147 double x;
148
149 if (PyFloat_CheckExact(arg)) {
150 x = PyFloat_AS_DOUBLE(arg);
151 }
152 else
153 {
154 x = PyFloat_AsDouble(arg);
155 if (x == -1.0 && PyErr_Occurred()) {
156 goto exit;
157 }
158 }
159 return_value = math_frexp_impl(module, x);
160
161 exit:
162 return return_value;
163 }
164
165 PyDoc_STRVAR(math_ldexp__doc__,
166 "ldexp($module, x, i, /)\n"
167 "--\n"
168 "\n"
169 "Return x * (2**i).\n"
170 "\n"
171 "This is essentially the inverse of frexp().");
172
173 #define MATH_LDEXP_METHODDEF \
174 {"ldexp", _PyCFunction_CAST(math_ldexp), METH_FASTCALL, math_ldexp__doc__},
175
176 static PyObject *
177 math_ldexp_impl(PyObject *module, double x, PyObject *i);
178
179 static PyObject *
180 math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
181 {
182 PyObject *return_value = NULL;
183 double x;
184 PyObject *i;
185
186 if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) {
187 goto exit;
188 }
189 if (PyFloat_CheckExact(args[0])) {
190 x = PyFloat_AS_DOUBLE(args[0]);
191 }
192 else
193 {
194 x = PyFloat_AsDouble(args[0]);
195 if (x == -1.0 && PyErr_Occurred()) {
196 goto exit;
197 }
198 }
199 i = args[1];
200 return_value = math_ldexp_impl(module, x, i);
201
202 exit:
203 return return_value;
204 }
205
206 PyDoc_STRVAR(math_modf__doc__,
207 "modf($module, x, /)\n"
208 "--\n"
209 "\n"
210 "Return the fractional and integer parts of x.\n"
211 "\n"
212 "Both results carry the sign of x and are floats.");
213
214 #define MATH_MODF_METHODDEF \
215 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
216
217 static PyObject *
218 math_modf_impl(PyObject *module, double x);
219
220 static PyObject *
221 math_modf(PyObject *module, PyObject *arg)
222 {
223 PyObject *return_value = NULL;
224 double x;
225
226 if (PyFloat_CheckExact(arg)) {
227 x = PyFloat_AS_DOUBLE(arg);
228 }
229 else
230 {
231 x = PyFloat_AsDouble(arg);
232 if (x == -1.0 && PyErr_Occurred()) {
233 goto exit;
234 }
235 }
236 return_value = math_modf_impl(module, x);
237
238 exit:
239 return return_value;
240 }
241
242 PyDoc_STRVAR(math_log2__doc__,
243 "log2($module, x, /)\n"
244 "--\n"
245 "\n"
246 "Return the base 2 logarithm of x.");
247
248 #define MATH_LOG2_METHODDEF \
249 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
250
251 PyDoc_STRVAR(math_log10__doc__,
252 "log10($module, x, /)\n"
253 "--\n"
254 "\n"
255 "Return the base 10 logarithm of x.");
256
257 #define MATH_LOG10_METHODDEF \
258 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
259
260 PyDoc_STRVAR(math_fma__doc__,
261 "fma($module, x, y, z, /)\n"
262 "--\n"
263 "\n"
264 "Fused multiply-add operation.\n"
265 "\n"
266 "Compute (x * y) + z with a single round.");
267
268 #define MATH_FMA_METHODDEF \
269 {"fma", _PyCFunction_CAST(math_fma), METH_FASTCALL, math_fma__doc__},
270
271 static PyObject *
272 math_fma_impl(PyObject *module, double x, double y, double z);
273
274 static PyObject *
275 math_fma(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
276 {
277 PyObject *return_value = NULL;
278 double x;
279 double y;
280 double z;
281
282 if (!_PyArg_CheckPositional("fma", nargs, 3, 3)) {
283 goto exit;
284 }
285 if (PyFloat_CheckExact(args[0])) {
286 x = PyFloat_AS_DOUBLE(args[0]);
287 }
288 else
289 {
290 x = PyFloat_AsDouble(args[0]);
291 if (x == -1.0 && PyErr_Occurred()) {
292 goto exit;
293 }
294 }
295 if (PyFloat_CheckExact(args[1])) {
296 y = PyFloat_AS_DOUBLE(args[1]);
297 }
298 else
299 {
300 y = PyFloat_AsDouble(args[1]);
301 if (y == -1.0 && PyErr_Occurred()) {
302 goto exit;
303 }
304 }
305 if (PyFloat_CheckExact(args[2])) {
306 z = PyFloat_AS_DOUBLE(args[2]);
307 }
308 else
309 {
310 z = PyFloat_AsDouble(args[2]);
311 if (z == -1.0 && PyErr_Occurred()) {
312 goto exit;
313 }
314 }
315 return_value = math_fma_impl(module, x, y, z);
316
317 exit:
318 return return_value;
319 }
320
321 PyDoc_STRVAR(math_fmod__doc__,
322 "fmod($module, x, y, /)\n"
323 "--\n"
324 "\n"
325 "Return fmod(x, y), according to platform C.\n"
326 "\n"
327 "x % y may differ.");
328
329 #define MATH_FMOD_METHODDEF \
330 {"fmod", _PyCFunction_CAST(math_fmod), METH_FASTCALL, math_fmod__doc__},
331
332 static PyObject *
333 math_fmod_impl(PyObject *module, double x, double y);
334
335 static PyObject *
336 math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
337 {
338 PyObject *return_value = NULL;
339 double x;
340 double y;
341
342 if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) {
343 goto exit;
344 }
345 if (PyFloat_CheckExact(args[0])) {
346 x = PyFloat_AS_DOUBLE(args[0]);
347 }
348 else
349 {
350 x = PyFloat_AsDouble(args[0]);
351 if (x == -1.0 && PyErr_Occurred()) {
352 goto exit;
353 }
354 }
355 if (PyFloat_CheckExact(args[1])) {
356 y = PyFloat_AS_DOUBLE(args[1]);
357 }
358 else
359 {
360 y = PyFloat_AsDouble(args[1]);
361 if (y == -1.0 && PyErr_Occurred()) {
362 goto exit;
363 }
364 }
365 return_value = math_fmod_impl(module, x, y);
366
367 exit:
368 return return_value;
369 }
370
371 PyDoc_STRVAR(math_dist__doc__,
372 "dist($module, p, q, /)\n"
373 "--\n"
374 "\n"
375 "Return the Euclidean distance between two points p and q.\n"
376 "\n"
377 "The points should be specified as sequences (or iterables) of\n"
378 "coordinates. Both inputs must have the same dimension.\n"
379 "\n"
380 "Roughly equivalent to:\n"
381 " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
382
383 #define MATH_DIST_METHODDEF \
384 {"dist", _PyCFunction_CAST(math_dist), METH_FASTCALL, math_dist__doc__},
385
386 static PyObject *
387 math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
388
389 static PyObject *
390 math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
391 {
392 PyObject *return_value = NULL;
393 PyObject *p;
394 PyObject *q;
395
396 if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
397 goto exit;
398 }
399 p = args[0];
400 q = args[1];
401 return_value = math_dist_impl(module, p, q);
402
403 exit:
404 return return_value;
405 }
406
407 PyDoc_STRVAR(math_hypot__doc__,
408 "hypot($module, /, *coordinates)\n"
409 "--\n"
410 "\n"
411 "Multidimensional Euclidean distance from the origin to a point.\n"
412 "\n"
413 "Roughly equivalent to:\n"
414 " sqrt(sum(x**2 for x in coordinates))\n"
415 "\n"
416 "For a two dimensional point (x, y), gives the hypotenuse\n"
417 "using the Pythagorean theorem: sqrt(x*x + y*y).\n"
418 "\n"
419 "For example, the hypotenuse of a 3/4/5 right triangle is:\n"
420 "\n"
421 " >>> hypot(3.0, 4.0)\n"
422 " 5.0");
423
424 #define MATH_HYPOT_METHODDEF \
425 {"hypot", _PyCFunction_CAST(math_hypot), METH_FASTCALL, math_hypot__doc__},
426
427 static PyObject *
428 math_hypot_impl(PyObject *module, PyObject * const *args,
429 Py_ssize_t args_length);
430
431 static PyObject *
432 math_hypot(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
433 {
434 PyObject *return_value = NULL;
435 PyObject * const *__clinic_args;
436 Py_ssize_t args_length;
437
438 __clinic_args = args;
439 args_length = nargs;
440 return_value = math_hypot_impl(module, __clinic_args, args_length);
441
442 return return_value;
443 }
444
445 PyDoc_STRVAR(math_sumprod__doc__,
446 "sumprod($module, p, q, /)\n"
447 "--\n"
448 "\n"
449 "Return the sum of products of values from two iterables p and q.\n"
450 "\n"
451 "Roughly equivalent to:\n"
452 "\n"
453 " sum(map(operator.mul, p, q, strict=True))\n"
454 "\n"
455 "For float and mixed int/float inputs, the intermediate products\n"
456 "and sums are computed with extended precision.");
457
458 #define MATH_SUMPROD_METHODDEF \
459 {"sumprod", _PyCFunction_CAST(math_sumprod), METH_FASTCALL, math_sumprod__doc__},
460
461 static PyObject *
462 math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q);
463
464 static PyObject *
465 math_sumprod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
466 {
467 PyObject *return_value = NULL;
468 PyObject *p;
469 PyObject *q;
470
471 if (!_PyArg_CheckPositional("sumprod", nargs, 2, 2)) {
472 goto exit;
473 }
474 p = args[0];
475 q = args[1];
476 return_value = math_sumprod_impl(module, p, q);
477
478 exit:
479 return return_value;
480 }
481
482 PyDoc_STRVAR(math_pow__doc__,
483 "pow($module, x, y, /)\n"
484 "--\n"
485 "\n"
486 "Return x**y (x to the power of y).");
487
488 #define MATH_POW_METHODDEF \
489 {"pow", _PyCFunction_CAST(math_pow), METH_FASTCALL, math_pow__doc__},
490
491 static PyObject *
492 math_pow_impl(PyObject *module, double x, double y);
493
494 static PyObject *
495 math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
496 {
497 PyObject *return_value = NULL;
498 double x;
499 double y;
500
501 if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
502 goto exit;
503 }
504 if (PyFloat_CheckExact(args[0])) {
505 x = PyFloat_AS_DOUBLE(args[0]);
506 }
507 else
508 {
509 x = PyFloat_AsDouble(args[0]);
510 if (x == -1.0 && PyErr_Occurred()) {
511 goto exit;
512 }
513 }
514 if (PyFloat_CheckExact(args[1])) {
515 y = PyFloat_AS_DOUBLE(args[1]);
516 }
517 else
518 {
519 y = PyFloat_AsDouble(args[1]);
520 if (y == -1.0 && PyErr_Occurred()) {
521 goto exit;
522 }
523 }
524 return_value = math_pow_impl(module, x, y);
525
526 exit:
527 return return_value;
528 }
529
530 PyDoc_STRVAR(math_degrees__doc__,
531 "degrees($module, x, /)\n"
532 "--\n"
533 "\n"
534 "Convert angle x from radians to degrees.");
535
536 #define MATH_DEGREES_METHODDEF \
537 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
538
539 static PyObject *
540 math_degrees_impl(PyObject *module, double x);
541
542 static PyObject *
543 math_degrees(PyObject *module, PyObject *arg)
544 {
545 PyObject *return_value = NULL;
546 double x;
547
548 if (PyFloat_CheckExact(arg)) {
549 x = PyFloat_AS_DOUBLE(arg);
550 }
551 else
552 {
553 x = PyFloat_AsDouble(arg);
554 if (x == -1.0 && PyErr_Occurred()) {
555 goto exit;
556 }
557 }
558 return_value = math_degrees_impl(module, x);
559
560 exit:
561 return return_value;
562 }
563
564 PyDoc_STRVAR(math_radians__doc__,
565 "radians($module, x, /)\n"
566 "--\n"
567 "\n"
568 "Convert angle x from degrees to radians.");
569
570 #define MATH_RADIANS_METHODDEF \
571 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
572
573 static PyObject *
574 math_radians_impl(PyObject *module, double x);
575
576 static PyObject *
577 math_radians(PyObject *module, PyObject *arg)
578 {
579 PyObject *return_value = NULL;
580 double x;
581
582 if (PyFloat_CheckExact(arg)) {
583 x = PyFloat_AS_DOUBLE(arg);
584 }
585 else
586 {
587 x = PyFloat_AsDouble(arg);
588 if (x == -1.0 && PyErr_Occurred()) {
589 goto exit;
590 }
591 }
592 return_value = math_radians_impl(module, x);
593
594 exit:
595 return return_value;
596 }
597
598 PyDoc_STRVAR(math_isfinite__doc__,
599 "isfinite($module, x, /)\n"
600 "--\n"
601 "\n"
602 "Return True if x is neither an infinity nor a NaN, and False otherwise.");
603
604 #define MATH_ISFINITE_METHODDEF \
605 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
606
607 static PyObject *
608 math_isfinite_impl(PyObject *module, double x);
609
610 static PyObject *
611 math_isfinite(PyObject *module, PyObject *arg)
612 {
613 PyObject *return_value = NULL;
614 double x;
615
616 if (PyFloat_CheckExact(arg)) {
617 x = PyFloat_AS_DOUBLE(arg);
618 }
619 else
620 {
621 x = PyFloat_AsDouble(arg);
622 if (x == -1.0 && PyErr_Occurred()) {
623 goto exit;
624 }
625 }
626 return_value = math_isfinite_impl(module, x);
627
628 exit:
629 return return_value;
630 }
631
632 PyDoc_STRVAR(math_isnan__doc__,
633 "isnan($module, x, /)\n"
634 "--\n"
635 "\n"
636 "Return True if x is a NaN (not a number), and False otherwise.");
637
638 #define MATH_ISNAN_METHODDEF \
639 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
640
641 static PyObject *
642 math_isnan_impl(PyObject *module, double x);
643
644 static PyObject *
645 math_isnan(PyObject *module, PyObject *arg)
646 {
647 PyObject *return_value = NULL;
648 double x;
649
650 if (PyFloat_CheckExact(arg)) {
651 x = PyFloat_AS_DOUBLE(arg);
652 }
653 else
654 {
655 x = PyFloat_AsDouble(arg);
656 if (x == -1.0 && PyErr_Occurred()) {
657 goto exit;
658 }
659 }
660 return_value = math_isnan_impl(module, x);
661
662 exit:
663 return return_value;
664 }
665
666 PyDoc_STRVAR(math_isinf__doc__,
667 "isinf($module, x, /)\n"
668 "--\n"
669 "\n"
670 "Return True if x is a positive or negative infinity, and False otherwise.");
671
672 #define MATH_ISINF_METHODDEF \
673 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
674
675 static PyObject *
676 math_isinf_impl(PyObject *module, double x);
677
678 static PyObject *
679 math_isinf(PyObject *module, PyObject *arg)
680 {
681 PyObject *return_value = NULL;
682 double x;
683
684 if (PyFloat_CheckExact(arg)) {
685 x = PyFloat_AS_DOUBLE(arg);
686 }
687 else
688 {
689 x = PyFloat_AsDouble(arg);
690 if (x == -1.0 && PyErr_Occurred()) {
691 goto exit;
692 }
693 }
694 return_value = math_isinf_impl(module, x);
695
696 exit:
697 return return_value;
698 }
699
700 PyDoc_STRVAR(math_isclose__doc__,
701 "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
702 "--\n"
703 "\n"
704 "Determine whether two floating-point numbers are close in value.\n"
705 "\n"
706 " rel_tol\n"
707 " maximum difference for being considered \"close\", relative to the\n"
708 " magnitude of the input values\n"
709 " abs_tol\n"
710 " maximum difference for being considered \"close\", regardless of the\n"
711 " magnitude of the input values\n"
712 "\n"
713 "Return True if a is close in value to b, and False otherwise.\n"
714 "\n"
715 "For the values to be considered close, the difference between them\n"
716 "must be smaller than at least one of the tolerances.\n"
717 "\n"
718 "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
719 "is, NaN is not close to anything, even itself. inf and -inf are\n"
720 "only close to themselves.");
721
722 #define MATH_ISCLOSE_METHODDEF \
723 {"isclose", _PyCFunction_CAST(math_isclose), METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
724
725 static int
726 math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
727 double abs_tol);
728
729 static PyObject *
730 math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
731 {
732 PyObject *return_value = NULL;
733 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
734
735 #define NUM_KEYWORDS 4
736 static struct {
737 PyGC_Head _this_is_not_used;
738 PyObject_VAR_HEAD
739 Py_hash_t ob_hash;
740 PyObject *ob_item[NUM_KEYWORDS];
741 } _kwtuple = {
742 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
743 .ob_hash = -1,
744 .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('b'), &_Py_ID(rel_tol), &_Py_ID(abs_tol), },
745 };
746 #undef NUM_KEYWORDS
747 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
748
749 #else // !Py_BUILD_CORE
750 # define KWTUPLE NULL
751 #endif // !Py_BUILD_CORE
752
753 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
754 static _PyArg_Parser _parser = {
755 .keywords = _keywords,
756 .fname = "isclose",
757 .kwtuple = KWTUPLE,
758 };
759 #undef KWTUPLE
760 PyObject *argsbuf[4];
761 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
762 double a;
763 double b;
764 double rel_tol = 1e-09;
765 double abs_tol = 0.0;
766 int _return_value;
767
768 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
769 /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
770 if (!args) {
771 goto exit;
772 }
773 if (PyFloat_CheckExact(args[0])) {
774 a = PyFloat_AS_DOUBLE(args[0]);
775 }
776 else
777 {
778 a = PyFloat_AsDouble(args[0]);
779 if (a == -1.0 && PyErr_Occurred()) {
780 goto exit;
781 }
782 }
783 if (PyFloat_CheckExact(args[1])) {
784 b = PyFloat_AS_DOUBLE(args[1]);
785 }
786 else
787 {
788 b = PyFloat_AsDouble(args[1]);
789 if (b == -1.0 && PyErr_Occurred()) {
790 goto exit;
791 }
792 }
793 if (!noptargs) {
794 goto skip_optional_kwonly;
795 }
796 if (args[2]) {
797 if (PyFloat_CheckExact(args[2])) {
798 rel_tol = PyFloat_AS_DOUBLE(args[2]);
799 }
800 else
801 {
802 rel_tol = PyFloat_AsDouble(args[2]);
803 if (rel_tol == -1.0 && PyErr_Occurred()) {
804 goto exit;
805 }
806 }
807 if (!--noptargs) {
808 goto skip_optional_kwonly;
809 }
810 }
811 if (PyFloat_CheckExact(args[3])) {
812 abs_tol = PyFloat_AS_DOUBLE(args[3]);
813 }
814 else
815 {
816 abs_tol = PyFloat_AsDouble(args[3]);
817 if (abs_tol == -1.0 && PyErr_Occurred()) {
818 goto exit;
819 }
820 }
821 skip_optional_kwonly:
822 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
823 if ((_return_value == -1) && PyErr_Occurred()) {
824 goto exit;
825 }
826 return_value = PyBool_FromLong((long)_return_value);
827
828 exit:
829 return return_value;
830 }
831
832 PyDoc_STRVAR(math_prod__doc__,
833 "prod($module, iterable, /, *, start=1)\n"
834 "--\n"
835 "\n"
836 "Calculate the product of all the elements in the input iterable.\n"
837 "\n"
838 "The default start value for the product is 1.\n"
839 "\n"
840 "When the iterable is empty, return the start value. This function is\n"
841 "intended specifically for use with numeric values and may reject\n"
842 "non-numeric types.");
843
844 #define MATH_PROD_METHODDEF \
845 {"prod", _PyCFunction_CAST(math_prod), METH_FASTCALL|METH_KEYWORDS, math_prod__doc__},
846
847 static PyObject *
848 math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start);
849
850 static PyObject *
851 math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
852 {
853 PyObject *return_value = NULL;
854 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
855
856 #define NUM_KEYWORDS 1
857 static struct {
858 PyGC_Head _this_is_not_used;
859 PyObject_VAR_HEAD
860 Py_hash_t ob_hash;
861 PyObject *ob_item[NUM_KEYWORDS];
862 } _kwtuple = {
863 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
864 .ob_hash = -1,
865 .ob_item = { &_Py_ID(start), },
866 };
867 #undef NUM_KEYWORDS
868 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
869
870 #else // !Py_BUILD_CORE
871 # define KWTUPLE NULL
872 #endif // !Py_BUILD_CORE
873
874 static const char * const _keywords[] = {"", "start", NULL};
875 static _PyArg_Parser _parser = {
876 .keywords = _keywords,
877 .fname = "prod",
878 .kwtuple = KWTUPLE,
879 };
880 #undef KWTUPLE
881 PyObject *argsbuf[2];
882 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
883 PyObject *iterable;
884 PyObject *start = NULL;
885
886 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
887 /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
888 if (!args) {
889 goto exit;
890 }
891 iterable = args[0];
892 if (!noptargs) {
893 goto skip_optional_kwonly;
894 }
895 start = args[1];
896 skip_optional_kwonly:
897 return_value = math_prod_impl(module, iterable, start);
898
899 exit:
900 return return_value;
901 }
902
903 PyDoc_STRVAR(math_perm__doc__,
904 "perm($module, n, k=None, /)\n"
905 "--\n"
906 "\n"
907 "Number of ways to choose k items from n items without repetition and with order.\n"
908 "\n"
909 "Evaluates to n! / (n - k)! when k <= n and evaluates\n"
910 "to zero when k > n.\n"
911 "\n"
912 "If k is not specified or is None, then k defaults to n\n"
913 "and the function returns n!.\n"
914 "\n"
915 "Raises TypeError if either of the arguments are not integers.\n"
916 "Raises ValueError if either of the arguments are negative.");
917
918 #define MATH_PERM_METHODDEF \
919 {"perm", _PyCFunction_CAST(math_perm), METH_FASTCALL, math_perm__doc__},
920
921 static PyObject *
922 math_perm_impl(PyObject *module, PyObject *n, PyObject *k);
923
924 static PyObject *
925 math_perm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
926 {
927 PyObject *return_value = NULL;
928 PyObject *n;
929 PyObject *k = Py_None;
930
931 if (!_PyArg_CheckPositional("perm", nargs, 1, 2)) {
932 goto exit;
933 }
934 n = args[0];
935 if (nargs < 2) {
936 goto skip_optional;
937 }
938 k = args[1];
939 skip_optional:
940 return_value = math_perm_impl(module, n, k);
941
942 exit:
943 return return_value;
944 }
945
946 PyDoc_STRVAR(math_comb__doc__,
947 "comb($module, n, k, /)\n"
948 "--\n"
949 "\n"
950 "Number of ways to choose k items from n items without repetition and without order.\n"
951 "\n"
952 "Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n"
953 "to zero when k > n.\n"
954 "\n"
955 "Also called the binomial coefficient because it is equivalent\n"
956 "to the coefficient of k-th term in polynomial expansion of the\n"
957 "expression (1 + x)**n.\n"
958 "\n"
959 "Raises TypeError if either of the arguments are not integers.\n"
960 "Raises ValueError if either of the arguments are negative.");
961
962 #define MATH_COMB_METHODDEF \
963 {"comb", _PyCFunction_CAST(math_comb), METH_FASTCALL, math_comb__doc__},
964
965 static PyObject *
966 math_comb_impl(PyObject *module, PyObject *n, PyObject *k);
967
968 static PyObject *
969 math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
970 {
971 PyObject *return_value = NULL;
972 PyObject *n;
973 PyObject *k;
974
975 if (!_PyArg_CheckPositional("comb", nargs, 2, 2)) {
976 goto exit;
977 }
978 n = args[0];
979 k = args[1];
980 return_value = math_comb_impl(module, n, k);
981
982 exit:
983 return return_value;
984 }
985
986 PyDoc_STRVAR(math_nextafter__doc__,
987 "nextafter($module, x, y, /, *, steps=None)\n"
988 "--\n"
989 "\n"
990 "Return the floating-point value the given number of steps after x towards y.\n"
991 "\n"
992 "If steps is not specified or is None, it defaults to 1.\n"
993 "\n"
994 "Raises a TypeError, if x or y is not a double, or if steps is not\n"
995 "an integer. Raises ValueError if steps is negative.");
996
997 #define MATH_NEXTAFTER_METHODDEF \
998 {"nextafter", _PyCFunction_CAST(math_nextafter), METH_FASTCALL|METH_KEYWORDS, math_nextafter__doc__},
999
1000 static PyObject *
1001 math_nextafter_impl(PyObject *module, double x, double y, PyObject *steps);
1002
1003 static PyObject *
1004 math_nextafter(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1005 {
1006 PyObject *return_value = NULL;
1007 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1008
1009 #define NUM_KEYWORDS 1
1010 static struct {
1011 PyGC_Head _this_is_not_used;
1012 PyObject_VAR_HEAD
1013 Py_hash_t ob_hash;
1014 PyObject *ob_item[NUM_KEYWORDS];
1015 } _kwtuple = {
1016 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1017 .ob_hash = -1,
1018 .ob_item = { &_Py_ID(steps), },
1019 };
1020 #undef NUM_KEYWORDS
1021 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1022
1023 #else // !Py_BUILD_CORE
1024 # define KWTUPLE NULL
1025 #endif // !Py_BUILD_CORE
1026
1027 static const char * const _keywords[] = {"", "", "steps", NULL};
1028 static _PyArg_Parser _parser = {
1029 .keywords = _keywords,
1030 .fname = "nextafter",
1031 .kwtuple = KWTUPLE,
1032 };
1033 #undef KWTUPLE
1034 PyObject *argsbuf[3];
1035 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
1036 double x;
1037 double y;
1038 PyObject *steps = Py_None;
1039
1040 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1041 /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1042 if (!args) {
1043 goto exit;
1044 }
1045 if (PyFloat_CheckExact(args[0])) {
1046 x = PyFloat_AS_DOUBLE(args[0]);
1047 }
1048 else
1049 {
1050 x = PyFloat_AsDouble(args[0]);
1051 if (x == -1.0 && PyErr_Occurred()) {
1052 goto exit;
1053 }
1054 }
1055 if (PyFloat_CheckExact(args[1])) {
1056 y = PyFloat_AS_DOUBLE(args[1]);
1057 }
1058 else
1059 {
1060 y = PyFloat_AsDouble(args[1]);
1061 if (y == -1.0 && PyErr_Occurred()) {
1062 goto exit;
1063 }
1064 }
1065 if (!noptargs) {
1066 goto skip_optional_kwonly;
1067 }
1068 steps = args[2];
1069 skip_optional_kwonly:
1070 return_value = math_nextafter_impl(module, x, y, steps);
1071
1072 exit:
1073 return return_value;
1074 }
1075
1076 PyDoc_STRVAR(math_ulp__doc__,
1077 "ulp($module, x, /)\n"
1078 "--\n"
1079 "\n"
1080 "Return the value of the least significant bit of the float x.");
1081
1082 #define MATH_ULP_METHODDEF \
1083 {"ulp", (PyCFunction)math_ulp, METH_O, math_ulp__doc__},
1084
1085 static double
1086 math_ulp_impl(PyObject *module, double x);
1087
1088 static PyObject *
1089 math_ulp(PyObject *module, PyObject *arg)
1090 {
1091 PyObject *return_value = NULL;
1092 double x;
1093 double _return_value;
1094
1095 if (PyFloat_CheckExact(arg)) {
1096 x = PyFloat_AS_DOUBLE(arg);
1097 }
1098 else
1099 {
1100 x = PyFloat_AsDouble(arg);
1101 if (x == -1.0 && PyErr_Occurred()) {
1102 goto exit;
1103 }
1104 }
1105 _return_value = math_ulp_impl(module, x);
1106 if ((_return_value == -1.0) && PyErr_Occurred()) {
1107 goto exit;
1108 }
1109 return_value = PyFloat_FromDouble(_return_value);
1110
1111 exit:
1112 return return_value;
1113 }
1114 /*[clinic end generated code: output=18e16eef548c9c43 input=a9049054013a1b77]*/