blob: 7b23e77570335925033f53e272adc443d5e87e99 [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
Sylvain74453812017-06-10 06:51:48 +020024 if (!_PyArg_NoStackKeywords("gcd", kwnames)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020025 goto exit;
26 }
27
Sylvain74453812017-06-10 06:51:48 +020028 if (!_PyArg_UnpackStack(args, nargs, "gcd",
29 2, 2,
30 &a, &b)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +020031 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
83PyDoc_STRVAR(math_trunc__doc__,
84"trunc($module, x, /)\n"
85"--\n"
86"\n"
87"Truncates the Real x to the nearest Integral toward 0.\n"
88"\n"
89"Uses the __trunc__ magic method.");
90
91#define MATH_TRUNC_METHODDEF \
92 {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
93
94PyDoc_STRVAR(math_frexp__doc__,
95"frexp($module, x, /)\n"
96"--\n"
97"\n"
98"Return the mantissa and exponent of x, as pair (m, e).\n"
99"\n"
100"m is a float and e is an int, such that x = m * 2.**e.\n"
101"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
102
103#define MATH_FREXP_METHODDEF \
104 {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
105
106static PyObject *
107math_frexp_impl(PyObject *module, double x);
108
109static PyObject *
110math_frexp(PyObject *module, PyObject *arg)
111{
112 PyObject *return_value = NULL;
113 double x;
114
115 if (!PyArg_Parse(arg, "d:frexp", &x)) {
116 goto exit;
117 }
118 return_value = math_frexp_impl(module, x);
119
120exit:
121 return return_value;
122}
123
124PyDoc_STRVAR(math_ldexp__doc__,
125"ldexp($module, x, i, /)\n"
126"--\n"
127"\n"
128"Return x * (2**i).\n"
129"\n"
130"This is essentially the inverse of frexp().");
131
132#define MATH_LDEXP_METHODDEF \
133 {"ldexp", (PyCFunction)math_ldexp, METH_FASTCALL, math_ldexp__doc__},
134
135static PyObject *
136math_ldexp_impl(PyObject *module, double x, PyObject *i);
137
138static PyObject *
139math_ldexp(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
140{
141 PyObject *return_value = NULL;
142 double x;
143 PyObject *i;
144
Sylvain74453812017-06-10 06:51:48 +0200145 if (!_PyArg_NoStackKeywords("ldexp", kwnames)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200146 goto exit;
147 }
148
Sylvain74453812017-06-10 06:51:48 +0200149 if (!_PyArg_ParseStack(args, nargs, "dO:ldexp",
150 &x, &i)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200151 goto exit;
152 }
153 return_value = math_ldexp_impl(module, x, i);
154
155exit:
156 return return_value;
157}
158
159PyDoc_STRVAR(math_modf__doc__,
160"modf($module, x, /)\n"
161"--\n"
162"\n"
163"Return the fractional and integer parts of x.\n"
164"\n"
165"Both results carry the sign of x and are floats.");
166
167#define MATH_MODF_METHODDEF \
168 {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
169
170static PyObject *
171math_modf_impl(PyObject *module, double x);
172
173static PyObject *
174math_modf(PyObject *module, PyObject *arg)
175{
176 PyObject *return_value = NULL;
177 double x;
178
179 if (!PyArg_Parse(arg, "d:modf", &x)) {
180 goto exit;
181 }
182 return_value = math_modf_impl(module, x);
183
184exit:
185 return return_value;
186}
187
188PyDoc_STRVAR(math_log__doc__,
189"log(x, [base=math.e])\n"
190"Return the logarithm of x to the given base.\n"
191"\n"
192"If the base not specified, returns the natural logarithm (base e) of x.");
193
194#define MATH_LOG_METHODDEF \
195 {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__},
196
197static PyObject *
198math_log_impl(PyObject *module, PyObject *x, int group_right_1,
199 PyObject *base);
200
201static PyObject *
202math_log(PyObject *module, PyObject *args)
203{
204 PyObject *return_value = NULL;
205 PyObject *x;
206 int group_right_1 = 0;
207 PyObject *base = NULL;
208
209 switch (PyTuple_GET_SIZE(args)) {
210 case 1:
211 if (!PyArg_ParseTuple(args, "O:log", &x)) {
212 goto exit;
213 }
214 break;
215 case 2:
216 if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) {
217 goto exit;
218 }
219 group_right_1 = 1;
220 break;
221 default:
222 PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments");
223 goto exit;
224 }
225 return_value = math_log_impl(module, x, group_right_1, base);
226
227exit:
228 return return_value;
229}
230
231PyDoc_STRVAR(math_log2__doc__,
232"log2($module, x, /)\n"
233"--\n"
234"\n"
235"Return the base 2 logarithm of x.");
236
237#define MATH_LOG2_METHODDEF \
238 {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
239
240PyDoc_STRVAR(math_log10__doc__,
241"log10($module, x, /)\n"
242"--\n"
243"\n"
244"Return the base 10 logarithm of x.");
245
246#define MATH_LOG10_METHODDEF \
247 {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
248
249PyDoc_STRVAR(math_fmod__doc__,
250"fmod($module, x, y, /)\n"
251"--\n"
252"\n"
253"Return fmod(x, y), according to platform C.\n"
254"\n"
255"x % y may differ.");
256
257#define MATH_FMOD_METHODDEF \
258 {"fmod", (PyCFunction)math_fmod, METH_FASTCALL, math_fmod__doc__},
259
260static PyObject *
261math_fmod_impl(PyObject *module, double x, double y);
262
263static PyObject *
264math_fmod(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
265{
266 PyObject *return_value = NULL;
267 double x;
268 double y;
269
Sylvain74453812017-06-10 06:51:48 +0200270 if (!_PyArg_NoStackKeywords("fmod", kwnames)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200271 goto exit;
272 }
273
Sylvain74453812017-06-10 06:51:48 +0200274 if (!_PyArg_ParseStack(args, nargs, "dd:fmod",
275 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200276 goto exit;
277 }
278 return_value = math_fmod_impl(module, x, y);
279
280exit:
281 return return_value;
282}
283
284PyDoc_STRVAR(math_hypot__doc__,
285"hypot($module, x, y, /)\n"
286"--\n"
287"\n"
288"Return the Euclidean distance, sqrt(x*x + y*y).");
289
290#define MATH_HYPOT_METHODDEF \
291 {"hypot", (PyCFunction)math_hypot, METH_FASTCALL, math_hypot__doc__},
292
293static PyObject *
294math_hypot_impl(PyObject *module, double x, double y);
295
296static PyObject *
297math_hypot(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
298{
299 PyObject *return_value = NULL;
300 double x;
301 double y;
302
Sylvain74453812017-06-10 06:51:48 +0200303 if (!_PyArg_NoStackKeywords("hypot", kwnames)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200304 goto exit;
305 }
306
Sylvain74453812017-06-10 06:51:48 +0200307 if (!_PyArg_ParseStack(args, nargs, "dd:hypot",
308 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200309 goto exit;
310 }
311 return_value = math_hypot_impl(module, x, y);
312
313exit:
314 return return_value;
315}
316
317PyDoc_STRVAR(math_pow__doc__,
318"pow($module, x, y, /)\n"
319"--\n"
320"\n"
321"Return x**y (x to the power of y).");
322
323#define MATH_POW_METHODDEF \
324 {"pow", (PyCFunction)math_pow, METH_FASTCALL, math_pow__doc__},
325
326static PyObject *
327math_pow_impl(PyObject *module, double x, double y);
328
329static PyObject *
330math_pow(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
331{
332 PyObject *return_value = NULL;
333 double x;
334 double y;
335
Sylvain74453812017-06-10 06:51:48 +0200336 if (!_PyArg_NoStackKeywords("pow", kwnames)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200337 goto exit;
338 }
339
Sylvain74453812017-06-10 06:51:48 +0200340 if (!_PyArg_ParseStack(args, nargs, "dd:pow",
341 &x, &y)) {
Serhiy Storchakac9ea9332017-01-19 18:13:09 +0200342 goto exit;
343 }
344 return_value = math_pow_impl(module, x, y);
345
346exit:
347 return return_value;
348}
349
350PyDoc_STRVAR(math_degrees__doc__,
351"degrees($module, x, /)\n"
352"--\n"
353"\n"
354"Convert angle x from radians to degrees.");
355
356#define MATH_DEGREES_METHODDEF \
357 {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
358
359static PyObject *
360math_degrees_impl(PyObject *module, double x);
361
362static PyObject *
363math_degrees(PyObject *module, PyObject *arg)
364{
365 PyObject *return_value = NULL;
366 double x;
367
368 if (!PyArg_Parse(arg, "d:degrees", &x)) {
369 goto exit;
370 }
371 return_value = math_degrees_impl(module, x);
372
373exit:
374 return return_value;
375}
376
377PyDoc_STRVAR(math_radians__doc__,
378"radians($module, x, /)\n"
379"--\n"
380"\n"
381"Convert angle x from degrees to radians.");
382
383#define MATH_RADIANS_METHODDEF \
384 {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
385
386static PyObject *
387math_radians_impl(PyObject *module, double x);
388
389static PyObject *
390math_radians(PyObject *module, PyObject *arg)
391{
392 PyObject *return_value = NULL;
393 double x;
394
395 if (!PyArg_Parse(arg, "d:radians", &x)) {
396 goto exit;
397 }
398 return_value = math_radians_impl(module, x);
399
400exit:
401 return return_value;
402}
403
404PyDoc_STRVAR(math_isfinite__doc__,
405"isfinite($module, x, /)\n"
406"--\n"
407"\n"
408"Return True if x is neither an infinity nor a NaN, and False otherwise.");
409
410#define MATH_ISFINITE_METHODDEF \
411 {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
412
413static PyObject *
414math_isfinite_impl(PyObject *module, double x);
415
416static PyObject *
417math_isfinite(PyObject *module, PyObject *arg)
418{
419 PyObject *return_value = NULL;
420 double x;
421
422 if (!PyArg_Parse(arg, "d:isfinite", &x)) {
423 goto exit;
424 }
425 return_value = math_isfinite_impl(module, x);
426
427exit:
428 return return_value;
429}
430
431PyDoc_STRVAR(math_isnan__doc__,
432"isnan($module, x, /)\n"
433"--\n"
434"\n"
435"Return True if x is a NaN (not a number), and False otherwise.");
436
437#define MATH_ISNAN_METHODDEF \
438 {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
439
440static PyObject *
441math_isnan_impl(PyObject *module, double x);
442
443static PyObject *
444math_isnan(PyObject *module, PyObject *arg)
445{
446 PyObject *return_value = NULL;
447 double x;
448
449 if (!PyArg_Parse(arg, "d:isnan", &x)) {
450 goto exit;
451 }
452 return_value = math_isnan_impl(module, x);
453
454exit:
455 return return_value;
456}
457
458PyDoc_STRVAR(math_isinf__doc__,
459"isinf($module, x, /)\n"
460"--\n"
461"\n"
462"Return True if x is a positive or negative infinity, and False otherwise.");
463
464#define MATH_ISINF_METHODDEF \
465 {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
466
467static PyObject *
468math_isinf_impl(PyObject *module, double x);
469
470static PyObject *
471math_isinf(PyObject *module, PyObject *arg)
472{
473 PyObject *return_value = NULL;
474 double x;
475
476 if (!PyArg_Parse(arg, "d:isinf", &x)) {
477 goto exit;
478 }
479 return_value = math_isinf_impl(module, x);
480
481exit:
482 return return_value;
483}
484
485PyDoc_STRVAR(math_isclose__doc__,
486"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
487"--\n"
488"\n"
489"Determine whether two floating point numbers are close in value.\n"
490"\n"
491" rel_tol\n"
492" maximum difference for being considered \"close\", relative to the\n"
493" magnitude of the input values\n"
494" abs_tol\n"
495" maximum difference for being considered \"close\", regardless of the\n"
496" magnitude of the input values\n"
497"\n"
498"Return True if a is close in value to b, and False otherwise.\n"
499"\n"
500"For the values to be considered close, the difference between them\n"
501"must be smaller than at least one of the tolerances.\n"
502"\n"
503"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
504"is, NaN is not close to anything, even itself. inf and -inf are\n"
505"only close to themselves.");
506
507#define MATH_ISCLOSE_METHODDEF \
508 {"isclose", (PyCFunction)math_isclose, METH_FASTCALL, math_isclose__doc__},
509
510static int
511math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
512 double abs_tol);
513
514static PyObject *
515math_isclose(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
516{
517 PyObject *return_value = NULL;
518 static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
519 static _PyArg_Parser _parser = {"dd|$dd:isclose", _keywords, 0};
520 double a;
521 double b;
522 double rel_tol = 1e-09;
523 double abs_tol = 0.0;
524 int _return_value;
525
526 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
527 &a, &b, &rel_tol, &abs_tol)) {
528 goto exit;
529 }
530 _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
531 if ((_return_value == -1) && PyErr_Occurred()) {
532 goto exit;
533 }
534 return_value = PyBool_FromLong((long)_return_value);
535
536exit:
537 return return_value;
538}
Sylvain74453812017-06-10 06:51:48 +0200539/*[clinic end generated code: output=c9f1ac6ded547cc8 input=a9049054013a1b77]*/