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