blob: f95d291d41f8046d2e927a9531918bd5a7be9f21 [file] [log] [blame]
Serhiy Storchakac9ea9332017-01-19 18:13:09 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +020012 {"gcd", (PyCFunction)(void(*)(void))math_gcd, METH_FASTCALL, math_gcd__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020013
14static PyObject *
15math_gcd_impl(PyObject *module, PyObject *a, PyObject *b);
16
17static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020018math_gcd(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020019{
20 PyObject *return_value = NULL;
21 PyObject *a;
22 PyObject *b;
23
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020024 if (!_PyArg_CheckPositional("gcd", nargs, 2, 2)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020025 goto exit;
26 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020027 a = args[0];
28 b = args[1];
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020029 return_value = math_gcd_impl(module, a, b);
30
31exit:
32 return return_value;
33}
34
35PyDoc_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
46PyDoc_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
57PyDoc_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
Mark Dickinson73934b92019-05-18 12:29:50 +010068PyDoc_STRVAR(math_isqrt__doc__,
69"isqrt($module, n, /)\n"
70"--\n"
71"\n"
72"Return the integer part of the square root of the input.");
73
74#define MATH_ISQRT_METHODDEF \
75 {"isqrt", (PyCFunction)math_isqrt, METH_O, math_isqrt__doc__},
76
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020077PyDoc_STRVAR(math_factorial__doc__,
78"factorial($module, x, /)\n"
79"--\n"
80"\n"
81"Find x!.\n"
82"\n"
83"Raise a ValueError if x is negative or non-integral.");
84
85#define MATH_FACTORIAL_METHODDEF \
86 {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__},
87
88PyDoc_STRVAR(math_trunc__doc__,
89"trunc($module, x, /)\n"
90"--\n"
91"\n"
92"Truncates the Real x to the nearest Integral toward 0.\n"
93"\n"
94"Uses the __trunc__ magic method.");
95
96#define MATH_TRUNC_METHODDEF \
97 {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
98
99PyDoc_STRVAR(math_frexp__doc__,
100"frexp($module, x, /)\n"
101"--\n"
102"\n"
103"Return the mantissa and exponent of x, as pair (m, e).\n"
104"\n"
105"m is a float and e is an int, such that x = m * 2.**e.\n"
106"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
107
108#define MATH_FREXP_METHODDEF \
109 {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
110
111static PyObject *
112math_frexp_impl(PyObject *module, double x);
113
114static PyObject *
115math_frexp(PyObject *module, PyObject *arg)
116{
117 PyObject *return_value = NULL;
118 double x;
119
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700120 if (PyFloat_CheckExact(arg)) {
121 x = PyFloat_AS_DOUBLE(arg);
122 }
123 else
124 {
125 x = PyFloat_AsDouble(arg);
126 if (x == -1.0 && PyErr_Occurred()) {
127 goto exit;
128 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200129 }
130 return_value = math_frexp_impl(module, x);
131
132exit:
133 return return_value;
134}
135
136PyDoc_STRVAR(math_ldexp__doc__,
137"ldexp($module, x, i, /)\n"
138"--\n"
139"\n"
140"Return x * (2**i).\n"
141"\n"
142"This is essentially the inverse of frexp().");
143
144#define MATH_LDEXP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200145 {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200146
147static PyObject *
148math_ldexp_impl(PyObject *module, double x, PyObject *i);
149
150static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200151math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200152{
153 PyObject *return_value = NULL;
154 double x;
155 PyObject *i;
156
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200157 if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200158 goto exit;
159 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700160 if (PyFloat_CheckExact(args[0])) {
161 x = PyFloat_AS_DOUBLE(args[0]);
162 }
163 else
164 {
165 x = PyFloat_AsDouble(args[0]);
166 if (x == -1.0 && PyErr_Occurred()) {
167 goto exit;
168 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200169 }
170 i = args[1];
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200171 return_value = math_ldexp_impl(module, x, i);
172
173exit:
174 return return_value;
175}
176
177PyDoc_STRVAR(math_modf__doc__,
178"modf($module, x, /)\n"
179"--\n"
180"\n"
181"Return the fractional and integer parts of x.\n"
182"\n"
183"Both results carry the sign of x and are floats.");
184
185#define MATH_MODF_METHODDEF \
186 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
187
188static PyObject *
189math_modf_impl(PyObject *module, double x);
190
191static PyObject *
192math_modf(PyObject *module, PyObject *arg)
193{
194 PyObject *return_value = NULL;
195 double x;
196
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700197 if (PyFloat_CheckExact(arg)) {
198 x = PyFloat_AS_DOUBLE(arg);
199 }
200 else
201 {
202 x = PyFloat_AsDouble(arg);
203 if (x == -1.0 && PyErr_Occurred()) {
204 goto exit;
205 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200206 }
207 return_value = math_modf_impl(module, x);
208
209exit:
210 return return_value;
211}
212
213PyDoc_STRVAR(math_log__doc__,
214"log(x, [base=math.e])\n"
215"Return the logarithm of x to the given base.\n"
216"\n"
217"If the base not specified, returns the natural logarithm (base e) of x.");
218
219#define MATH_LOG_METHODDEF \
220 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
221
222static PyObject *
223math_log_impl(PyObject *module, PyObject *x, int group_right_1,
224 PyObject *base);
225
226static PyObject *
227math_log(PyObject *module, PyObject *args)
228{
229 PyObject *return_value = NULL;
230 PyObject *x;
231 int group_right_1 = 0;
232 PyObject *base = NULL;
233
234 switch (PyTuple_GET_SIZE(args)) {
235 case 1:
236 if (!PyArg_ParseTuple(args, "O:log", &x)) {
237 goto exit;
238 }
239 break;
240 case 2:
241 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
242 goto exit;
243 }
244 group_right_1 = 1;
245 break;
246 default:
247 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
248 goto exit;
249 }
250 return_value = math_log_impl(module, x, group_right_1, base);
251
252exit:
253 return return_value;
254}
255
256PyDoc_STRVAR(math_log2__doc__,
257"log2($module, x, /)\n"
258"--\n"
259"\n"
260"Return the base 2 logarithm of x.");
261
262#define MATH_LOG2_METHODDEF \
263 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
264
265PyDoc_STRVAR(math_log10__doc__,
266"log10($module, x, /)\n"
267"--\n"
268"\n"
269"Return the base 10 logarithm of x.");
270
271#define MATH_LOG10_METHODDEF \
272 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
273
274PyDoc_STRVAR(math_fmod__doc__,
275"fmod($module, x, y, /)\n"
276"--\n"
277"\n"
278"Return fmod(x, y), according to platform C.\n"
279"\n"
280"x % y may differ.");
281
282#define MATH_FMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200283 {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200284
285static PyObject *
286math_fmod_impl(PyObject *module, double x, double y);
287
288static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200289math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200290{
291 PyObject *return_value = NULL;
292 double x;
293 double y;
294
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200295 if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) {
296 goto exit;
297 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700298 if (PyFloat_CheckExact(args[0])) {
299 x = PyFloat_AS_DOUBLE(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200300 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700301 else
302 {
303 x = PyFloat_AsDouble(args[0]);
304 if (x == -1.0 && PyErr_Occurred()) {
305 goto exit;
306 }
307 }
308 if (PyFloat_CheckExact(args[1])) {
309 y = PyFloat_AS_DOUBLE(args[1]);
310 }
311 else
312 {
313 y = PyFloat_AsDouble(args[1]);
314 if (y == -1.0 && PyErr_Occurred()) {
315 goto exit;
316 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200317 }
318 return_value = math_fmod_impl(module, x, y);
319
320exit:
321 return return_value;
322}
323
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700324PyDoc_STRVAR(math_dist__doc__,
325"dist($module, p, q, /)\n"
326"--\n"
327"\n"
328"Return the Euclidean distance between two points p and q.\n"
329"\n"
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -0700330"The points should be specified as sequences (or iterables) of\n"
331"coordinates. Both inputs must have the same dimension.\n"
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700332"\n"
333"Roughly equivalent to:\n"
334" sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
335
336#define MATH_DIST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200337 {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__},
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700338
339static PyObject *
340math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
341
342static PyObject *
343math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
344{
345 PyObject *return_value = NULL;
346 PyObject *p;
347 PyObject *q;
348
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200349 if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700350 goto exit;
351 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200352 p = args[0];
353 q = args[1];
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700354 return_value = math_dist_impl(module, p, q);
355
356exit:
357 return return_value;
358}
359
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200360PyDoc_STRVAR(math_pow__doc__,
361"pow($module, x, y, /)\n"
362"--\n"
363"\n"
364"Return x**y (x to the power of y).");
365
366#define MATH_POW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200367 {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200368
369static PyObject *
370math_pow_impl(PyObject *module, double x, double y);
371
372static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200373math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200374{
375 PyObject *return_value = NULL;
376 double x;
377 double y;
378
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200379 if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
380 goto exit;
381 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700382 if (PyFloat_CheckExact(args[0])) {
383 x = PyFloat_AS_DOUBLE(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200384 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700385 else
386 {
387 x = PyFloat_AsDouble(args[0]);
388 if (x == -1.0 && PyErr_Occurred()) {
389 goto exit;
390 }
391 }
392 if (PyFloat_CheckExact(args[1])) {
393 y = PyFloat_AS_DOUBLE(args[1]);
394 }
395 else
396 {
397 y = PyFloat_AsDouble(args[1]);
398 if (y == -1.0 && PyErr_Occurred()) {
399 goto exit;
400 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200401 }
402 return_value = math_pow_impl(module, x, y);
403
404exit:
405 return return_value;
406}
407
408PyDoc_STRVAR(math_degrees__doc__,
409"degrees($module, x, /)\n"
410"--\n"
411"\n"
412"Convert angle x from radians to degrees.");
413
414#define MATH_DEGREES_METHODDEF \
415 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
416
417static PyObject *
418math_degrees_impl(PyObject *module, double x);
419
420static PyObject *
421math_degrees(PyObject *module, PyObject *arg)
422{
423 PyObject *return_value = NULL;
424 double x;
425
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700426 if (PyFloat_CheckExact(arg)) {
427 x = PyFloat_AS_DOUBLE(arg);
428 }
429 else
430 {
431 x = PyFloat_AsDouble(arg);
432 if (x == -1.0 && PyErr_Occurred()) {
433 goto exit;
434 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200435 }
436 return_value = math_degrees_impl(module, x);
437
438exit:
439 return return_value;
440}
441
442PyDoc_STRVAR(math_radians__doc__,
443"radians($module, x, /)\n"
444"--\n"
445"\n"
446"Convert angle x from degrees to radians.");
447
448#define MATH_RADIANS_METHODDEF \
449 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
450
451static PyObject *
452math_radians_impl(PyObject *module, double x);
453
454static PyObject *
455math_radians(PyObject *module, PyObject *arg)
456{
457 PyObject *return_value = NULL;
458 double x;
459
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700460 if (PyFloat_CheckExact(arg)) {
461 x = PyFloat_AS_DOUBLE(arg);
462 }
463 else
464 {
465 x = PyFloat_AsDouble(arg);
466 if (x == -1.0 && PyErr_Occurred()) {
467 goto exit;
468 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200469 }
470 return_value = math_radians_impl(module, x);
471
472exit:
473 return return_value;
474}
475
476PyDoc_STRVAR(math_isfinite__doc__,
477"isfinite($module, x, /)\n"
478"--\n"
479"\n"
480"Return True if x is neither an infinity nor a NaN, and False otherwise.");
481
482#define MATH_ISFINITE_METHODDEF \
483 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
484
485static PyObject *
486math_isfinite_impl(PyObject *module, double x);
487
488static PyObject *
489math_isfinite(PyObject *module, PyObject *arg)
490{
491 PyObject *return_value = NULL;
492 double x;
493
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700494 if (PyFloat_CheckExact(arg)) {
495 x = PyFloat_AS_DOUBLE(arg);
496 }
497 else
498 {
499 x = PyFloat_AsDouble(arg);
500 if (x == -1.0 && PyErr_Occurred()) {
501 goto exit;
502 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200503 }
504 return_value = math_isfinite_impl(module, x);
505
506exit:
507 return return_value;
508}
509
510PyDoc_STRVAR(math_isnan__doc__,
511"isnan($module, x, /)\n"
512"--\n"
513"\n"
514"Return True if x is a NaN (not a number), and False otherwise.");
515
516#define MATH_ISNAN_METHODDEF \
517 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
518
519static PyObject *
520math_isnan_impl(PyObject *module, double x);
521
522static PyObject *
523math_isnan(PyObject *module, PyObject *arg)
524{
525 PyObject *return_value = NULL;
526 double x;
527
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700528 if (PyFloat_CheckExact(arg)) {
529 x = PyFloat_AS_DOUBLE(arg);
530 }
531 else
532 {
533 x = PyFloat_AsDouble(arg);
534 if (x == -1.0 && PyErr_Occurred()) {
535 goto exit;
536 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200537 }
538 return_value = math_isnan_impl(module, x);
539
540exit:
541 return return_value;
542}
543
544PyDoc_STRVAR(math_isinf__doc__,
545"isinf($module, x, /)\n"
546"--\n"
547"\n"
548"Return True if x is a positive or negative infinity, and False otherwise.");
549
550#define MATH_ISINF_METHODDEF \
551 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
552
553static PyObject *
554math_isinf_impl(PyObject *module, double x);
555
556static PyObject *
557math_isinf(PyObject *module, PyObject *arg)
558{
559 PyObject *return_value = NULL;
560 double x;
561
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700562 if (PyFloat_CheckExact(arg)) {
563 x = PyFloat_AS_DOUBLE(arg);
564 }
565 else
566 {
567 x = PyFloat_AsDouble(arg);
568 if (x == -1.0 && PyErr_Occurred()) {
569 goto exit;
570 }
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200571 }
572 return_value = math_isinf_impl(module, x);
573
574exit:
575 return return_value;
576}
577
578PyDoc_STRVAR(math_isclose__doc__,
579"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
580"--\n"
581"\n"
582"Determine whether two floating point numbers are close in value.\n"
583"\n"
584" rel_tol\n"
585" maximum difference for being considered \"close\", relative to the\n"
586" magnitude of the input values\n"
587" abs_tol\n"
588" maximum difference for being considered \"close\", regardless of the\n"
589" magnitude of the input values\n"
590"\n"
591"Return True if a is close in value to b, and False otherwise.\n"
592"\n"
593"For the values to be considered close, the difference between them\n"
594"must be smaller than at least one of the tolerances.\n"
595"\n"
596"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
597"is, NaN is not close to anything, even itself. inf and -inf are\n"
598"only close to themselves.");
599
600#define MATH_ISCLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200601 {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200602
603static int
604math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
605 double abs_tol);
606
607static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200608math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200609{
610 PyObject *return_value = NULL;
611 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200612 static _PyArg_Parser _parser = {NULL, _keywords, "isclose", 0};
613 PyObject *argsbuf[4];
614 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200615 double a;
616 double b;
617 double rel_tol = 1e-09;
618 double abs_tol = 0.0;
619 int _return_value;
620
Serhiy Storchaka31913912019-03-14 10:32:22 +0200621 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
622 if (!args) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200623 goto exit;
624 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700625 if (PyFloat_CheckExact(args[0])) {
626 a = PyFloat_AS_DOUBLE(args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200627 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700628 else
629 {
630 a = PyFloat_AsDouble(args[0]);
631 if (a == -1.0 && PyErr_Occurred()) {
632 goto exit;
633 }
634 }
635 if (PyFloat_CheckExact(args[1])) {
636 b = PyFloat_AS_DOUBLE(args[1]);
637 }
638 else
639 {
640 b = PyFloat_AsDouble(args[1]);
641 if (b == -1.0 && PyErr_Occurred()) {
642 goto exit;
643 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200644 }
645 if (!noptargs) {
646 goto skip_optional_kwonly;
647 }
648 if (args[2]) {
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700649 if (PyFloat_CheckExact(args[2])) {
650 rel_tol = PyFloat_AS_DOUBLE(args[2]);
651 }
652 else
653 {
654 rel_tol = PyFloat_AsDouble(args[2]);
655 if (rel_tol == -1.0 && PyErr_Occurred()) {
656 goto exit;
657 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200658 }
659 if (!--noptargs) {
660 goto skip_optional_kwonly;
661 }
662 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700663 if (PyFloat_CheckExact(args[3])) {
664 abs_tol = PyFloat_AS_DOUBLE(args[3]);
665 }
666 else
667 {
668 abs_tol = PyFloat_AsDouble(args[3]);
669 if (abs_tol == -1.0 && PyErr_Occurred()) {
670 goto exit;
671 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200672 }
673skip_optional_kwonly:
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200674 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
675 if ((_return_value == -1) && PyErr_Occurred()) {
676 goto exit;
677 }
678 return_value = PyBool_FromLong((long)_return_value);
679
680exit:
681 return return_value;
682}
Pablo Galindobc098512019-02-07 07:04:02 +0000683
684PyDoc_STRVAR(math_prod__doc__,
685"prod($module, iterable, /, *, start=1)\n"
686"--\n"
687"\n"
688"Calculate the product of all the elements in the input iterable.\n"
689"\n"
690"The default start value for the product is 1.\n"
691"\n"
692"When the iterable is empty, return the start value. This function is\n"
693"intended specifically for use with numeric values and may reject\n"
694"non-numeric types.");
695
696#define MATH_PROD_METHODDEF \
697 {"prod", (PyCFunction)(void(*)(void))math_prod, METH_FASTCALL|METH_KEYWORDS, math_prod__doc__},
698
699static PyObject *
700math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start);
701
702static PyObject *
703math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
704{
705 PyObject *return_value = NULL;
706 static const char * const _keywords[] = {"", "start", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200707 static _PyArg_Parser _parser = {NULL, _keywords, "prod", 0};
708 PyObject *argsbuf[2];
709 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Pablo Galindobc098512019-02-07 07:04:02 +0000710 PyObject *iterable;
711 PyObject *start = NULL;
712
Serhiy Storchaka31913912019-03-14 10:32:22 +0200713 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
714 if (!args) {
Pablo Galindobc098512019-02-07 07:04:02 +0000715 goto exit;
716 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200717 iterable = args[0];
718 if (!noptargs) {
719 goto skip_optional_kwonly;
720 }
721 start = args[1];
722skip_optional_kwonly:
Pablo Galindobc098512019-02-07 07:04:02 +0000723 return_value = math_prod_impl(module, iterable, start);
724
725exit:
726 return return_value;
727}
Yash Aggarwal4a686502019-06-01 12:51:27 +0530728
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300729PyDoc_STRVAR(math_perm__doc__,
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700730"perm($module, n, k=None, /)\n"
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300731"--\n"
732"\n"
733"Number of ways to choose k items from n items without repetition and with order.\n"
734"\n"
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700735"Evaluates to n! / (n - k)! when k <= n and evaluates\n"
736"to zero when k > n.\n"
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300737"\n"
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700738"If k is not specified or is None, then k defaults to n\n"
739"and the function returns n!.\n"
740"\n"
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700741"Raises TypeError if either of the arguments are not integers.\n"
742"Raises ValueError if either of the arguments are negative.");
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300743
744#define MATH_PERM_METHODDEF \
745 {"perm", (PyCFunction)(void(*)(void))math_perm, METH_FASTCALL, math_perm__doc__},
746
747static PyObject *
748math_perm_impl(PyObject *module, PyObject *n, PyObject *k);
749
750static PyObject *
751math_perm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
752{
753 PyObject *return_value = NULL;
754 PyObject *n;
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700755 PyObject *k = Py_None;
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300756
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700757 if (!_PyArg_CheckPositional("perm", nargs, 1, 2)) {
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300758 goto exit;
759 }
760 n = args[0];
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700761 if (nargs < 2) {
762 goto skip_optional;
763 }
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300764 k = args[1];
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700765skip_optional:
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300766 return_value = math_perm_impl(module, n, k);
767
768exit:
769 return return_value;
770}
771
Yash Aggarwal4a686502019-06-01 12:51:27 +0530772PyDoc_STRVAR(math_comb__doc__,
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300773"comb($module, n, k, /)\n"
Yash Aggarwal4a686502019-06-01 12:51:27 +0530774"--\n"
775"\n"
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300776"Number of ways to choose k items from n items without repetition and without order.\n"
Yash Aggarwal4a686502019-06-01 12:51:27 +0530777"\n"
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700778"Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n"
779"to zero when k > n.\n"
Yash Aggarwal4a686502019-06-01 12:51:27 +0530780"\n"
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700781"Also called the binomial coefficient because it is equivalent\n"
782"to the coefficient of k-th term in polynomial expansion of the\n"
783"expression (1 + x)**n.\n"
784"\n"
785"Raises TypeError if either of the arguments are not integers.\n"
786"Raises ValueError if either of the arguments are negative.");
Yash Aggarwal4a686502019-06-01 12:51:27 +0530787
788#define MATH_COMB_METHODDEF \
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300789 {"comb", (PyCFunction)(void(*)(void))math_comb, METH_FASTCALL, math_comb__doc__},
Yash Aggarwal4a686502019-06-01 12:51:27 +0530790
791static PyObject *
792math_comb_impl(PyObject *module, PyObject *n, PyObject *k);
793
794static PyObject *
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300795math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Yash Aggarwal4a686502019-06-01 12:51:27 +0530796{
797 PyObject *return_value = NULL;
Yash Aggarwal4a686502019-06-01 12:51:27 +0530798 PyObject *n;
799 PyObject *k;
800
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300801 if (!_PyArg_CheckPositional("comb", nargs, 2, 2)) {
Yash Aggarwal4a686502019-06-01 12:51:27 +0530802 goto exit;
803 }
804 n = args[0];
Yash Aggarwal4a686502019-06-01 12:51:27 +0530805 k = args[1];
806 return_value = math_comb_impl(module, n, k);
807
808exit:
809 return return_value;
810}
Victor Stinner100fafc2020-01-12 02:15:42 +0100811
812PyDoc_STRVAR(math_nextafter__doc__,
813"nextafter($module, x, y, /)\n"
814"--\n"
815"\n"
816"Return the next floating-point value after x towards y.");
817
818#define MATH_NEXTAFTER_METHODDEF \
819 {"nextafter", (PyCFunction)(void(*)(void))math_nextafter, METH_FASTCALL, math_nextafter__doc__},
820
821static PyObject *
822math_nextafter_impl(PyObject *module, double x, double y);
823
824static PyObject *
825math_nextafter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
826{
827 PyObject *return_value = NULL;
828 double x;
829 double y;
830
831 if (!_PyArg_CheckPositional("nextafter", nargs, 2, 2)) {
832 goto exit;
833 }
834 if (PyFloat_CheckExact(args[0])) {
835 x = PyFloat_AS_DOUBLE(args[0]);
836 }
837 else
838 {
839 x = PyFloat_AsDouble(args[0]);
840 if (x == -1.0 && PyErr_Occurred()) {
841 goto exit;
842 }
843 }
844 if (PyFloat_CheckExact(args[1])) {
845 y = PyFloat_AS_DOUBLE(args[1]);
846 }
847 else
848 {
849 y = PyFloat_AsDouble(args[1]);
850 if (y == -1.0 && PyErr_Occurred()) {
851 goto exit;
852 }
853 }
854 return_value = math_nextafter_impl(module, x, y);
855
856exit:
857 return return_value;
858}
Victor Stinner0b2ab212020-01-13 12:44:35 +0100859
860PyDoc_STRVAR(math_ulp__doc__,
861"ulp($module, x, /)\n"
862"--\n"
863"\n"
864"Return the value of the least significant bit of the float x.");
865
866#define MATH_ULP_METHODDEF \
867 {"ulp", (PyCFunction)math_ulp, METH_O, math_ulp__doc__},
868
869static double
870math_ulp_impl(PyObject *module, double x);
871
872static PyObject *
873math_ulp(PyObject *module, PyObject *arg)
874{
875 PyObject *return_value = NULL;
876 double x;
877 double _return_value;
878
879 if (PyFloat_CheckExact(arg)) {
880 x = PyFloat_AS_DOUBLE(arg);
881 }
882 else
883 {
884 x = PyFloat_AsDouble(arg);
885 if (x == -1.0 && PyErr_Occurred()) {
886 goto exit;
887 }
888 }
889 _return_value = math_ulp_impl(module, x);
890 if ((_return_value == -1.0) && PyErr_Occurred()) {
891 goto exit;
892 }
893 return_value = PyFloat_FromDouble(_return_value);
894
895exit:
896 return return_value;
897}
898/*[clinic end generated code: output=9b51d215dbcac060 input=a9049054013a1b77]*/