blob: 1c8fc2fc2c3feea8e50c6bcf45bb7da8e74e3baa [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 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200313 p = args[0];
314 q = args[1];
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700315 return_value = math_dist_impl(module, p, q);
316
317exit:
318 return return_value;
319}
320
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200321PyDoc_STRVAR(math_pow__doc__,
322"pow($module, x, y, /)\n"
323"--\n"
324"\n"
325"Return x**y (x to the power of y).");
326
327#define MATH_POW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200328 {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200329
330static PyObject *
331math_pow_impl(PyObject *module, double x, double y);
332
333static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200334math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200335{
336 PyObject *return_value = NULL;
337 double x;
338 double y;
339
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200340 if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
341 goto exit;
342 }
343 x = PyFloat_AsDouble(args[0]);
344 if (PyErr_Occurred()) {
345 goto exit;
346 }
347 y = PyFloat_AsDouble(args[1]);
348 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200349 goto exit;
350 }
351 return_value = math_pow_impl(module, x, y);
352
353exit:
354 return return_value;
355}
356
357PyDoc_STRVAR(math_degrees__doc__,
358"degrees($module, x, /)\n"
359"--\n"
360"\n"
361"Convert angle x from radians to degrees.");
362
363#define MATH_DEGREES_METHODDEF \
364 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
365
366static PyObject *
367math_degrees_impl(PyObject *module, double x);
368
369static PyObject *
370math_degrees(PyObject *module, PyObject *arg)
371{
372 PyObject *return_value = NULL;
373 double x;
374
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200375 x = PyFloat_AsDouble(arg);
376 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200377 goto exit;
378 }
379 return_value = math_degrees_impl(module, x);
380
381exit:
382 return return_value;
383}
384
385PyDoc_STRVAR(math_radians__doc__,
386"radians($module, x, /)\n"
387"--\n"
388"\n"
389"Convert angle x from degrees to radians.");
390
391#define MATH_RADIANS_METHODDEF \
392 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
393
394static PyObject *
395math_radians_impl(PyObject *module, double x);
396
397static PyObject *
398math_radians(PyObject *module, PyObject *arg)
399{
400 PyObject *return_value = NULL;
401 double x;
402
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200403 x = PyFloat_AsDouble(arg);
404 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200405 goto exit;
406 }
407 return_value = math_radians_impl(module, x);
408
409exit:
410 return return_value;
411}
412
413PyDoc_STRVAR(math_isfinite__doc__,
414"isfinite($module, x, /)\n"
415"--\n"
416"\n"
417"Return True if x is neither an infinity nor a NaN, and False otherwise.");
418
419#define MATH_ISFINITE_METHODDEF \
420 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
421
422static PyObject *
423math_isfinite_impl(PyObject *module, double x);
424
425static PyObject *
426math_isfinite(PyObject *module, PyObject *arg)
427{
428 PyObject *return_value = NULL;
429 double x;
430
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200431 x = PyFloat_AsDouble(arg);
432 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200433 goto exit;
434 }
435 return_value = math_isfinite_impl(module, x);
436
437exit:
438 return return_value;
439}
440
441PyDoc_STRVAR(math_isnan__doc__,
442"isnan($module, x, /)\n"
443"--\n"
444"\n"
445"Return True if x is a NaN (not a number), and False otherwise.");
446
447#define MATH_ISNAN_METHODDEF \
448 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
449
450static PyObject *
451math_isnan_impl(PyObject *module, double x);
452
453static PyObject *
454math_isnan(PyObject *module, PyObject *arg)
455{
456 PyObject *return_value = NULL;
457 double x;
458
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200459 x = PyFloat_AsDouble(arg);
460 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200461 goto exit;
462 }
463 return_value = math_isnan_impl(module, x);
464
465exit:
466 return return_value;
467}
468
469PyDoc_STRVAR(math_isinf__doc__,
470"isinf($module, x, /)\n"
471"--\n"
472"\n"
473"Return True if x is a positive or negative infinity, and False otherwise.");
474
475#define MATH_ISINF_METHODDEF \
476 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
477
478static PyObject *
479math_isinf_impl(PyObject *module, double x);
480
481static PyObject *
482math_isinf(PyObject *module, PyObject *arg)
483{
484 PyObject *return_value = NULL;
485 double x;
486
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200487 x = PyFloat_AsDouble(arg);
488 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200489 goto exit;
490 }
491 return_value = math_isinf_impl(module, x);
492
493exit:
494 return return_value;
495}
496
497PyDoc_STRVAR(math_isclose__doc__,
498"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
499"--\n"
500"\n"
501"Determine whether two floating point numbers are close in value.\n"
502"\n"
503" rel_tol\n"
504" maximum difference for being considered \"close\", relative to the\n"
505" magnitude of the input values\n"
506" abs_tol\n"
507" maximum difference for being considered \"close\", regardless of the\n"
508" magnitude of the input values\n"
509"\n"
510"Return True if a is close in value to b, and False otherwise.\n"
511"\n"
512"For the values to be considered close, the difference between them\n"
513"must be smaller than at least one of the tolerances.\n"
514"\n"
515"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
516"is, NaN is not close to anything, even itself. inf and -inf are\n"
517"only close to themselves.");
518
519#define MATH_ISCLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200520 {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200521
522static int
523math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
524 double abs_tol);
525
526static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200527math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200528{
529 PyObject *return_value = NULL;
530 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
531 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
532 double a;
533 double b;
534 double rel_tol = 1e-09;
535 double abs_tol = 0.0;
536 int _return_value;
537
538 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
539 &a, &b, &rel_tol, &abs_tol)) {
540 goto exit;
541 }
542 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
543 if ((_return_value == -1) && PyErr_Occurred()) {
544 goto exit;
545 }
546 return_value = PyBool_FromLong((long)_return_value);
547
548exit:
549 return return_value;
550}
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200551/*[clinic end generated code: output=f3264ab0ef57ba0a input=a9049054013a1b77]*/