mathmodule.c.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(math_ceil__doc__,
  9. "ceil($module, x, /)\n"
  10. "--\n"
  11. "\n"
  12. "Return the ceiling of x as an Integral.\n"
  13. "\n"
  14. "This is the smallest integer >= x.");
  15. #define MATH_CEIL_METHODDEF \
  16. {"ceil", (PyCFunction)math_ceil, METH_O, math_ceil__doc__},
  17. PyDoc_STRVAR(math_floor__doc__,
  18. "floor($module, x, /)\n"
  19. "--\n"
  20. "\n"
  21. "Return the floor of x as an Integral.\n"
  22. "\n"
  23. "This is the largest integer <= x.");
  24. #define MATH_FLOOR_METHODDEF \
  25. {"floor", (PyCFunction)math_floor, METH_O, math_floor__doc__},
  26. PyDoc_STRVAR(math_fsum__doc__,
  27. "fsum($module, seq, /)\n"
  28. "--\n"
  29. "\n"
  30. "Return an accurate floating-point sum of values in the iterable seq.\n"
  31. "\n"
  32. "Assumes IEEE-754 floating-point arithmetic.");
  33. #define MATH_FSUM_METHODDEF \
  34. {"fsum", (PyCFunction)math_fsum, METH_O, math_fsum__doc__},
  35. PyDoc_STRVAR(math_isqrt__doc__,
  36. "isqrt($module, n, /)\n"
  37. "--\n"
  38. "\n"
  39. "Return the integer part of the square root of the input.");
  40. #define MATH_ISQRT_METHODDEF \
  41. {"isqrt", (PyCFunction)math_isqrt, METH_O, math_isqrt__doc__},
  42. PyDoc_STRVAR(math_factorial__doc__,
  43. "factorial($module, n, /)\n"
  44. "--\n"
  45. "\n"
  46. "Find n!.\n"
  47. "\n"
  48. "Raise a ValueError if x is negative or non-integral.");
  49. #define MATH_FACTORIAL_METHODDEF \
  50. {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__},
  51. PyDoc_STRVAR(math_trunc__doc__,
  52. "trunc($module, x, /)\n"
  53. "--\n"
  54. "\n"
  55. "Truncates the Real x to the nearest Integral toward 0.\n"
  56. "\n"
  57. "Uses the __trunc__ magic method.");
  58. #define MATH_TRUNC_METHODDEF \
  59. {"trunc", (PyCFunction)math_trunc, METH_O, math_trunc__doc__},
  60. PyDoc_STRVAR(math_frexp__doc__,
  61. "frexp($module, x, /)\n"
  62. "--\n"
  63. "\n"
  64. "Return the mantissa and exponent of x, as pair (m, e).\n"
  65. "\n"
  66. "m is a float and e is an int, such that x = m * 2.**e.\n"
  67. "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
  68. #define MATH_FREXP_METHODDEF \
  69. {"frexp", (PyCFunction)math_frexp, METH_O, math_frexp__doc__},
  70. static PyObject *
  71. math_frexp_impl(PyObject *module, double x);
  72. static PyObject *
  73. math_frexp(PyObject *module, PyObject *arg)
  74. {
  75. PyObject *return_value = NULL;
  76. double x;
  77. if (PyFloat_CheckExact(arg)) {
  78. x = PyFloat_AS_DOUBLE(arg);
  79. }
  80. else
  81. {
  82. x = PyFloat_AsDouble(arg);
  83. if (x == -1.0 && PyErr_Occurred()) {
  84. goto exit;
  85. }
  86. }
  87. return_value = math_frexp_impl(module, x);
  88. exit:
  89. return return_value;
  90. }
  91. PyDoc_STRVAR(math_ldexp__doc__,
  92. "ldexp($module, x, i, /)\n"
  93. "--\n"
  94. "\n"
  95. "Return x * (2**i).\n"
  96. "\n"
  97. "This is essentially the inverse of frexp().");
  98. #define MATH_LDEXP_METHODDEF \
  99. {"ldexp", _PyCFunction_CAST(math_ldexp), METH_FASTCALL, math_ldexp__doc__},
  100. static PyObject *
  101. math_ldexp_impl(PyObject *module, double x, PyObject *i);
  102. static PyObject *
  103. math_ldexp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  104. {
  105. PyObject *return_value = NULL;
  106. double x;
  107. PyObject *i;
  108. if (!_PyArg_CheckPositional("ldexp", nargs, 2, 2)) {
  109. goto exit;
  110. }
  111. if (PyFloat_CheckExact(args[0])) {
  112. x = PyFloat_AS_DOUBLE(args[0]);
  113. }
  114. else
  115. {
  116. x = PyFloat_AsDouble(args[0]);
  117. if (x == -1.0 && PyErr_Occurred()) {
  118. goto exit;
  119. }
  120. }
  121. i = args[1];
  122. return_value = math_ldexp_impl(module, x, i);
  123. exit:
  124. return return_value;
  125. }
  126. PyDoc_STRVAR(math_modf__doc__,
  127. "modf($module, x, /)\n"
  128. "--\n"
  129. "\n"
  130. "Return the fractional and integer parts of x.\n"
  131. "\n"
  132. "Both results carry the sign of x and are floats.");
  133. #define MATH_MODF_METHODDEF \
  134. {"modf", (PyCFunction)math_modf, METH_O, math_modf__doc__},
  135. static PyObject *
  136. math_modf_impl(PyObject *module, double x);
  137. static PyObject *
  138. math_modf(PyObject *module, PyObject *arg)
  139. {
  140. PyObject *return_value = NULL;
  141. double x;
  142. if (PyFloat_CheckExact(arg)) {
  143. x = PyFloat_AS_DOUBLE(arg);
  144. }
  145. else
  146. {
  147. x = PyFloat_AsDouble(arg);
  148. if (x == -1.0 && PyErr_Occurred()) {
  149. goto exit;
  150. }
  151. }
  152. return_value = math_modf_impl(module, x);
  153. exit:
  154. return return_value;
  155. }
  156. PyDoc_STRVAR(math_log2__doc__,
  157. "log2($module, x, /)\n"
  158. "--\n"
  159. "\n"
  160. "Return the base 2 logarithm of x.");
  161. #define MATH_LOG2_METHODDEF \
  162. {"log2", (PyCFunction)math_log2, METH_O, math_log2__doc__},
  163. PyDoc_STRVAR(math_log10__doc__,
  164. "log10($module, x, /)\n"
  165. "--\n"
  166. "\n"
  167. "Return the base 10 logarithm of x.");
  168. #define MATH_LOG10_METHODDEF \
  169. {"log10", (PyCFunction)math_log10, METH_O, math_log10__doc__},
  170. PyDoc_STRVAR(math_fmod__doc__,
  171. "fmod($module, x, y, /)\n"
  172. "--\n"
  173. "\n"
  174. "Return fmod(x, y), according to platform C.\n"
  175. "\n"
  176. "x % y may differ.");
  177. #define MATH_FMOD_METHODDEF \
  178. {"fmod", _PyCFunction_CAST(math_fmod), METH_FASTCALL, math_fmod__doc__},
  179. static PyObject *
  180. math_fmod_impl(PyObject *module, double x, double y);
  181. static PyObject *
  182. math_fmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  183. {
  184. PyObject *return_value = NULL;
  185. double x;
  186. double y;
  187. if (!_PyArg_CheckPositional("fmod", nargs, 2, 2)) {
  188. goto exit;
  189. }
  190. if (PyFloat_CheckExact(args[0])) {
  191. x = PyFloat_AS_DOUBLE(args[0]);
  192. }
  193. else
  194. {
  195. x = PyFloat_AsDouble(args[0]);
  196. if (x == -1.0 && PyErr_Occurred()) {
  197. goto exit;
  198. }
  199. }
  200. if (PyFloat_CheckExact(args[1])) {
  201. y = PyFloat_AS_DOUBLE(args[1]);
  202. }
  203. else
  204. {
  205. y = PyFloat_AsDouble(args[1]);
  206. if (y == -1.0 && PyErr_Occurred()) {
  207. goto exit;
  208. }
  209. }
  210. return_value = math_fmod_impl(module, x, y);
  211. exit:
  212. return return_value;
  213. }
  214. PyDoc_STRVAR(math_dist__doc__,
  215. "dist($module, p, q, /)\n"
  216. "--\n"
  217. "\n"
  218. "Return the Euclidean distance between two points p and q.\n"
  219. "\n"
  220. "The points should be specified as sequences (or iterables) of\n"
  221. "coordinates. Both inputs must have the same dimension.\n"
  222. "\n"
  223. "Roughly equivalent to:\n"
  224. " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))");
  225. #define MATH_DIST_METHODDEF \
  226. {"dist", _PyCFunction_CAST(math_dist), METH_FASTCALL, math_dist__doc__},
  227. static PyObject *
  228. math_dist_impl(PyObject *module, PyObject *p, PyObject *q);
  229. static PyObject *
  230. math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  231. {
  232. PyObject *return_value = NULL;
  233. PyObject *p;
  234. PyObject *q;
  235. if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
  236. goto exit;
  237. }
  238. p = args[0];
  239. q = args[1];
  240. return_value = math_dist_impl(module, p, q);
  241. exit:
  242. return return_value;
  243. }
  244. PyDoc_STRVAR(math_sumprod__doc__,
  245. "sumprod($module, p, q, /)\n"
  246. "--\n"
  247. "\n"
  248. "Return the sum of products of values from two iterables p and q.\n"
  249. "\n"
  250. "Roughly equivalent to:\n"
  251. "\n"
  252. " sum(itertools.starmap(operator.mul, zip(p, q, strict=True)))\n"
  253. "\n"
  254. "For float and mixed int/float inputs, the intermediate products\n"
  255. "and sums are computed with extended precision.");
  256. #define MATH_SUMPROD_METHODDEF \
  257. {"sumprod", _PyCFunction_CAST(math_sumprod), METH_FASTCALL, math_sumprod__doc__},
  258. static PyObject *
  259. math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q);
  260. static PyObject *
  261. math_sumprod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  262. {
  263. PyObject *return_value = NULL;
  264. PyObject *p;
  265. PyObject *q;
  266. if (!_PyArg_CheckPositional("sumprod", nargs, 2, 2)) {
  267. goto exit;
  268. }
  269. p = args[0];
  270. q = args[1];
  271. return_value = math_sumprod_impl(module, p, q);
  272. exit:
  273. return return_value;
  274. }
  275. PyDoc_STRVAR(math_pow__doc__,
  276. "pow($module, x, y, /)\n"
  277. "--\n"
  278. "\n"
  279. "Return x**y (x to the power of y).");
  280. #define MATH_POW_METHODDEF \
  281. {"pow", _PyCFunction_CAST(math_pow), METH_FASTCALL, math_pow__doc__},
  282. static PyObject *
  283. math_pow_impl(PyObject *module, double x, double y);
  284. static PyObject *
  285. math_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  286. {
  287. PyObject *return_value = NULL;
  288. double x;
  289. double y;
  290. if (!_PyArg_CheckPositional("pow", nargs, 2, 2)) {
  291. goto exit;
  292. }
  293. if (PyFloat_CheckExact(args[0])) {
  294. x = PyFloat_AS_DOUBLE(args[0]);
  295. }
  296. else
  297. {
  298. x = PyFloat_AsDouble(args[0]);
  299. if (x == -1.0 && PyErr_Occurred()) {
  300. goto exit;
  301. }
  302. }
  303. if (PyFloat_CheckExact(args[1])) {
  304. y = PyFloat_AS_DOUBLE(args[1]);
  305. }
  306. else
  307. {
  308. y = PyFloat_AsDouble(args[1]);
  309. if (y == -1.0 && PyErr_Occurred()) {
  310. goto exit;
  311. }
  312. }
  313. return_value = math_pow_impl(module, x, y);
  314. exit:
  315. return return_value;
  316. }
  317. PyDoc_STRVAR(math_degrees__doc__,
  318. "degrees($module, x, /)\n"
  319. "--\n"
  320. "\n"
  321. "Convert angle x from radians to degrees.");
  322. #define MATH_DEGREES_METHODDEF \
  323. {"degrees", (PyCFunction)math_degrees, METH_O, math_degrees__doc__},
  324. static PyObject *
  325. math_degrees_impl(PyObject *module, double x);
  326. static PyObject *
  327. math_degrees(PyObject *module, PyObject *arg)
  328. {
  329. PyObject *return_value = NULL;
  330. double x;
  331. if (PyFloat_CheckExact(arg)) {
  332. x = PyFloat_AS_DOUBLE(arg);
  333. }
  334. else
  335. {
  336. x = PyFloat_AsDouble(arg);
  337. if (x == -1.0 && PyErr_Occurred()) {
  338. goto exit;
  339. }
  340. }
  341. return_value = math_degrees_impl(module, x);
  342. exit:
  343. return return_value;
  344. }
  345. PyDoc_STRVAR(math_radians__doc__,
  346. "radians($module, x, /)\n"
  347. "--\n"
  348. "\n"
  349. "Convert angle x from degrees to radians.");
  350. #define MATH_RADIANS_METHODDEF \
  351. {"radians", (PyCFunction)math_radians, METH_O, math_radians__doc__},
  352. static PyObject *
  353. math_radians_impl(PyObject *module, double x);
  354. static PyObject *
  355. math_radians(PyObject *module, PyObject *arg)
  356. {
  357. PyObject *return_value = NULL;
  358. double x;
  359. if (PyFloat_CheckExact(arg)) {
  360. x = PyFloat_AS_DOUBLE(arg);
  361. }
  362. else
  363. {
  364. x = PyFloat_AsDouble(arg);
  365. if (x == -1.0 && PyErr_Occurred()) {
  366. goto exit;
  367. }
  368. }
  369. return_value = math_radians_impl(module, x);
  370. exit:
  371. return return_value;
  372. }
  373. PyDoc_STRVAR(math_isfinite__doc__,
  374. "isfinite($module, x, /)\n"
  375. "--\n"
  376. "\n"
  377. "Return True if x is neither an infinity nor a NaN, and False otherwise.");
  378. #define MATH_ISFINITE_METHODDEF \
  379. {"isfinite", (PyCFunction)math_isfinite, METH_O, math_isfinite__doc__},
  380. static PyObject *
  381. math_isfinite_impl(PyObject *module, double x);
  382. static PyObject *
  383. math_isfinite(PyObject *module, PyObject *arg)
  384. {
  385. PyObject *return_value = NULL;
  386. double x;
  387. if (PyFloat_CheckExact(arg)) {
  388. x = PyFloat_AS_DOUBLE(arg);
  389. }
  390. else
  391. {
  392. x = PyFloat_AsDouble(arg);
  393. if (x == -1.0 && PyErr_Occurred()) {
  394. goto exit;
  395. }
  396. }
  397. return_value = math_isfinite_impl(module, x);
  398. exit:
  399. return return_value;
  400. }
  401. PyDoc_STRVAR(math_isnan__doc__,
  402. "isnan($module, x, /)\n"
  403. "--\n"
  404. "\n"
  405. "Return True if x is a NaN (not a number), and False otherwise.");
  406. #define MATH_ISNAN_METHODDEF \
  407. {"isnan", (PyCFunction)math_isnan, METH_O, math_isnan__doc__},
  408. static PyObject *
  409. math_isnan_impl(PyObject *module, double x);
  410. static PyObject *
  411. math_isnan(PyObject *module, PyObject *arg)
  412. {
  413. PyObject *return_value = NULL;
  414. double x;
  415. if (PyFloat_CheckExact(arg)) {
  416. x = PyFloat_AS_DOUBLE(arg);
  417. }
  418. else
  419. {
  420. x = PyFloat_AsDouble(arg);
  421. if (x == -1.0 && PyErr_Occurred()) {
  422. goto exit;
  423. }
  424. }
  425. return_value = math_isnan_impl(module, x);
  426. exit:
  427. return return_value;
  428. }
  429. PyDoc_STRVAR(math_isinf__doc__,
  430. "isinf($module, x, /)\n"
  431. "--\n"
  432. "\n"
  433. "Return True if x is a positive or negative infinity, and False otherwise.");
  434. #define MATH_ISINF_METHODDEF \
  435. {"isinf", (PyCFunction)math_isinf, METH_O, math_isinf__doc__},
  436. static PyObject *
  437. math_isinf_impl(PyObject *module, double x);
  438. static PyObject *
  439. math_isinf(PyObject *module, PyObject *arg)
  440. {
  441. PyObject *return_value = NULL;
  442. double x;
  443. if (PyFloat_CheckExact(arg)) {
  444. x = PyFloat_AS_DOUBLE(arg);
  445. }
  446. else
  447. {
  448. x = PyFloat_AsDouble(arg);
  449. if (x == -1.0 && PyErr_Occurred()) {
  450. goto exit;
  451. }
  452. }
  453. return_value = math_isinf_impl(module, x);
  454. exit:
  455. return return_value;
  456. }
  457. PyDoc_STRVAR(math_isclose__doc__,
  458. "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
  459. "--\n"
  460. "\n"
  461. "Determine whether two floating-point numbers are close in value.\n"
  462. "\n"
  463. " rel_tol\n"
  464. " maximum difference for being considered \"close\", relative to the\n"
  465. " magnitude of the input values\n"
  466. " abs_tol\n"
  467. " maximum difference for being considered \"close\", regardless of the\n"
  468. " magnitude of the input values\n"
  469. "\n"
  470. "Return True if a is close in value to b, and False otherwise.\n"
  471. "\n"
  472. "For the values to be considered close, the difference between them\n"
  473. "must be smaller than at least one of the tolerances.\n"
  474. "\n"
  475. "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n"
  476. "is, NaN is not close to anything, even itself. inf and -inf are\n"
  477. "only close to themselves.");
  478. #define MATH_ISCLOSE_METHODDEF \
  479. {"isclose", _PyCFunction_CAST(math_isclose), METH_FASTCALL|METH_KEYWORDS, math_isclose__doc__},
  480. static int
  481. math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
  482. double abs_tol);
  483. static PyObject *
  484. math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  485. {
  486. PyObject *return_value = NULL;
  487. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  488. #define NUM_KEYWORDS 4
  489. static struct {
  490. PyGC_Head _this_is_not_used;
  491. PyObject_VAR_HEAD
  492. PyObject *ob_item[NUM_KEYWORDS];
  493. } _kwtuple = {
  494. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  495. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('b'), &_Py_ID(rel_tol), &_Py_ID(abs_tol), },
  496. };
  497. #undef NUM_KEYWORDS
  498. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  499. #else // !Py_BUILD_CORE
  500. # define KWTUPLE NULL
  501. #endif // !Py_BUILD_CORE
  502. static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
  503. static _PyArg_Parser _parser = {
  504. .keywords = _keywords,
  505. .fname = "isclose",
  506. .kwtuple = KWTUPLE,
  507. };
  508. #undef KWTUPLE
  509. PyObject *argsbuf[4];
  510. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  511. double a;
  512. double b;
  513. double rel_tol = 1e-09;
  514. double abs_tol = 0.0;
  515. int _return_value;
  516. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
  517. if (!args) {
  518. goto exit;
  519. }
  520. if (PyFloat_CheckExact(args[0])) {
  521. a = PyFloat_AS_DOUBLE(args[0]);
  522. }
  523. else
  524. {
  525. a = PyFloat_AsDouble(args[0]);
  526. if (a == -1.0 && PyErr_Occurred()) {
  527. goto exit;
  528. }
  529. }
  530. if (PyFloat_CheckExact(args[1])) {
  531. b = PyFloat_AS_DOUBLE(args[1]);
  532. }
  533. else
  534. {
  535. b = PyFloat_AsDouble(args[1]);
  536. if (b == -1.0 && PyErr_Occurred()) {
  537. goto exit;
  538. }
  539. }
  540. if (!noptargs) {
  541. goto skip_optional_kwonly;
  542. }
  543. if (args[2]) {
  544. if (PyFloat_CheckExact(args[2])) {
  545. rel_tol = PyFloat_AS_DOUBLE(args[2]);
  546. }
  547. else
  548. {
  549. rel_tol = PyFloat_AsDouble(args[2]);
  550. if (rel_tol == -1.0 && PyErr_Occurred()) {
  551. goto exit;
  552. }
  553. }
  554. if (!--noptargs) {
  555. goto skip_optional_kwonly;
  556. }
  557. }
  558. if (PyFloat_CheckExact(args[3])) {
  559. abs_tol = PyFloat_AS_DOUBLE(args[3]);
  560. }
  561. else
  562. {
  563. abs_tol = PyFloat_AsDouble(args[3]);
  564. if (abs_tol == -1.0 && PyErr_Occurred()) {
  565. goto exit;
  566. }
  567. }
  568. skip_optional_kwonly:
  569. _return_value = math_isclose_impl(module, a, b, rel_tol, abs_tol);
  570. if ((_return_value == -1) && PyErr_Occurred()) {
  571. goto exit;
  572. }
  573. return_value = PyBool_FromLong((long)_return_value);
  574. exit:
  575. return return_value;
  576. }
  577. PyDoc_STRVAR(math_prod__doc__,
  578. "prod($module, iterable, /, *, start=1)\n"
  579. "--\n"
  580. "\n"
  581. "Calculate the product of all the elements in the input iterable.\n"
  582. "\n"
  583. "The default start value for the product is 1.\n"
  584. "\n"
  585. "When the iterable is empty, return the start value. This function is\n"
  586. "intended specifically for use with numeric values and may reject\n"
  587. "non-numeric types.");
  588. #define MATH_PROD_METHODDEF \
  589. {"prod", _PyCFunction_CAST(math_prod), METH_FASTCALL|METH_KEYWORDS, math_prod__doc__},
  590. static PyObject *
  591. math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start);
  592. static PyObject *
  593. math_prod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  594. {
  595. PyObject *return_value = NULL;
  596. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  597. #define NUM_KEYWORDS 1
  598. static struct {
  599. PyGC_Head _this_is_not_used;
  600. PyObject_VAR_HEAD
  601. PyObject *ob_item[NUM_KEYWORDS];
  602. } _kwtuple = {
  603. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  604. .ob_item = { &_Py_ID(start), },
  605. };
  606. #undef NUM_KEYWORDS
  607. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  608. #else // !Py_BUILD_CORE
  609. # define KWTUPLE NULL
  610. #endif // !Py_BUILD_CORE
  611. static const char * const _keywords[] = {"", "start", NULL};
  612. static _PyArg_Parser _parser = {
  613. .keywords = _keywords,
  614. .fname = "prod",
  615. .kwtuple = KWTUPLE,
  616. };
  617. #undef KWTUPLE
  618. PyObject *argsbuf[2];
  619. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  620. PyObject *iterable;
  621. PyObject *start = NULL;
  622. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  623. if (!args) {
  624. goto exit;
  625. }
  626. iterable = args[0];
  627. if (!noptargs) {
  628. goto skip_optional_kwonly;
  629. }
  630. start = args[1];
  631. skip_optional_kwonly:
  632. return_value = math_prod_impl(module, iterable, start);
  633. exit:
  634. return return_value;
  635. }
  636. PyDoc_STRVAR(math_perm__doc__,
  637. "perm($module, n, k=None, /)\n"
  638. "--\n"
  639. "\n"
  640. "Number of ways to choose k items from n items without repetition and with order.\n"
  641. "\n"
  642. "Evaluates to n! / (n - k)! when k <= n and evaluates\n"
  643. "to zero when k > n.\n"
  644. "\n"
  645. "If k is not specified or is None, then k defaults to n\n"
  646. "and the function returns n!.\n"
  647. "\n"
  648. "Raises TypeError if either of the arguments are not integers.\n"
  649. "Raises ValueError if either of the arguments are negative.");
  650. #define MATH_PERM_METHODDEF \
  651. {"perm", _PyCFunction_CAST(math_perm), METH_FASTCALL, math_perm__doc__},
  652. static PyObject *
  653. math_perm_impl(PyObject *module, PyObject *n, PyObject *k);
  654. static PyObject *
  655. math_perm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  656. {
  657. PyObject *return_value = NULL;
  658. PyObject *n;
  659. PyObject *k = Py_None;
  660. if (!_PyArg_CheckPositional("perm", nargs, 1, 2)) {
  661. goto exit;
  662. }
  663. n = args[0];
  664. if (nargs < 2) {
  665. goto skip_optional;
  666. }
  667. k = args[1];
  668. skip_optional:
  669. return_value = math_perm_impl(module, n, k);
  670. exit:
  671. return return_value;
  672. }
  673. PyDoc_STRVAR(math_comb__doc__,
  674. "comb($module, n, k, /)\n"
  675. "--\n"
  676. "\n"
  677. "Number of ways to choose k items from n items without repetition and without order.\n"
  678. "\n"
  679. "Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n"
  680. "to zero when k > n.\n"
  681. "\n"
  682. "Also called the binomial coefficient because it is equivalent\n"
  683. "to the coefficient of k-th term in polynomial expansion of the\n"
  684. "expression (1 + x)**n.\n"
  685. "\n"
  686. "Raises TypeError if either of the arguments are not integers.\n"
  687. "Raises ValueError if either of the arguments are negative.");
  688. #define MATH_COMB_METHODDEF \
  689. {"comb", _PyCFunction_CAST(math_comb), METH_FASTCALL, math_comb__doc__},
  690. static PyObject *
  691. math_comb_impl(PyObject *module, PyObject *n, PyObject *k);
  692. static PyObject *
  693. math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  694. {
  695. PyObject *return_value = NULL;
  696. PyObject *n;
  697. PyObject *k;
  698. if (!_PyArg_CheckPositional("comb", nargs, 2, 2)) {
  699. goto exit;
  700. }
  701. n = args[0];
  702. k = args[1];
  703. return_value = math_comb_impl(module, n, k);
  704. exit:
  705. return return_value;
  706. }
  707. PyDoc_STRVAR(math_nextafter__doc__,
  708. "nextafter($module, x, y, /, *, steps=None)\n"
  709. "--\n"
  710. "\n"
  711. "Return the floating-point value the given number of steps after x towards y.\n"
  712. "\n"
  713. "If steps is not specified or is None, it defaults to 1.\n"
  714. "\n"
  715. "Raises a TypeError, if x or y is not a double, or if steps is not an integer.\n"
  716. "Raises ValueError if steps is negative.");
  717. #define MATH_NEXTAFTER_METHODDEF \
  718. {"nextafter", _PyCFunction_CAST(math_nextafter), METH_FASTCALL|METH_KEYWORDS, math_nextafter__doc__},
  719. static PyObject *
  720. math_nextafter_impl(PyObject *module, double x, double y, PyObject *steps);
  721. static PyObject *
  722. math_nextafter(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  723. {
  724. PyObject *return_value = NULL;
  725. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  726. #define NUM_KEYWORDS 1
  727. static struct {
  728. PyGC_Head _this_is_not_used;
  729. PyObject_VAR_HEAD
  730. PyObject *ob_item[NUM_KEYWORDS];
  731. } _kwtuple = {
  732. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  733. .ob_item = { &_Py_ID(steps), },
  734. };
  735. #undef NUM_KEYWORDS
  736. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  737. #else // !Py_BUILD_CORE
  738. # define KWTUPLE NULL
  739. #endif // !Py_BUILD_CORE
  740. static const char * const _keywords[] = {"", "", "steps", NULL};
  741. static _PyArg_Parser _parser = {
  742. .keywords = _keywords,
  743. .fname = "nextafter",
  744. .kwtuple = KWTUPLE,
  745. };
  746. #undef KWTUPLE
  747. PyObject *argsbuf[3];
  748. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  749. double x;
  750. double y;
  751. PyObject *steps = Py_None;
  752. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
  753. if (!args) {
  754. goto exit;
  755. }
  756. if (PyFloat_CheckExact(args[0])) {
  757. x = PyFloat_AS_DOUBLE(args[0]);
  758. }
  759. else
  760. {
  761. x = PyFloat_AsDouble(args[0]);
  762. if (x == -1.0 && PyErr_Occurred()) {
  763. goto exit;
  764. }
  765. }
  766. if (PyFloat_CheckExact(args[1])) {
  767. y = PyFloat_AS_DOUBLE(args[1]);
  768. }
  769. else
  770. {
  771. y = PyFloat_AsDouble(args[1]);
  772. if (y == -1.0 && PyErr_Occurred()) {
  773. goto exit;
  774. }
  775. }
  776. if (!noptargs) {
  777. goto skip_optional_kwonly;
  778. }
  779. steps = args[2];
  780. skip_optional_kwonly:
  781. return_value = math_nextafter_impl(module, x, y, steps);
  782. exit:
  783. return return_value;
  784. }
  785. PyDoc_STRVAR(math_ulp__doc__,
  786. "ulp($module, x, /)\n"
  787. "--\n"
  788. "\n"
  789. "Return the value of the least significant bit of the float x.");
  790. #define MATH_ULP_METHODDEF \
  791. {"ulp", (PyCFunction)math_ulp, METH_O, math_ulp__doc__},
  792. static double
  793. math_ulp_impl(PyObject *module, double x);
  794. static PyObject *
  795. math_ulp(PyObject *module, PyObject *arg)
  796. {
  797. PyObject *return_value = NULL;
  798. double x;
  799. double _return_value;
  800. if (PyFloat_CheckExact(arg)) {
  801. x = PyFloat_AS_DOUBLE(arg);
  802. }
  803. else
  804. {
  805. x = PyFloat_AsDouble(arg);
  806. if (x == -1.0 && PyErr_Occurred()) {
  807. goto exit;
  808. }
  809. }
  810. _return_value = math_ulp_impl(module, x);
  811. if ((_return_value == -1.0) && PyErr_Occurred()) {
  812. goto exit;
  813. }
  814. return_value = PyFloat_FromDouble(_return_value);
  815. exit:
  816. return return_value;
  817. }
  818. /*[clinic end generated code: output=c1335a499389a04e input=a9049054013a1b77]*/