blob: b99a8deecea11e30e72d3f0842b2a6b66414cf99 [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
68PyDoc_STRVAR(math_factorial__doc__,
69"factorial($module, x, /)\n"
70"--\n"
71"\n"
72"Find x!.\n"
73"\n"
74"Raise a ValueError if x is negative or non-integral.");
75
76#define MATH_FACTORIAL_METHODDEF \
77 {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__},
78
79PyDoc_STRVAR(math_trunc__doc__,
80"trunc($module, x, /)\n"
81"--\n"
82"\n"
83"Truncates the Real x to the nearest Integral toward 0.\n"
84"\n"
85"Uses the __trunc__ magic method.");
86
87#define MATH_TRUNC_METHODDEF \
88 {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
89
90PyDoc_STRVAR(math_frexp__doc__,
91"frexp($module, x, /)\n"
92"--\n"
93"\n"
94"Return the mantissa and exponent of x, as pair (m, e).\n"
95"\n"
96"m is a float and e is an int, such that x = m * 2.**e.\n"
97"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
98
99#define MATH_FREXP_METHODDEF \
100 {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
101
102static PyObject *
103math_frexp_impl(PyObject *module, double x);
104
105static PyObject *
106math_frexp(PyObject *module, PyObject *arg)
107{
108 PyObject *return_value = NULL;
109 double x;
110
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200111 x = PyFloat_AsDouble(arg);
112 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200113 goto exit;
114 }
115 return_value = math_frexp_impl(module, x);
116
117exit:
118 return return_value;
119}
120
121PyDoc_STRVAR(math_ldexp__doc__,
122"ldexp($module, x, i, /)\n"
123"--\n"
124"\n"
125"Return x * (2**i).\n"
126"\n"
127"This is essentially the inverse of frexp().");
128
129#define MATH_LDEXP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200130 {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200131
132static PyObject *
133math_ldexp_impl(PyObject *module, double x, PyObject *i);
134
135static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200136math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200137{
138 PyObject *return_value = NULL;
139 double x;
140 PyObject *i;
141
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200142 if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200143 goto exit;
144 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200145 x = PyFloat_AsDouble(args[0]);
146 if (PyErr_Occurred()) {
147 goto exit;
148 }
149 i = args[1];
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200150 return_value = math_ldexp_impl(module, x, i);
151
152exit:
153 return return_value;
154}
155
156PyDoc_STRVAR(math_modf__doc__,
157"modf($module, x, /)\n"
158"--\n"
159"\n"
160"Return the fractional and integer parts of x.\n"
161"\n"
162"Both results carry the sign of x and are floats.");
163
164#define MATH_MODF_METHODDEF \
165 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
166
167static PyObject *
168math_modf_impl(PyObject *module, double x);
169
170static PyObject *
171math_modf(PyObject *module, PyObject *arg)
172{
173 PyObject *return_value = NULL;
174 double x;
175
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200176 x = PyFloat_AsDouble(arg);
177 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200178 goto exit;
179 }
180 return_value = math_modf_impl(module, x);
181
182exit:
183 return return_value;
184}
185
186PyDoc_STRVAR(math_log__doc__,
187"log(x, [base=math.e])\n"
188"Return the logarithm of x to the given base.\n"
189"\n"
190"If the base not specified, returns the natural logarithm (base e) of x.");
191
192#define MATH_LOG_METHODDEF \
193 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
194
195static PyObject *
196math_log_impl(PyObject *module, PyObject *x, int group_right_1,
197 PyObject *base);
198
199static PyObject *
200math_log(PyObject *module, PyObject *args)
201{
202 PyObject *return_value = NULL;
203 PyObject *x;
204 int group_right_1 = 0;
205 PyObject *base = NULL;
206
207 switch (PyTuple_GET_SIZE(args)) {
208 case 1:
209 if (!PyArg_ParseTuple(args, "O:log", &x)) {
210 goto exit;
211 }
212 break;
213 case 2:
214 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
215 goto exit;
216 }
217 group_right_1 = 1;
218 break;
219 default:
220 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
221 goto exit;
222 }
223 return_value = math_log_impl(module, x, group_right_1, base);
224
225exit:
226 return return_value;
227}
228
229PyDoc_STRVAR(math_log2__doc__,
230"log2($module, x, /)\n"
231"--\n"
232"\n"
233"Return the base 2 logarithm of x.");
234
235#define MATH_LOG2_METHODDEF \
236 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
237
238PyDoc_STRVAR(math_log10__doc__,
239"log10($module, x, /)\n"
240"--\n"
241"\n"
242"Return the base 10 logarithm of x.");
243
244#define MATH_LOG10_METHODDEF \
245 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
246
247PyDoc_STRVAR(math_fmod__doc__,
248"fmod($module, x, y, /)\n"
249"--\n"
250"\n"
251"Return fmod(x, y), according to platform C.\n"
252"\n"
253"x % y may differ.");
254
255#define MATH_FMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200256 {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200257
258static PyObject *
259math_fmod_impl(PyObject *module, double x, double y);
260
261static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200262math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200263{
264 PyObject *return_value = NULL;
265 double x;
266 double y;
267
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200268 if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) {
269 goto exit;
270 }
271 x = PyFloat_AsDouble(args[0]);
272 if (PyErr_Occurred()) {
273 goto exit;
274 }
275 y = PyFloat_AsDouble(args[1]);
276 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200277 goto exit;
278 }
279 return_value = math_fmod_impl(module, x, y);
280
281exit:
282 return return_value;
283}
284
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700285PyDoc_STRVAR(math_dist__doc__,
286"dist($module, p, q, /)\n"
287"--\n"
288"\n"
289"Return the Euclidean distance between two points p and q.\n"
290"\n"
291"The points should be specified as tuples of coordinates.\n"
292"Both tuples must be the same size.\n"
293"\n"
294"Roughly equivalent to:\n"
295" sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
296
297#define MATH_DIST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200298 {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__},
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700299
300static PyObject *
301math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
302
303static PyObject *
304math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
305{
306 PyObject *return_value = NULL;
307 PyObject *p;
308 PyObject *q;
309
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200310 if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700311 goto exit;
312 }
Ammar Askarcb08a712019-01-12 01:23:41 -0500313 if (!PyTuple_Check(args[0])) {
314 _PyArg_BadArgument("dist", 1, "tuple", args[0]);
315 goto exit;
316 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200317 p = args[0];
Ammar Askarcb08a712019-01-12 01:23:41 -0500318 if (!PyTuple_Check(args[1])) {
319 _PyArg_BadArgument("dist", 2, "tuple", args[1]);
320 goto exit;
321 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200322 q = args[1];
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700323 return_value = math_dist_impl(module, p, q);
324
325exit:
326 return return_value;
327}
328
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200329PyDoc_STRVAR(math_pow__doc__,
330"pow($module, x, y, /)\n"
331"--\n"
332"\n"
333"Return x**y (x to the power of y).");
334
335#define MATH_POW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200336 {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200337
338static PyObject *
339math_pow_impl(PyObject *module, double x, double y);
340
341static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200342math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200343{
344 PyObject *return_value = NULL;
345 double x;
346 double y;
347
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200348 if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
349 goto exit;
350 }
351 x = PyFloat_AsDouble(args[0]);
352 if (PyErr_Occurred()) {
353 goto exit;
354 }
355 y = PyFloat_AsDouble(args[1]);
356 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200357 goto exit;
358 }
359 return_value = math_pow_impl(module, x, y);
360
361exit:
362 return return_value;
363}
364
365PyDoc_STRVAR(math_degrees__doc__,
366"degrees($module, x, /)\n"
367"--\n"
368"\n"
369"Convert angle x from radians to degrees.");
370
371#define MATH_DEGREES_METHODDEF \
372 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
373
374static PyObject *
375math_degrees_impl(PyObject *module, double x);
376
377static PyObject *
378math_degrees(PyObject *module, PyObject *arg)
379{
380 PyObject *return_value = NULL;
381 double x;
382
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200383 x = PyFloat_AsDouble(arg);
384 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200385 goto exit;
386 }
387 return_value = math_degrees_impl(module, x);
388
389exit:
390 return return_value;
391}
392
393PyDoc_STRVAR(math_radians__doc__,
394"radians($module, x, /)\n"
395"--\n"
396"\n"
397"Convert angle x from degrees to radians.");
398
399#define MATH_RADIANS_METHODDEF \
400 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
401
402static PyObject *
403math_radians_impl(PyObject *module, double x);
404
405static PyObject *
406math_radians(PyObject *module, PyObject *arg)
407{
408 PyObject *return_value = NULL;
409 double x;
410
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200411 x = PyFloat_AsDouble(arg);
412 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200413 goto exit;
414 }
415 return_value = math_radians_impl(module, x);
416
417exit:
418 return return_value;
419}
420
421PyDoc_STRVAR(math_isfinite__doc__,
422"isfinite($module, x, /)\n"
423"--\n"
424"\n"
425"Return True if x is neither an infinity nor a NaN, and False otherwise.");
426
427#define MATH_ISFINITE_METHODDEF \
428 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
429
430static PyObject *
431math_isfinite_impl(PyObject *module, double x);
432
433static PyObject *
434math_isfinite(PyObject *module, PyObject *arg)
435{
436 PyObject *return_value = NULL;
437 double x;
438
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200439 x = PyFloat_AsDouble(arg);
440 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200441 goto exit;
442 }
443 return_value = math_isfinite_impl(module, x);
444
445exit:
446 return return_value;
447}
448
449PyDoc_STRVAR(math_isnan__doc__,
450"isnan($module, x, /)\n"
451"--\n"
452"\n"
453"Return True if x is a NaN (not a number), and False otherwise.");
454
455#define MATH_ISNAN_METHODDEF \
456 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
457
458static PyObject *
459math_isnan_impl(PyObject *module, double x);
460
461static PyObject *
462math_isnan(PyObject *module, PyObject *arg)
463{
464 PyObject *return_value = NULL;
465 double x;
466
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200467 x = PyFloat_AsDouble(arg);
468 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200469 goto exit;
470 }
471 return_value = math_isnan_impl(module, x);
472
473exit:
474 return return_value;
475}
476
477PyDoc_STRVAR(math_isinf__doc__,
478"isinf($module, x, /)\n"
479"--\n"
480"\n"
481"Return True if x is a positive or negative infinity, and False otherwise.");
482
483#define MATH_ISINF_METHODDEF \
484 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
485
486static PyObject *
487math_isinf_impl(PyObject *module, double x);
488
489static PyObject *
490math_isinf(PyObject *module, PyObject *arg)
491{
492 PyObject *return_value = NULL;
493 double x;
494
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200495 x = PyFloat_AsDouble(arg);
496 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200497 goto exit;
498 }
499 return_value = math_isinf_impl(module, x);
500
501exit:
502 return return_value;
503}
504
505PyDoc_STRVAR(math_isclose__doc__,
506"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
507"--\n"
508"\n"
509"Determine whether two floating point numbers are close in value.\n"
510"\n"
511" rel_tol\n"
512" maximum difference for being considered \"close\", relative to the\n"
513" magnitude of the input values\n"
514" abs_tol\n"
515" maximum difference for being considered \"close\", regardless of the\n"
516" magnitude of the input values\n"
517"\n"
518"Return True if a is close in value to b, and False otherwise.\n"
519"\n"
520"For the values to be considered close, the difference between them\n"
521"must be smaller than at least one of the tolerances.\n"
522"\n"
523"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
524"is, NaN is not close to anything, even itself. inf and -inf are\n"
525"only close to themselves.");
526
527#define MATH_ISCLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200528 {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200529
530static int
531math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
532 double abs_tol);
533
534static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200535math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200536{
537 PyObject *return_value = NULL;
538 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
539 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
540 double a;
541 double b;
542 double rel_tol = 1e-09;
543 double abs_tol = 0.0;
544 int _return_value;
545
546 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
547 &a, &b, &rel_tol, &abs_tol)) {
548 goto exit;
549 }
550 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
551 if ((_return_value == -1) && PyErr_Occurred()) {
552 goto exit;
553 }
554 return_value = PyBool_FromLong((long)_return_value);
555
556exit:
557 return return_value;
558}
Pablo Galindobc098512019-02-07 07:04:02 +0000559
560PyDoc_STRVAR(math_prod__doc__,
561"prod($module, iterable, /, *, start=1)\n"
562"--\n"
563"\n"
564"Calculate the product of all the elements in the input iterable.\n"
565"\n"
566"The default start value for the product is 1.\n"
567"\n"
568"When the iterable is empty, return the start value. This function is\n"
569"intended specifically for use with numeric values and may reject\n"
570"non-numeric types.");
571
572#define MATH_PROD_METHODDEF \
573 {"prod", (PyCFunction)(void(*)(void))math_prod, METH_FASTCALL|METH_KEYWORDS, math_prod__doc__},
574
575static PyObject *
576math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start);
577
578static PyObject *
579math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
580{
581 PyObject *return_value = NULL;
582 static const char * const _keywords[] = {"", "start", NULL};
583 static _PyArg_Parser _parser = {"O|$O:prod", _keywords, 0};
584 PyObject *iterable;
585 PyObject *start = NULL;
586
587 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
588 &iterable, &start)) {
589 goto exit;
590 }
591 return_value = math_prod_impl(module, iterable, start);
592
593exit:
594 return return_value;
595}
596/*[clinic end generated code: output=20505690ca6fe402 input=a9049054013a1b77]*/