blob: c8d80296f7b6f2ef6848f229440b02b4b09a68df [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
Sylvain74453812017-06-10 06:51:48 +020024 if (!_PyArg_UnpackStack(args, nargs, "gcd",
25 2, 2,
26 &a, &b)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020027 goto exit;
28 }
29 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
111 if (!PyArg_Parse(arg, "d:frexp", &x)) {
112 goto exit;
113 }
114 return_value = math_frexp_impl(module, x);
115
116exit:
117 return return_value;
118}
119
120PyDoc_STRVAR(math_ldexp__doc__,
121"ldexp($module, x, i, /)\n"
122"--\n"
123"\n"
124"Return x * (2**i).\n"
125"\n"
126"This is essentially the inverse of frexp().");
127
128#define MATH_LDEXP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200129 {"ldexp", (PyCFunction)(void(*)(void))math_ldexp, METH_FASTCALL, math_ldexp__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200130
131static PyObject *
132math_ldexp_impl(PyObject *module, double x, PyObject *i);
133
134static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200135math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200136{
137 PyObject *return_value = NULL;
138 double x;
139 PyObject *i;
140
Sylvain74453812017-06-10 06:51:48 +0200141 if (!_PyArg_ParseStack(args, nargs, "dO:ldexp",
142 &x, &i)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200143 goto exit;
144 }
145 return_value = math_ldexp_impl(module, x, i);
146
147exit:
148 return return_value;
149}
150
151PyDoc_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
162static PyObject *
163math_modf_impl(PyObject *module, double x);
164
165static PyObject *
166math_modf(PyObject *module, PyObject *arg)
167{
168 PyObject *return_value = NULL;
169 double x;
170
171 if (!PyArg_Parse(arg, "d:modf", &x)) {
172 goto exit;
173 }
174 return_value = math_modf_impl(module, x);
175
176exit:
177 return return_value;
178}
179
180PyDoc_STRVAR(math_log__doc__,
181"log(x, [base=math.e])\n"
182"Return the logarithm of x to the given base.\n"
183"\n"
184"If the base not specified, returns the natural logarithm (base e) of x.");
185
186#define MATH_LOG_METHODDEF \
187 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
188
189static PyObject *
190math_log_impl(PyObject *module, PyObject *x, int group_right_1,
191 PyObject *base);
192
193static PyObject *
194math_log(PyObject *module, PyObject *args)
195{
196 PyObject *return_value = NULL;
197 PyObject *x;
198 int group_right_1 = 0;
199 PyObject *base = NULL;
200
201 switch (PyTuple_GET_SIZE(args)) {
202 case 1:
203 if (!PyArg_ParseTuple(args, "O:log", &x)) {
204 goto exit;
205 }
206 break;
207 case 2:
208 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
209 goto exit;
210 }
211 group_right_1 = 1;
212 break;
213 default:
214 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
215 goto exit;
216 }
217 return_value = math_log_impl(module, x, group_right_1, base);
218
219exit:
220 return return_value;
221}
222
223PyDoc_STRVAR(math_log2__doc__,
224"log2($module, x, /)\n"
225"--\n"
226"\n"
227"Return the base 2 logarithm of x.");
228
229#define MATH_LOG2_METHODDEF \
230 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
231
232PyDoc_STRVAR(math_log10__doc__,
233"log10($module, x, /)\n"
234"--\n"
235"\n"
236"Return the base 10 logarithm of x.");
237
238#define MATH_LOG10_METHODDEF \
239 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
240
241PyDoc_STRVAR(math_fmod__doc__,
242"fmod($module, x, y, /)\n"
243"--\n"
244"\n"
245"Return fmod(x, y), according to platform C.\n"
246"\n"
247"x % y may differ.");
248
249#define MATH_FMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200250 {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200251
252static PyObject *
253math_fmod_impl(PyObject *module, double x, double y);
254
255static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200256math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200257{
258 PyObject *return_value = NULL;
259 double x;
260 double y;
261
Sylvain74453812017-06-10 06:51:48 +0200262 if (!_PyArg_ParseStack(args, nargs, "dd:fmod",
263 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200264 goto exit;
265 }
266 return_value = math_fmod_impl(module, x, y);
267
268exit:
269 return return_value;
270}
271
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700272PyDoc_STRVAR(math_dist__doc__,
273"dist($module, p, q, /)\n"
274"--\n"
275"\n"
276"Return the Euclidean distance between two points p and q.\n"
277"\n"
278"The points should be specified as tuples of coordinates.\n"
279"Both tuples must be the same size.\n"
280"\n"
281"Roughly equivalent to:\n"
282" sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
283
284#define MATH_DIST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200285 {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__},
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700286
287static PyObject *
288math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
289
290static PyObject *
291math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
292{
293 PyObject *return_value = NULL;
294 PyObject *p;
295 PyObject *q;
296
Raymond Hettingerdf810152018-09-29 14:30:38 -0700297 if (!_PyArg_UnpackStack(args, nargs, "dist",
298 2, 2,
299 &p, &q)) {
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700300 goto exit;
301 }
302 return_value = math_dist_impl(module, p, q);
303
304exit:
305 return return_value;
306}
307
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200308PyDoc_STRVAR(math_pow__doc__,
309"pow($module, x, y, /)\n"
310"--\n"
311"\n"
312"Return x**y (x to the power of y).");
313
314#define MATH_POW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200315 {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200316
317static PyObject *
318math_pow_impl(PyObject *module, double x, double y);
319
320static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200321math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200322{
323 PyObject *return_value = NULL;
324 double x;
325 double y;
326
Sylvain74453812017-06-10 06:51:48 +0200327 if (!_PyArg_ParseStack(args, nargs, "dd:pow",
328 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200329 goto exit;
330 }
331 return_value = math_pow_impl(module, x, y);
332
333exit:
334 return return_value;
335}
336
337PyDoc_STRVAR(math_degrees__doc__,
338"degrees($module, x, /)\n"
339"--\n"
340"\n"
341"Convert angle x from radians to degrees.");
342
343#define MATH_DEGREES_METHODDEF \
344 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
345
346static PyObject *
347math_degrees_impl(PyObject *module, double x);
348
349static PyObject *
350math_degrees(PyObject *module, PyObject *arg)
351{
352 PyObject *return_value = NULL;
353 double x;
354
355 if (!PyArg_Parse(arg, "d:degrees", &x)) {
356 goto exit;
357 }
358 return_value = math_degrees_impl(module, x);
359
360exit:
361 return return_value;
362}
363
364PyDoc_STRVAR(math_radians__doc__,
365"radians($module, x, /)\n"
366"--\n"
367"\n"
368"Convert angle x from degrees to radians.");
369
370#define MATH_RADIANS_METHODDEF \
371 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
372
373static PyObject *
374math_radians_impl(PyObject *module, double x);
375
376static PyObject *
377math_radians(PyObject *module, PyObject *arg)
378{
379 PyObject *return_value = NULL;
380 double x;
381
382 if (!PyArg_Parse(arg, "d:radians", &x)) {
383 goto exit;
384 }
385 return_value = math_radians_impl(module, x);
386
387exit:
388 return return_value;
389}
390
391PyDoc_STRVAR(math_isfinite__doc__,
392"isfinite($module, x, /)\n"
393"--\n"
394"\n"
395"Return True if x is neither an infinity nor a NaN, and False otherwise.");
396
397#define MATH_ISFINITE_METHODDEF \
398 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
399
400static PyObject *
401math_isfinite_impl(PyObject *module, double x);
402
403static PyObject *
404math_isfinite(PyObject *module, PyObject *arg)
405{
406 PyObject *return_value = NULL;
407 double x;
408
409 if (!PyArg_Parse(arg, "d:isfinite", &x)) {
410 goto exit;
411 }
412 return_value = math_isfinite_impl(module, x);
413
414exit:
415 return return_value;
416}
417
418PyDoc_STRVAR(math_isnan__doc__,
419"isnan($module, x, /)\n"
420"--\n"
421"\n"
422"Return True if x is a NaN (not a number), and False otherwise.");
423
424#define MATH_ISNAN_METHODDEF \
425 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
426
427static PyObject *
428math_isnan_impl(PyObject *module, double x);
429
430static PyObject *
431math_isnan(PyObject *module, PyObject *arg)
432{
433 PyObject *return_value = NULL;
434 double x;
435
436 if (!PyArg_Parse(arg, "d:isnan", &x)) {
437 goto exit;
438 }
439 return_value = math_isnan_impl(module, x);
440
441exit:
442 return return_value;
443}
444
445PyDoc_STRVAR(math_isinf__doc__,
446"isinf($module, x, /)\n"
447"--\n"
448"\n"
449"Return True if x is a positive or negative infinity, and False otherwise.");
450
451#define MATH_ISINF_METHODDEF \
452 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
453
454static PyObject *
455math_isinf_impl(PyObject *module, double x);
456
457static PyObject *
458math_isinf(PyObject *module, PyObject *arg)
459{
460 PyObject *return_value = NULL;
461 double x;
462
463 if (!PyArg_Parse(arg, "d:isinf", &x)) {
464 goto exit;
465 }
466 return_value = math_isinf_impl(module, x);
467
468exit:
469 return return_value;
470}
471
472PyDoc_STRVAR(math_isclose__doc__,
473"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
474"--\n"
475"\n"
476"Determine whether two floating point numbers are close in value.\n"
477"\n"
478" rel_tol\n"
479" maximum difference for being considered \"close\", relative to the\n"
480" magnitude of the input values\n"
481" abs_tol\n"
482" maximum difference for being considered \"close\", regardless of the\n"
483" magnitude of the input values\n"
484"\n"
485"Return True if a is close in value to b, and False otherwise.\n"
486"\n"
487"For the values to be considered close, the difference between them\n"
488"must be smaller than at least one of the tolerances.\n"
489"\n"
490"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
491"is, NaN is not close to anything, even itself. inf and -inf are\n"
492"only close to themselves.");
493
494#define MATH_ISCLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200495 {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200496
497static int
498math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
499 double abs_tol);
500
501static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200502math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200503{
504 PyObject *return_value = NULL;
505 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
506 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
507 double a;
508 double b;
509 double rel_tol = 1e-09;
510 double abs_tol = 0.0;
511 int _return_value;
512
513 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
514 &a, &b, &rel_tol, &abs_tol)) {
515 goto exit;
516 }
517 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
518 if ((_return_value == -1) && PyErr_Occurred()) {
519 goto exit;
520 }
521 return_value = PyBool_FromLong((long)_return_value);
522
523exit:
524 return return_value;
525}
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200526/*[clinic end generated code: output=8b1709a71e5fb855 input=a9049054013a1b77]*/