cmathmodule.c.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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(cmath_acos__doc__,
  9. "acos($module, z, /)\n"
  10. "--\n"
  11. "\n"
  12. "Return the arc cosine of z.");
  13. #define CMATH_ACOS_METHODDEF \
  14. {"acos", (PyCFunction)cmath_acos, METH_O, cmath_acos__doc__},
  15. static Py_complex
  16. cmath_acos_impl(PyObject *module, Py_complex z);
  17. static PyObject *
  18. cmath_acos(PyObject *module, PyObject *arg)
  19. {
  20. PyObject *return_value = NULL;
  21. Py_complex z;
  22. Py_complex _return_value;
  23. z = PyComplex_AsCComplex(arg);
  24. if (PyErr_Occurred()) {
  25. goto exit;
  26. }
  27. /* modifications for z */
  28. errno = 0;
  29. _return_value = cmath_acos_impl(module, z);
  30. if (errno == EDOM) {
  31. PyErr_SetString(PyExc_ValueError, "math domain error");
  32. goto exit;
  33. }
  34. else if (errno == ERANGE) {
  35. PyErr_SetString(PyExc_OverflowError, "math range error");
  36. goto exit;
  37. }
  38. else {
  39. return_value = PyComplex_FromCComplex(_return_value);
  40. }
  41. exit:
  42. return return_value;
  43. }
  44. PyDoc_STRVAR(cmath_acosh__doc__,
  45. "acosh($module, z, /)\n"
  46. "--\n"
  47. "\n"
  48. "Return the inverse hyperbolic cosine of z.");
  49. #define CMATH_ACOSH_METHODDEF \
  50. {"acosh", (PyCFunction)cmath_acosh, METH_O, cmath_acosh__doc__},
  51. static Py_complex
  52. cmath_acosh_impl(PyObject *module, Py_complex z);
  53. static PyObject *
  54. cmath_acosh(PyObject *module, PyObject *arg)
  55. {
  56. PyObject *return_value = NULL;
  57. Py_complex z;
  58. Py_complex _return_value;
  59. z = PyComplex_AsCComplex(arg);
  60. if (PyErr_Occurred()) {
  61. goto exit;
  62. }
  63. /* modifications for z */
  64. errno = 0;
  65. _return_value = cmath_acosh_impl(module, z);
  66. if (errno == EDOM) {
  67. PyErr_SetString(PyExc_ValueError, "math domain error");
  68. goto exit;
  69. }
  70. else if (errno == ERANGE) {
  71. PyErr_SetString(PyExc_OverflowError, "math range error");
  72. goto exit;
  73. }
  74. else {
  75. return_value = PyComplex_FromCComplex(_return_value);
  76. }
  77. exit:
  78. return return_value;
  79. }
  80. PyDoc_STRVAR(cmath_asin__doc__,
  81. "asin($module, z, /)\n"
  82. "--\n"
  83. "\n"
  84. "Return the arc sine of z.");
  85. #define CMATH_ASIN_METHODDEF \
  86. {"asin", (PyCFunction)cmath_asin, METH_O, cmath_asin__doc__},
  87. static Py_complex
  88. cmath_asin_impl(PyObject *module, Py_complex z);
  89. static PyObject *
  90. cmath_asin(PyObject *module, PyObject *arg)
  91. {
  92. PyObject *return_value = NULL;
  93. Py_complex z;
  94. Py_complex _return_value;
  95. z = PyComplex_AsCComplex(arg);
  96. if (PyErr_Occurred()) {
  97. goto exit;
  98. }
  99. /* modifications for z */
  100. errno = 0;
  101. _return_value = cmath_asin_impl(module, z);
  102. if (errno == EDOM) {
  103. PyErr_SetString(PyExc_ValueError, "math domain error");
  104. goto exit;
  105. }
  106. else if (errno == ERANGE) {
  107. PyErr_SetString(PyExc_OverflowError, "math range error");
  108. goto exit;
  109. }
  110. else {
  111. return_value = PyComplex_FromCComplex(_return_value);
  112. }
  113. exit:
  114. return return_value;
  115. }
  116. PyDoc_STRVAR(cmath_asinh__doc__,
  117. "asinh($module, z, /)\n"
  118. "--\n"
  119. "\n"
  120. "Return the inverse hyperbolic sine of z.");
  121. #define CMATH_ASINH_METHODDEF \
  122. {"asinh", (PyCFunction)cmath_asinh, METH_O, cmath_asinh__doc__},
  123. static Py_complex
  124. cmath_asinh_impl(PyObject *module, Py_complex z);
  125. static PyObject *
  126. cmath_asinh(PyObject *module, PyObject *arg)
  127. {
  128. PyObject *return_value = NULL;
  129. Py_complex z;
  130. Py_complex _return_value;
  131. z = PyComplex_AsCComplex(arg);
  132. if (PyErr_Occurred()) {
  133. goto exit;
  134. }
  135. /* modifications for z */
  136. errno = 0;
  137. _return_value = cmath_asinh_impl(module, z);
  138. if (errno == EDOM) {
  139. PyErr_SetString(PyExc_ValueError, "math domain error");
  140. goto exit;
  141. }
  142. else if (errno == ERANGE) {
  143. PyErr_SetString(PyExc_OverflowError, "math range error");
  144. goto exit;
  145. }
  146. else {
  147. return_value = PyComplex_FromCComplex(_return_value);
  148. }
  149. exit:
  150. return return_value;
  151. }
  152. PyDoc_STRVAR(cmath_atan__doc__,
  153. "atan($module, z, /)\n"
  154. "--\n"
  155. "\n"
  156. "Return the arc tangent of z.");
  157. #define CMATH_ATAN_METHODDEF \
  158. {"atan", (PyCFunction)cmath_atan, METH_O, cmath_atan__doc__},
  159. static Py_complex
  160. cmath_atan_impl(PyObject *module, Py_complex z);
  161. static PyObject *
  162. cmath_atan(PyObject *module, PyObject *arg)
  163. {
  164. PyObject *return_value = NULL;
  165. Py_complex z;
  166. Py_complex _return_value;
  167. z = PyComplex_AsCComplex(arg);
  168. if (PyErr_Occurred()) {
  169. goto exit;
  170. }
  171. /* modifications for z */
  172. errno = 0;
  173. _return_value = cmath_atan_impl(module, z);
  174. if (errno == EDOM) {
  175. PyErr_SetString(PyExc_ValueError, "math domain error");
  176. goto exit;
  177. }
  178. else if (errno == ERANGE) {
  179. PyErr_SetString(PyExc_OverflowError, "math range error");
  180. goto exit;
  181. }
  182. else {
  183. return_value = PyComplex_FromCComplex(_return_value);
  184. }
  185. exit:
  186. return return_value;
  187. }
  188. PyDoc_STRVAR(cmath_atanh__doc__,
  189. "atanh($module, z, /)\n"
  190. "--\n"
  191. "\n"
  192. "Return the inverse hyperbolic tangent of z.");
  193. #define CMATH_ATANH_METHODDEF \
  194. {"atanh", (PyCFunction)cmath_atanh, METH_O, cmath_atanh__doc__},
  195. static Py_complex
  196. cmath_atanh_impl(PyObject *module, Py_complex z);
  197. static PyObject *
  198. cmath_atanh(PyObject *module, PyObject *arg)
  199. {
  200. PyObject *return_value = NULL;
  201. Py_complex z;
  202. Py_complex _return_value;
  203. z = PyComplex_AsCComplex(arg);
  204. if (PyErr_Occurred()) {
  205. goto exit;
  206. }
  207. /* modifications for z */
  208. errno = 0;
  209. _return_value = cmath_atanh_impl(module, z);
  210. if (errno == EDOM) {
  211. PyErr_SetString(PyExc_ValueError, "math domain error");
  212. goto exit;
  213. }
  214. else if (errno == ERANGE) {
  215. PyErr_SetString(PyExc_OverflowError, "math range error");
  216. goto exit;
  217. }
  218. else {
  219. return_value = PyComplex_FromCComplex(_return_value);
  220. }
  221. exit:
  222. return return_value;
  223. }
  224. PyDoc_STRVAR(cmath_cos__doc__,
  225. "cos($module, z, /)\n"
  226. "--\n"
  227. "\n"
  228. "Return the cosine of z.");
  229. #define CMATH_COS_METHODDEF \
  230. {"cos", (PyCFunction)cmath_cos, METH_O, cmath_cos__doc__},
  231. static Py_complex
  232. cmath_cos_impl(PyObject *module, Py_complex z);
  233. static PyObject *
  234. cmath_cos(PyObject *module, PyObject *arg)
  235. {
  236. PyObject *return_value = NULL;
  237. Py_complex z;
  238. Py_complex _return_value;
  239. z = PyComplex_AsCComplex(arg);
  240. if (PyErr_Occurred()) {
  241. goto exit;
  242. }
  243. /* modifications for z */
  244. errno = 0;
  245. _return_value = cmath_cos_impl(module, z);
  246. if (errno == EDOM) {
  247. PyErr_SetString(PyExc_ValueError, "math domain error");
  248. goto exit;
  249. }
  250. else if (errno == ERANGE) {
  251. PyErr_SetString(PyExc_OverflowError, "math range error");
  252. goto exit;
  253. }
  254. else {
  255. return_value = PyComplex_FromCComplex(_return_value);
  256. }
  257. exit:
  258. return return_value;
  259. }
  260. PyDoc_STRVAR(cmath_cosh__doc__,
  261. "cosh($module, z, /)\n"
  262. "--\n"
  263. "\n"
  264. "Return the hyperbolic cosine of z.");
  265. #define CMATH_COSH_METHODDEF \
  266. {"cosh", (PyCFunction)cmath_cosh, METH_O, cmath_cosh__doc__},
  267. static Py_complex
  268. cmath_cosh_impl(PyObject *module, Py_complex z);
  269. static PyObject *
  270. cmath_cosh(PyObject *module, PyObject *arg)
  271. {
  272. PyObject *return_value = NULL;
  273. Py_complex z;
  274. Py_complex _return_value;
  275. z = PyComplex_AsCComplex(arg);
  276. if (PyErr_Occurred()) {
  277. goto exit;
  278. }
  279. /* modifications for z */
  280. errno = 0;
  281. _return_value = cmath_cosh_impl(module, z);
  282. if (errno == EDOM) {
  283. PyErr_SetString(PyExc_ValueError, "math domain error");
  284. goto exit;
  285. }
  286. else if (errno == ERANGE) {
  287. PyErr_SetString(PyExc_OverflowError, "math range error");
  288. goto exit;
  289. }
  290. else {
  291. return_value = PyComplex_FromCComplex(_return_value);
  292. }
  293. exit:
  294. return return_value;
  295. }
  296. PyDoc_STRVAR(cmath_exp__doc__,
  297. "exp($module, z, /)\n"
  298. "--\n"
  299. "\n"
  300. "Return the exponential value e**z.");
  301. #define CMATH_EXP_METHODDEF \
  302. {"exp", (PyCFunction)cmath_exp, METH_O, cmath_exp__doc__},
  303. static Py_complex
  304. cmath_exp_impl(PyObject *module, Py_complex z);
  305. static PyObject *
  306. cmath_exp(PyObject *module, PyObject *arg)
  307. {
  308. PyObject *return_value = NULL;
  309. Py_complex z;
  310. Py_complex _return_value;
  311. z = PyComplex_AsCComplex(arg);
  312. if (PyErr_Occurred()) {
  313. goto exit;
  314. }
  315. /* modifications for z */
  316. errno = 0;
  317. _return_value = cmath_exp_impl(module, z);
  318. if (errno == EDOM) {
  319. PyErr_SetString(PyExc_ValueError, "math domain error");
  320. goto exit;
  321. }
  322. else if (errno == ERANGE) {
  323. PyErr_SetString(PyExc_OverflowError, "math range error");
  324. goto exit;
  325. }
  326. else {
  327. return_value = PyComplex_FromCComplex(_return_value);
  328. }
  329. exit:
  330. return return_value;
  331. }
  332. PyDoc_STRVAR(cmath_log10__doc__,
  333. "log10($module, z, /)\n"
  334. "--\n"
  335. "\n"
  336. "Return the base-10 logarithm of z.");
  337. #define CMATH_LOG10_METHODDEF \
  338. {"log10", (PyCFunction)cmath_log10, METH_O, cmath_log10__doc__},
  339. static Py_complex
  340. cmath_log10_impl(PyObject *module, Py_complex z);
  341. static PyObject *
  342. cmath_log10(PyObject *module, PyObject *arg)
  343. {
  344. PyObject *return_value = NULL;
  345. Py_complex z;
  346. Py_complex _return_value;
  347. z = PyComplex_AsCComplex(arg);
  348. if (PyErr_Occurred()) {
  349. goto exit;
  350. }
  351. /* modifications for z */
  352. errno = 0;
  353. _return_value = cmath_log10_impl(module, z);
  354. if (errno == EDOM) {
  355. PyErr_SetString(PyExc_ValueError, "math domain error");
  356. goto exit;
  357. }
  358. else if (errno == ERANGE) {
  359. PyErr_SetString(PyExc_OverflowError, "math range error");
  360. goto exit;
  361. }
  362. else {
  363. return_value = PyComplex_FromCComplex(_return_value);
  364. }
  365. exit:
  366. return return_value;
  367. }
  368. PyDoc_STRVAR(cmath_sin__doc__,
  369. "sin($module, z, /)\n"
  370. "--\n"
  371. "\n"
  372. "Return the sine of z.");
  373. #define CMATH_SIN_METHODDEF \
  374. {"sin", (PyCFunction)cmath_sin, METH_O, cmath_sin__doc__},
  375. static Py_complex
  376. cmath_sin_impl(PyObject *module, Py_complex z);
  377. static PyObject *
  378. cmath_sin(PyObject *module, PyObject *arg)
  379. {
  380. PyObject *return_value = NULL;
  381. Py_complex z;
  382. Py_complex _return_value;
  383. z = PyComplex_AsCComplex(arg);
  384. if (PyErr_Occurred()) {
  385. goto exit;
  386. }
  387. /* modifications for z */
  388. errno = 0;
  389. _return_value = cmath_sin_impl(module, z);
  390. if (errno == EDOM) {
  391. PyErr_SetString(PyExc_ValueError, "math domain error");
  392. goto exit;
  393. }
  394. else if (errno == ERANGE) {
  395. PyErr_SetString(PyExc_OverflowError, "math range error");
  396. goto exit;
  397. }
  398. else {
  399. return_value = PyComplex_FromCComplex(_return_value);
  400. }
  401. exit:
  402. return return_value;
  403. }
  404. PyDoc_STRVAR(cmath_sinh__doc__,
  405. "sinh($module, z, /)\n"
  406. "--\n"
  407. "\n"
  408. "Return the hyperbolic sine of z.");
  409. #define CMATH_SINH_METHODDEF \
  410. {"sinh", (PyCFunction)cmath_sinh, METH_O, cmath_sinh__doc__},
  411. static Py_complex
  412. cmath_sinh_impl(PyObject *module, Py_complex z);
  413. static PyObject *
  414. cmath_sinh(PyObject *module, PyObject *arg)
  415. {
  416. PyObject *return_value = NULL;
  417. Py_complex z;
  418. Py_complex _return_value;
  419. z = PyComplex_AsCComplex(arg);
  420. if (PyErr_Occurred()) {
  421. goto exit;
  422. }
  423. /* modifications for z */
  424. errno = 0;
  425. _return_value = cmath_sinh_impl(module, z);
  426. if (errno == EDOM) {
  427. PyErr_SetString(PyExc_ValueError, "math domain error");
  428. goto exit;
  429. }
  430. else if (errno == ERANGE) {
  431. PyErr_SetString(PyExc_OverflowError, "math range error");
  432. goto exit;
  433. }
  434. else {
  435. return_value = PyComplex_FromCComplex(_return_value);
  436. }
  437. exit:
  438. return return_value;
  439. }
  440. PyDoc_STRVAR(cmath_sqrt__doc__,
  441. "sqrt($module, z, /)\n"
  442. "--\n"
  443. "\n"
  444. "Return the square root of z.");
  445. #define CMATH_SQRT_METHODDEF \
  446. {"sqrt", (PyCFunction)cmath_sqrt, METH_O, cmath_sqrt__doc__},
  447. static Py_complex
  448. cmath_sqrt_impl(PyObject *module, Py_complex z);
  449. static PyObject *
  450. cmath_sqrt(PyObject *module, PyObject *arg)
  451. {
  452. PyObject *return_value = NULL;
  453. Py_complex z;
  454. Py_complex _return_value;
  455. z = PyComplex_AsCComplex(arg);
  456. if (PyErr_Occurred()) {
  457. goto exit;
  458. }
  459. /* modifications for z */
  460. errno = 0;
  461. _return_value = cmath_sqrt_impl(module, z);
  462. if (errno == EDOM) {
  463. PyErr_SetString(PyExc_ValueError, "math domain error");
  464. goto exit;
  465. }
  466. else if (errno == ERANGE) {
  467. PyErr_SetString(PyExc_OverflowError, "math range error");
  468. goto exit;
  469. }
  470. else {
  471. return_value = PyComplex_FromCComplex(_return_value);
  472. }
  473. exit:
  474. return return_value;
  475. }
  476. PyDoc_STRVAR(cmath_tan__doc__,
  477. "tan($module, z, /)\n"
  478. "--\n"
  479. "\n"
  480. "Return the tangent of z.");
  481. #define CMATH_TAN_METHODDEF \
  482. {"tan", (PyCFunction)cmath_tan, METH_O, cmath_tan__doc__},
  483. static Py_complex
  484. cmath_tan_impl(PyObject *module, Py_complex z);
  485. static PyObject *
  486. cmath_tan(PyObject *module, PyObject *arg)
  487. {
  488. PyObject *return_value = NULL;
  489. Py_complex z;
  490. Py_complex _return_value;
  491. z = PyComplex_AsCComplex(arg);
  492. if (PyErr_Occurred()) {
  493. goto exit;
  494. }
  495. /* modifications for z */
  496. errno = 0;
  497. _return_value = cmath_tan_impl(module, z);
  498. if (errno == EDOM) {
  499. PyErr_SetString(PyExc_ValueError, "math domain error");
  500. goto exit;
  501. }
  502. else if (errno == ERANGE) {
  503. PyErr_SetString(PyExc_OverflowError, "math range error");
  504. goto exit;
  505. }
  506. else {
  507. return_value = PyComplex_FromCComplex(_return_value);
  508. }
  509. exit:
  510. return return_value;
  511. }
  512. PyDoc_STRVAR(cmath_tanh__doc__,
  513. "tanh($module, z, /)\n"
  514. "--\n"
  515. "\n"
  516. "Return the hyperbolic tangent of z.");
  517. #define CMATH_TANH_METHODDEF \
  518. {"tanh", (PyCFunction)cmath_tanh, METH_O, cmath_tanh__doc__},
  519. static Py_complex
  520. cmath_tanh_impl(PyObject *module, Py_complex z);
  521. static PyObject *
  522. cmath_tanh(PyObject *module, PyObject *arg)
  523. {
  524. PyObject *return_value = NULL;
  525. Py_complex z;
  526. Py_complex _return_value;
  527. z = PyComplex_AsCComplex(arg);
  528. if (PyErr_Occurred()) {
  529. goto exit;
  530. }
  531. /* modifications for z */
  532. errno = 0;
  533. _return_value = cmath_tanh_impl(module, z);
  534. if (errno == EDOM) {
  535. PyErr_SetString(PyExc_ValueError, "math domain error");
  536. goto exit;
  537. }
  538. else if (errno == ERANGE) {
  539. PyErr_SetString(PyExc_OverflowError, "math range error");
  540. goto exit;
  541. }
  542. else {
  543. return_value = PyComplex_FromCComplex(_return_value);
  544. }
  545. exit:
  546. return return_value;
  547. }
  548. PyDoc_STRVAR(cmath_log__doc__,
  549. "log($module, z, base=<unrepresentable>, /)\n"
  550. "--\n"
  551. "\n"
  552. "log(z[, base]) -> the logarithm of z to the given base.\n"
  553. "\n"
  554. "If the base is not specified, returns the natural logarithm (base e) of z.");
  555. #define CMATH_LOG_METHODDEF \
  556. {"log", _PyCFunction_CAST(cmath_log), METH_FASTCALL, cmath_log__doc__},
  557. static PyObject *
  558. cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj);
  559. static PyObject *
  560. cmath_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  561. {
  562. PyObject *return_value = NULL;
  563. Py_complex x;
  564. PyObject *y_obj = NULL;
  565. if (!_PyArg_CheckPositional("log", nargs, 1, 2)) {
  566. goto exit;
  567. }
  568. x = PyComplex_AsCComplex(args[0]);
  569. if (PyErr_Occurred()) {
  570. goto exit;
  571. }
  572. if (nargs < 2) {
  573. goto skip_optional;
  574. }
  575. y_obj = args[1];
  576. skip_optional:
  577. return_value = cmath_log_impl(module, x, y_obj);
  578. exit:
  579. return return_value;
  580. }
  581. PyDoc_STRVAR(cmath_phase__doc__,
  582. "phase($module, z, /)\n"
  583. "--\n"
  584. "\n"
  585. "Return argument, also known as the phase angle, of a complex.");
  586. #define CMATH_PHASE_METHODDEF \
  587. {"phase", (PyCFunction)cmath_phase, METH_O, cmath_phase__doc__},
  588. static PyObject *
  589. cmath_phase_impl(PyObject *module, Py_complex z);
  590. static PyObject *
  591. cmath_phase(PyObject *module, PyObject *arg)
  592. {
  593. PyObject *return_value = NULL;
  594. Py_complex z;
  595. z = PyComplex_AsCComplex(arg);
  596. if (PyErr_Occurred()) {
  597. goto exit;
  598. }
  599. return_value = cmath_phase_impl(module, z);
  600. exit:
  601. return return_value;
  602. }
  603. PyDoc_STRVAR(cmath_polar__doc__,
  604. "polar($module, z, /)\n"
  605. "--\n"
  606. "\n"
  607. "Convert a complex from rectangular coordinates to polar coordinates.\n"
  608. "\n"
  609. "r is the distance from 0 and phi the phase angle.");
  610. #define CMATH_POLAR_METHODDEF \
  611. {"polar", (PyCFunction)cmath_polar, METH_O, cmath_polar__doc__},
  612. static PyObject *
  613. cmath_polar_impl(PyObject *module, Py_complex z);
  614. static PyObject *
  615. cmath_polar(PyObject *module, PyObject *arg)
  616. {
  617. PyObject *return_value = NULL;
  618. Py_complex z;
  619. z = PyComplex_AsCComplex(arg);
  620. if (PyErr_Occurred()) {
  621. goto exit;
  622. }
  623. return_value = cmath_polar_impl(module, z);
  624. exit:
  625. return return_value;
  626. }
  627. PyDoc_STRVAR(cmath_rect__doc__,
  628. "rect($module, r, phi, /)\n"
  629. "--\n"
  630. "\n"
  631. "Convert from polar coordinates to rectangular coordinates.");
  632. #define CMATH_RECT_METHODDEF \
  633. {"rect", _PyCFunction_CAST(cmath_rect), METH_FASTCALL, cmath_rect__doc__},
  634. static PyObject *
  635. cmath_rect_impl(PyObject *module, double r, double phi);
  636. static PyObject *
  637. cmath_rect(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  638. {
  639. PyObject *return_value = NULL;
  640. double r;
  641. double phi;
  642. if (!_PyArg_CheckPositional("rect", nargs, 2, 2)) {
  643. goto exit;
  644. }
  645. if (PyFloat_CheckExact(args[0])) {
  646. r = PyFloat_AS_DOUBLE(args[0]);
  647. }
  648. else
  649. {
  650. r = PyFloat_AsDouble(args[0]);
  651. if (r == -1.0 && PyErr_Occurred()) {
  652. goto exit;
  653. }
  654. }
  655. if (PyFloat_CheckExact(args[1])) {
  656. phi = PyFloat_AS_DOUBLE(args[1]);
  657. }
  658. else
  659. {
  660. phi = PyFloat_AsDouble(args[1]);
  661. if (phi == -1.0 && PyErr_Occurred()) {
  662. goto exit;
  663. }
  664. }
  665. return_value = cmath_rect_impl(module, r, phi);
  666. exit:
  667. return return_value;
  668. }
  669. PyDoc_STRVAR(cmath_isfinite__doc__,
  670. "isfinite($module, z, /)\n"
  671. "--\n"
  672. "\n"
  673. "Return True if both the real and imaginary parts of z are finite, else False.");
  674. #define CMATH_ISFINITE_METHODDEF \
  675. {"isfinite", (PyCFunction)cmath_isfinite, METH_O, cmath_isfinite__doc__},
  676. static PyObject *
  677. cmath_isfinite_impl(PyObject *module, Py_complex z);
  678. static PyObject *
  679. cmath_isfinite(PyObject *module, PyObject *arg)
  680. {
  681. PyObject *return_value = NULL;
  682. Py_complex z;
  683. z = PyComplex_AsCComplex(arg);
  684. if (PyErr_Occurred()) {
  685. goto exit;
  686. }
  687. return_value = cmath_isfinite_impl(module, z);
  688. exit:
  689. return return_value;
  690. }
  691. PyDoc_STRVAR(cmath_isnan__doc__,
  692. "isnan($module, z, /)\n"
  693. "--\n"
  694. "\n"
  695. "Checks if the real or imaginary part of z not a number (NaN).");
  696. #define CMATH_ISNAN_METHODDEF \
  697. {"isnan", (PyCFunction)cmath_isnan, METH_O, cmath_isnan__doc__},
  698. static PyObject *
  699. cmath_isnan_impl(PyObject *module, Py_complex z);
  700. static PyObject *
  701. cmath_isnan(PyObject *module, PyObject *arg)
  702. {
  703. PyObject *return_value = NULL;
  704. Py_complex z;
  705. z = PyComplex_AsCComplex(arg);
  706. if (PyErr_Occurred()) {
  707. goto exit;
  708. }
  709. return_value = cmath_isnan_impl(module, z);
  710. exit:
  711. return return_value;
  712. }
  713. PyDoc_STRVAR(cmath_isinf__doc__,
  714. "isinf($module, z, /)\n"
  715. "--\n"
  716. "\n"
  717. "Checks if the real or imaginary part of z is infinite.");
  718. #define CMATH_ISINF_METHODDEF \
  719. {"isinf", (PyCFunction)cmath_isinf, METH_O, cmath_isinf__doc__},
  720. static PyObject *
  721. cmath_isinf_impl(PyObject *module, Py_complex z);
  722. static PyObject *
  723. cmath_isinf(PyObject *module, PyObject *arg)
  724. {
  725. PyObject *return_value = NULL;
  726. Py_complex z;
  727. z = PyComplex_AsCComplex(arg);
  728. if (PyErr_Occurred()) {
  729. goto exit;
  730. }
  731. return_value = cmath_isinf_impl(module, z);
  732. exit:
  733. return return_value;
  734. }
  735. PyDoc_STRVAR(cmath_isclose__doc__,
  736. "isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n"
  737. "--\n"
  738. "\n"
  739. "Determine whether two complex numbers are close in value.\n"
  740. "\n"
  741. " rel_tol\n"
  742. " maximum difference for being considered \"close\", relative to the\n"
  743. " magnitude of the input values\n"
  744. " abs_tol\n"
  745. " maximum difference for being considered \"close\", regardless of the\n"
  746. " magnitude of the input values\n"
  747. "\n"
  748. "Return True if a is close in value to b, and False otherwise.\n"
  749. "\n"
  750. "For the values to be considered close, the difference between them must be\n"
  751. "smaller than at least one of the tolerances.\n"
  752. "\n"
  753. "-inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is\n"
  754. "not close to anything, even itself. inf and -inf are only close to themselves.");
  755. #define CMATH_ISCLOSE_METHODDEF \
  756. {"isclose", _PyCFunction_CAST(cmath_isclose), METH_FASTCALL|METH_KEYWORDS, cmath_isclose__doc__},
  757. static int
  758. cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b,
  759. double rel_tol, double abs_tol);
  760. static PyObject *
  761. cmath_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  762. {
  763. PyObject *return_value = NULL;
  764. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  765. #define NUM_KEYWORDS 4
  766. static struct {
  767. PyGC_Head _this_is_not_used;
  768. PyObject_VAR_HEAD
  769. PyObject *ob_item[NUM_KEYWORDS];
  770. } _kwtuple = {
  771. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  772. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('b'), &_Py_ID(rel_tol), &_Py_ID(abs_tol), },
  773. };
  774. #undef NUM_KEYWORDS
  775. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  776. #else // !Py_BUILD_CORE
  777. # define KWTUPLE NULL
  778. #endif // !Py_BUILD_CORE
  779. static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL};
  780. static _PyArg_Parser _parser = {
  781. .keywords = _keywords,
  782. .fname = "isclose",
  783. .kwtuple = KWTUPLE,
  784. };
  785. #undef KWTUPLE
  786. PyObject *argsbuf[4];
  787. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  788. Py_complex a;
  789. Py_complex b;
  790. double rel_tol = 1e-09;
  791. double abs_tol = 0.0;
  792. int _return_value;
  793. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
  794. if (!args) {
  795. goto exit;
  796. }
  797. a = PyComplex_AsCComplex(args[0]);
  798. if (PyErr_Occurred()) {
  799. goto exit;
  800. }
  801. b = PyComplex_AsCComplex(args[1]);
  802. if (PyErr_Occurred()) {
  803. goto exit;
  804. }
  805. if (!noptargs) {
  806. goto skip_optional_kwonly;
  807. }
  808. if (args[2]) {
  809. if (PyFloat_CheckExact(args[2])) {
  810. rel_tol = PyFloat_AS_DOUBLE(args[2]);
  811. }
  812. else
  813. {
  814. rel_tol = PyFloat_AsDouble(args[2]);
  815. if (rel_tol == -1.0 && PyErr_Occurred()) {
  816. goto exit;
  817. }
  818. }
  819. if (!--noptargs) {
  820. goto skip_optional_kwonly;
  821. }
  822. }
  823. if (PyFloat_CheckExact(args[3])) {
  824. abs_tol = PyFloat_AS_DOUBLE(args[3]);
  825. }
  826. else
  827. {
  828. abs_tol = PyFloat_AsDouble(args[3]);
  829. if (abs_tol == -1.0 && PyErr_Occurred()) {
  830. goto exit;
  831. }
  832. }
  833. skip_optional_kwonly:
  834. _return_value = cmath_isclose_impl(module, a, b, rel_tol, abs_tol);
  835. if ((_return_value == -1) && PyErr_Occurred()) {
  836. goto exit;
  837. }
  838. return_value = PyBool_FromLong((long)_return_value);
  839. exit:
  840. return return_value;
  841. }
  842. /*[clinic end generated code: output=a6c9ca48ffe871b6 input=a9049054013a1b77]*/