blob: e436f5900c6bb1efe31a3e3a781c2361d922a1be [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
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
Sylvain74453812017-06-10 06:51:48 +0200142 if (!_PyArg_ParseStack(args, nargs, "dO:ldexp",
143 &x, &i)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200144 goto exit;
145 }
146 return_value = math_ldexp_impl(module, x, i);
147
148exit:
149 return return_value;
150}
151
152PyDoc_STRVAR(math_modf__doc__,
153"modf($module, x, /)\n"
154"--\n"
155"\n"
156"Return the fractional and integer parts of x.\n"
157"\n"
158"Both results carry the sign of x and are floats.");
159
160#define MATH_MODF_METHODDEF \
161 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
162
163static PyObject *
164math_modf_impl(PyObject *module, double x);
165
166static PyObject *
167math_modf(PyObject *module, PyObject *arg)
168{
169 PyObject *return_value = NULL;
170 double x;
171
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200172 x = PyFloat_AsDouble(arg);
173 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200174 goto exit;
175 }
176 return_value = math_modf_impl(module, x);
177
178exit:
179 return return_value;
180}
181
182PyDoc_STRVAR(math_log__doc__,
183"log(x, [base=math.e])\n"
184"Return the logarithm of x to the given base.\n"
185"\n"
186"If the base not specified, returns the natural logarithm (base e) of x.");
187
188#define MATH_LOG_METHODDEF \
189 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
190
191static PyObject *
192math_log_impl(PyObject *module, PyObject *x, int group_right_1,
193 PyObject *base);
194
195static PyObject *
196math_log(PyObject *module, PyObject *args)
197{
198 PyObject *return_value = NULL;
199 PyObject *x;
200 int group_right_1 = 0;
201 PyObject *base = NULL;
202
203 switch (PyTuple_GET_SIZE(args)) {
204 case 1:
205 if (!PyArg_ParseTuple(args, "O:log", &x)) {
206 goto exit;
207 }
208 break;
209 case 2:
210 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
211 goto exit;
212 }
213 group_right_1 = 1;
214 break;
215 default:
216 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
217 goto exit;
218 }
219 return_value = math_log_impl(module, x, group_right_1, base);
220
221exit:
222 return return_value;
223}
224
225PyDoc_STRVAR(math_log2__doc__,
226"log2($module, x, /)\n"
227"--\n"
228"\n"
229"Return the base 2 logarithm of x.");
230
231#define MATH_LOG2_METHODDEF \
232 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
233
234PyDoc_STRVAR(math_log10__doc__,
235"log10($module, x, /)\n"
236"--\n"
237"\n"
238"Return the base 10 logarithm of x.");
239
240#define MATH_LOG10_METHODDEF \
241 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
242
243PyDoc_STRVAR(math_fmod__doc__,
244"fmod($module, x, y, /)\n"
245"--\n"
246"\n"
247"Return fmod(x, y), according to platform C.\n"
248"\n"
249"x % y may differ.");
250
251#define MATH_FMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200252 {"fmod", (PyCFunction)(void(*)(void))math_fmod, METH_FASTCALL, math_fmod__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200253
254static PyObject *
255math_fmod_impl(PyObject *module, double x, double y);
256
257static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200258math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200259{
260 PyObject *return_value = NULL;
261 double x;
262 double y;
263
Sylvain74453812017-06-10 06:51:48 +0200264 if (!_PyArg_ParseStack(args, nargs, "dd:fmod",
265 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200266 goto exit;
267 }
268 return_value = math_fmod_impl(module, x, y);
269
270exit:
271 return return_value;
272}
273
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700274PyDoc_STRVAR(math_dist__doc__,
275"dist($module, p, q, /)\n"
276"--\n"
277"\n"
278"Return the Euclidean distance between two points p and q.\n"
279"\n"
280"The points should be specified as tuples of coordinates.\n"
281"Both tuples must be the same size.\n"
282"\n"
283"Roughly equivalent to:\n"
284" sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
285
286#define MATH_DIST_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200287 {"dist", (PyCFunction)(void(*)(void))math_dist, METH_FASTCALL, math_dist__doc__},
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700288
289static PyObject *
290math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
291
292static PyObject *
293math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
294{
295 PyObject *return_value = NULL;
296 PyObject *p;
297 PyObject *q;
298
Raymond Hettingerdf810152018-09-29 14:30:38 -0700299 if (!_PyArg_UnpackStack(args, nargs, "dist",
300 2, 2,
301 &p, &q)) {
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700302 goto exit;
303 }
304 return_value = math_dist_impl(module, p, q);
305
306exit:
307 return return_value;
308}
309
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200310PyDoc_STRVAR(math_pow__doc__,
311"pow($module, x, y, /)\n"
312"--\n"
313"\n"
314"Return x**y (x to the power of y).");
315
316#define MATH_POW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200317 {"pow", (PyCFunction)(void(*)(void))math_pow, METH_FASTCALL, math_pow__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200318
319static PyObject *
320math_pow_impl(PyObject *module, double x, double y);
321
322static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200323math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200324{
325 PyObject *return_value = NULL;
326 double x;
327 double y;
328
Sylvain74453812017-06-10 06:51:48 +0200329 if (!_PyArg_ParseStack(args, nargs, "dd:pow",
330 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200331 goto exit;
332 }
333 return_value = math_pow_impl(module, x, y);
334
335exit:
336 return return_value;
337}
338
339PyDoc_STRVAR(math_degrees__doc__,
340"degrees($module, x, /)\n"
341"--\n"
342"\n"
343"Convert angle x from radians to degrees.");
344
345#define MATH_DEGREES_METHODDEF \
346 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
347
348static PyObject *
349math_degrees_impl(PyObject *module, double x);
350
351static PyObject *
352math_degrees(PyObject *module, PyObject *arg)
353{
354 PyObject *return_value = NULL;
355 double x;
356
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200357 x = PyFloat_AsDouble(arg);
358 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200359 goto exit;
360 }
361 return_value = math_degrees_impl(module, x);
362
363exit:
364 return return_value;
365}
366
367PyDoc_STRVAR(math_radians__doc__,
368"radians($module, x, /)\n"
369"--\n"
370"\n"
371"Convert angle x from degrees to radians.");
372
373#define MATH_RADIANS_METHODDEF \
374 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
375
376static PyObject *
377math_radians_impl(PyObject *module, double x);
378
379static PyObject *
380math_radians(PyObject *module, PyObject *arg)
381{
382 PyObject *return_value = NULL;
383 double x;
384
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200385 x = PyFloat_AsDouble(arg);
386 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200387 goto exit;
388 }
389 return_value = math_radians_impl(module, x);
390
391exit:
392 return return_value;
393}
394
395PyDoc_STRVAR(math_isfinite__doc__,
396"isfinite($module, x, /)\n"
397"--\n"
398"\n"
399"Return True if x is neither an infinity nor a NaN, and False otherwise.");
400
401#define MATH_ISFINITE_METHODDEF \
402 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
403
404static PyObject *
405math_isfinite_impl(PyObject *module, double x);
406
407static PyObject *
408math_isfinite(PyObject *module, PyObject *arg)
409{
410 PyObject *return_value = NULL;
411 double x;
412
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200413 x = PyFloat_AsDouble(arg);
414 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200415 goto exit;
416 }
417 return_value = math_isfinite_impl(module, x);
418
419exit:
420 return return_value;
421}
422
423PyDoc_STRVAR(math_isnan__doc__,
424"isnan($module, x, /)\n"
425"--\n"
426"\n"
427"Return True if x is a NaN (not a number), and False otherwise.");
428
429#define MATH_ISNAN_METHODDEF \
430 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
431
432static PyObject *
433math_isnan_impl(PyObject *module, double x);
434
435static PyObject *
436math_isnan(PyObject *module, PyObject *arg)
437{
438 PyObject *return_value = NULL;
439 double x;
440
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200441 x = PyFloat_AsDouble(arg);
442 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200443 goto exit;
444 }
445 return_value = math_isnan_impl(module, x);
446
447exit:
448 return return_value;
449}
450
451PyDoc_STRVAR(math_isinf__doc__,
452"isinf($module, x, /)\n"
453"--\n"
454"\n"
455"Return True if x is a positive or negative infinity, and False otherwise.");
456
457#define MATH_ISINF_METHODDEF \
458 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
459
460static PyObject *
461math_isinf_impl(PyObject *module, double x);
462
463static PyObject *
464math_isinf(PyObject *module, PyObject *arg)
465{
466 PyObject *return_value = NULL;
467 double x;
468
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200469 x = PyFloat_AsDouble(arg);
470 if (PyErr_Occurred()) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200471 goto exit;
472 }
473 return_value = math_isinf_impl(module, x);
474
475exit:
476 return return_value;
477}
478
479PyDoc_STRVAR(math_isclose__doc__,
480"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
481"--\n"
482"\n"
483"Determine whether two floating point numbers are close in value.\n"
484"\n"
485" rel_tol\n"
486" maximum difference for being considered \"close\", relative to the\n"
487" magnitude of the input values\n"
488" abs_tol\n"
489" maximum difference for being considered \"close\", regardless of the\n"
490" magnitude of the input values\n"
491"\n"
492"Return True if a is close in value to b, and False otherwise.\n"
493"\n"
494"For the values to be considered close, the difference between them\n"
495"must be smaller than at least one of the tolerances.\n"
496"\n"
497"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
498"is, NaN is not close to anything, even itself. inf and -inf are\n"
499"only close to themselves.");
500
501#define MATH_ISCLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200502 {"isclose", (PyCFunction)(void(*)(void))math_isclose, METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200503
504static int
505math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
506 double abs_tol);
507
508static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200509math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200510{
511 PyObject *return_value = NULL;
512 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
513 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
514 double a;
515 double b;
516 double rel_tol = 1e-09;
517 double abs_tol = 0.0;
518 int _return_value;
519
520 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
521 &a, &b, &rel_tol, &abs_tol)) {
522 goto exit;
523 }
524 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
525 if ((_return_value == -1) && PyErr_Occurred()) {
526 goto exit;
527 }
528 return_value = PyBool_FromLong((long)_return_value);
529
530exit:
531 return return_value;
532}
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200533/*[clinic end generated code: output=da4b9940a5cb0188 input=a9049054013a1b77]*/