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