blob: 4e9fe20cf544daaf7bd758e3a38adb6aefaa126d [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 \
12 {"gcd", (PyCFunction)math_gcd, METH_FASTCALL, math_gcd__doc__},
13
14static PyObject *
15math_gcd_impl(PyObject *module, PyObject *a, PyObject *b);
16
17static PyObject *
18math_gcd(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
19{
20 PyObject *return_value = NULL;
21 PyObject *a;
22 PyObject *b;
23
24 if (!_PyArg_UnpackStack(args, nargs, "gcd",
25 2, 2,
26 &a, &b)) {
27 goto exit;
28 }
29
30 if (!_PyArg_NoStackKeywords("gcd", kwnames)) {
31 goto exit;
32 }
33 return_value = math_gcd_impl(module, a, b);
34
35exit:
36 return return_value;
37}
38
39PyDoc_STRVAR(math_ceil__doc__,
40"ceil($module, x, /)\n"
41"--\n"
42"\n"
43"Return the ceiling of x as an Integral.\n"
44"\n"
45"This is the smallest integer >= x.");
46
47#define MATH_CEIL_METHODDEF \
48 {"ceil", (PyCFunction)math_ceil, METH_O, math_ceil__doc__},
49
50PyDoc_STRVAR(math_floor__doc__,
51"floor($module, x, /)\n"
52"--\n"
53"\n"
54"Return the floor of x as an Integral.\n"
55"\n"
56"This is the largest integer <= x.");
57
58#define MATH_FLOOR_METHODDEF \
59 {"floor", (PyCFunction)math_floor, METH_O, math_floor__doc__},
60
61PyDoc_STRVAR(math_fsum__doc__,
62"fsum($module, seq, /)\n"
63"--\n"
64"\n"
65"Return an accurate floating point sum of values in the iterable seq.\n"
66"\n"
67"Assumes IEEE-754 floating point arithmetic.");
68
69#define MATH_FSUM_METHODDEF \
70 {"fsum", (PyCFunction)math_fsum, METH_O, math_fsum__doc__},
71
72PyDoc_STRVAR(math_factorial__doc__,
73"factorial($module, x, /)\n"
74"--\n"
75"\n"
76"Find x!.\n"
77"\n"
78"Raise a ValueError if x is negative or non-integral.");
79
80#define MATH_FACTORIAL_METHODDEF \
81 {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__},
82
Mark Dickinsond1b230e2017-01-21 12:35:30 +000083PyDoc_STRVAR(math_fma__doc__,
84"fma($module, x, y, z, /)\n"
85"--\n"
86"\n"
87"Fused multiply-add operation. Compute (x * y) + z with a single round.");
88
89#define MATH_FMA_METHODDEF \
90 {"fma", (PyCFunction)math_fma, METH_FASTCALL, math_fma__doc__},
91
92static PyObject *
93math_fma_impl(PyObject *module, double x, double y, double z);
94
95static PyObject *
96math_fma(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
97{
98 PyObject *return_value = NULL;
99 double x;
100 double y;
101 double z;
102
103 if (!_PyArg_ParseStack(args, nargs, "ddd:fma",
104 &x, &y, &z)) {
105 goto exit;
106 }
107
108 if (!_PyArg_NoStackKeywords("fma", kwnames)) {
109 goto exit;
110 }
111 return_value = math_fma_impl(module, x, y, z);
112
113exit:
114 return return_value;
115}
116
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200117PyDoc_STRVAR(math_trunc__doc__,
118"trunc($module, x, /)\n"
119"--\n"
120"\n"
121"Truncates the Real x to the nearest Integral toward 0.\n"
122"\n"
123"Uses the __trunc__ magic method.");
124
125#define MATH_TRUNC_METHODDEF \
126 {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
127
128PyDoc_STRVAR(math_frexp__doc__,
129"frexp($module, x, /)\n"
130"--\n"
131"\n"
132"Return the mantissa and exponent of x, as pair (m, e).\n"
133"\n"
134"m is a float and e is an int, such that x = m * 2.**e.\n"
135"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
136
137#define MATH_FREXP_METHODDEF \
138 {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
139
140static PyObject *
141math_frexp_impl(PyObject *module, double x);
142
143static PyObject *
144math_frexp(PyObject *module, PyObject *arg)
145{
146 PyObject *return_value = NULL;
147 double x;
148
149 if (!PyArg_Parse(arg, "d:frexp", &x)) {
150 goto exit;
151 }
152 return_value = math_frexp_impl(module, x);
153
154exit:
155 return return_value;
156}
157
158PyDoc_STRVAR(math_ldexp__doc__,
159"ldexp($module, x, i, /)\n"
160"--\n"
161"\n"
162"Return x * (2**i).\n"
163"\n"
164"This is essentially the inverse of frexp().");
165
166#define MATH_LDEXP_METHODDEF \
167 {"ldexp", (PyCFunction)math_ldexp, METH_FASTCALL, math_ldexp__doc__},
168
169static PyObject *
170math_ldexp_impl(PyObject *module, double x, PyObject *i);
171
172static PyObject *
173math_ldexp(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
174{
175 PyObject *return_value = NULL;
176 double x;
177 PyObject *i;
178
179 if (!_PyArg_ParseStack(args, nargs, "dO:ldexp",
180 &x, &i)) {
181 goto exit;
182 }
183
184 if (!_PyArg_NoStackKeywords("ldexp", kwnames)) {
185 goto exit;
186 }
187 return_value = math_ldexp_impl(module, x, i);
188
189exit:
190 return return_value;
191}
192
193PyDoc_STRVAR(math_modf__doc__,
194"modf($module, x, /)\n"
195"--\n"
196"\n"
197"Return the fractional and integer parts of x.\n"
198"\n"
199"Both results carry the sign of x and are floats.");
200
201#define MATH_MODF_METHODDEF \
202 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
203
204static PyObject *
205math_modf_impl(PyObject *module, double x);
206
207static PyObject *
208math_modf(PyObject *module, PyObject *arg)
209{
210 PyObject *return_value = NULL;
211 double x;
212
213 if (!PyArg_Parse(arg, "d:modf", &x)) {
214 goto exit;
215 }
216 return_value = math_modf_impl(module, x);
217
218exit:
219 return return_value;
220}
221
222PyDoc_STRVAR(math_log__doc__,
223"log(x, [base=math.e])\n"
224"Return the logarithm of x to the given base.\n"
225"\n"
226"If the base not specified, returns the natural logarithm (base e) of x.");
227
228#define MATH_LOG_METHODDEF \
229 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
230
231static PyObject *
232math_log_impl(PyObject *module, PyObject *x, int group_right_1,
233 PyObject *base);
234
235static PyObject *
236math_log(PyObject *module, PyObject *args)
237{
238 PyObject *return_value = NULL;
239 PyObject *x;
240 int group_right_1 = 0;
241 PyObject *base = NULL;
242
243 switch (PyTuple_GET_SIZE(args)) {
244 case 1:
245 if (!PyArg_ParseTuple(args, "O:log", &x)) {
246 goto exit;
247 }
248 break;
249 case 2:
250 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
251 goto exit;
252 }
253 group_right_1 = 1;
254 break;
255 default:
256 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
257 goto exit;
258 }
259 return_value = math_log_impl(module, x, group_right_1, base);
260
261exit:
262 return return_value;
263}
264
265PyDoc_STRVAR(math_log2__doc__,
266"log2($module, x, /)\n"
267"--\n"
268"\n"
269"Return the base 2 logarithm of x.");
270
271#define MATH_LOG2_METHODDEF \
272 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
273
274PyDoc_STRVAR(math_log10__doc__,
275"log10($module, x, /)\n"
276"--\n"
277"\n"
278"Return the base 10 logarithm of x.");
279
280#define MATH_LOG10_METHODDEF \
281 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
282
283PyDoc_STRVAR(math_fmod__doc__,
284"fmod($module, x, y, /)\n"
285"--\n"
286"\n"
287"Return fmod(x, y), according to platform C.\n"
288"\n"
289"x % y may differ.");
290
291#define MATH_FMOD_METHODDEF \
292 {"fmod", (PyCFunction)math_fmod, METH_FASTCALL, math_fmod__doc__},
293
294static PyObject *
295math_fmod_impl(PyObject *module, double x, double y);
296
297static PyObject *
298math_fmod(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
299{
300 PyObject *return_value = NULL;
301 double x;
302 double y;
303
304 if (!_PyArg_ParseStack(args, nargs, "dd:fmod",
305 &x, &y)) {
306 goto exit;
307 }
308
309 if (!_PyArg_NoStackKeywords("fmod", kwnames)) {
310 goto exit;
311 }
312 return_value = math_fmod_impl(module, x, y);
313
314exit:
315 return return_value;
316}
317
318PyDoc_STRVAR(math_hypot__doc__,
319"hypot($module, x, y, /)\n"
320"--\n"
321"\n"
322"Return the Euclidean distance, sqrt(x*x + y*y).");
323
324#define MATH_HYPOT_METHODDEF \
325 {"hypot", (PyCFunction)math_hypot, METH_FASTCALL, math_hypot__doc__},
326
327static PyObject *
328math_hypot_impl(PyObject *module, double x, double y);
329
330static PyObject *
331math_hypot(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
332{
333 PyObject *return_value = NULL;
334 double x;
335 double y;
336
337 if (!_PyArg_ParseStack(args, nargs, "dd:hypot",
338 &x, &y)) {
339 goto exit;
340 }
341
342 if (!_PyArg_NoStackKeywords("hypot", kwnames)) {
343 goto exit;
344 }
345 return_value = math_hypot_impl(module, x, y);
346
347exit:
348 return return_value;
349}
350
351PyDoc_STRVAR(math_pow__doc__,
352"pow($module, x, y, /)\n"
353"--\n"
354"\n"
355"Return x**y (x to the power of y).");
356
357#define MATH_POW_METHODDEF \
358 {"pow", (PyCFunction)math_pow, METH_FASTCALL, math_pow__doc__},
359
360static PyObject *
361math_pow_impl(PyObject *module, double x, double y);
362
363static PyObject *
364math_pow(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
365{
366 PyObject *return_value = NULL;
367 double x;
368 double y;
369
370 if (!_PyArg_ParseStack(args, nargs, "dd:pow",
371 &x, &y)) {
372 goto exit;
373 }
374
375 if (!_PyArg_NoStackKeywords("pow", kwnames)) {
376 goto exit;
377 }
378 return_value = math_pow_impl(module, x, y);
379
380exit:
381 return return_value;
382}
383
384PyDoc_STRVAR(math_degrees__doc__,
385"degrees($module, x, /)\n"
386"--\n"
387"\n"
388"Convert angle x from radians to degrees.");
389
390#define MATH_DEGREES_METHODDEF \
391 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
392
393static PyObject *
394math_degrees_impl(PyObject *module, double x);
395
396static PyObject *
397math_degrees(PyObject *module, PyObject *arg)
398{
399 PyObject *return_value = NULL;
400 double x;
401
402 if (!PyArg_Parse(arg, "d:degrees", &x)) {
403 goto exit;
404 }
405 return_value = math_degrees_impl(module, x);
406
407exit:
408 return return_value;
409}
410
411PyDoc_STRVAR(math_radians__doc__,
412"radians($module, x, /)\n"
413"--\n"
414"\n"
415"Convert angle x from degrees to radians.");
416
417#define MATH_RADIANS_METHODDEF \
418 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
419
420static PyObject *
421math_radians_impl(PyObject *module, double x);
422
423static PyObject *
424math_radians(PyObject *module, PyObject *arg)
425{
426 PyObject *return_value = NULL;
427 double x;
428
429 if (!PyArg_Parse(arg, "d:radians", &x)) {
430 goto exit;
431 }
432 return_value = math_radians_impl(module, x);
433
434exit:
435 return return_value;
436}
437
438PyDoc_STRVAR(math_isfinite__doc__,
439"isfinite($module, x, /)\n"
440"--\n"
441"\n"
442"Return True if x is neither an infinity nor a NaN, and False otherwise.");
443
444#define MATH_ISFINITE_METHODDEF \
445 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
446
447static PyObject *
448math_isfinite_impl(PyObject *module, double x);
449
450static PyObject *
451math_isfinite(PyObject *module, PyObject *arg)
452{
453 PyObject *return_value = NULL;
454 double x;
455
456 if (!PyArg_Parse(arg, "d:isfinite", &x)) {
457 goto exit;
458 }
459 return_value = math_isfinite_impl(module, x);
460
461exit:
462 return return_value;
463}
464
465PyDoc_STRVAR(math_isnan__doc__,
466"isnan($module, x, /)\n"
467"--\n"
468"\n"
469"Return True if x is a NaN (not a number), and False otherwise.");
470
471#define MATH_ISNAN_METHODDEF \
472 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
473
474static PyObject *
475math_isnan_impl(PyObject *module, double x);
476
477static PyObject *
478math_isnan(PyObject *module, PyObject *arg)
479{
480 PyObject *return_value = NULL;
481 double x;
482
483 if (!PyArg_Parse(arg, "d:isnan", &x)) {
484 goto exit;
485 }
486 return_value = math_isnan_impl(module, x);
487
488exit:
489 return return_value;
490}
491
492PyDoc_STRVAR(math_isinf__doc__,
493"isinf($module, x, /)\n"
494"--\n"
495"\n"
496"Return True if x is a positive or negative infinity, and False otherwise.");
497
498#define MATH_ISINF_METHODDEF \
499 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
500
501static PyObject *
502math_isinf_impl(PyObject *module, double x);
503
504static PyObject *
505math_isinf(PyObject *module, PyObject *arg)
506{
507 PyObject *return_value = NULL;
508 double x;
509
510 if (!PyArg_Parse(arg, "d:isinf", &x)) {
511 goto exit;
512 }
513 return_value = math_isinf_impl(module, x);
514
515exit:
516 return return_value;
517}
518
519PyDoc_STRVAR(math_isclose__doc__,
520"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
521"--\n"
522"\n"
523"Determine whether two floating point numbers are close in value.\n"
524"\n"
525" rel_tol\n"
526" maximum difference for being considered \"close\", relative to the\n"
527" magnitude of the input values\n"
528" abs_tol\n"
529" maximum difference for being considered \"close\", regardless of the\n"
530" magnitude of the input values\n"
531"\n"
532"Return True if a is close in value to b, and False otherwise.\n"
533"\n"
534"For the values to be considered close, the difference between them\n"
535"must be smaller than at least one of the tolerances.\n"
536"\n"
537"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
538"is, NaN is not close to anything, even itself. inf and -inf are\n"
539"only close to themselves.");
540
541#define MATH_ISCLOSE_METHODDEF \
542 {"isclose", (PyCFunction)math_isclose, METH_FASTCALL, math_isclose__doc__},
543
544static int
545math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
546 double abs_tol);
547
548static PyObject *
549math_isclose(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
550{
551 PyObject *return_value = NULL;
552 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
553 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
554 double a;
555 double b;
556 double rel_tol = 1e-09;
557 double abs_tol = 0.0;
558 int _return_value;
559
560 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
561 &a, &b, &rel_tol, &abs_tol)) {
562 goto exit;
563 }
564 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
565 if ((_return_value == -1) && PyErr_Occurred()) {
566 goto exit;
567 }
568 return_value = PyBool_FromLong((long)_return_value);
569
570exit:
571 return return_value;
572}
Mark Dickinsond1b230e2017-01-21 12:35:30 +0000573/*[clinic end generated code: output=f428e1075d00c334 input=a9049054013a1b77]*/