cmathmodule.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /* Complex math module */
  2. /* much code borrowed from mathmodule.c */
  3. #ifndef Py_BUILD_CORE_BUILTIN
  4. # define Py_BUILD_CORE_MODULE 1
  5. #endif
  6. #include "Python.h"
  7. #include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
  8. /* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from
  9. float.h. We assume that FLT_RADIX is either 2 or 16. */
  10. #include <float.h>
  11. /* For _Py_log1p with workarounds for buggy handling of zeros. */
  12. #include "_math.h"
  13. #include "clinic/cmathmodule.c.h"
  14. /*[clinic input]
  15. module cmath
  16. [clinic start generated code]*/
  17. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=308d6839f4a46333]*/
  18. /*[python input]
  19. class Py_complex_protected_converter(Py_complex_converter):
  20. def modify(self):
  21. return 'errno = 0;'
  22. class Py_complex_protected_return_converter(CReturnConverter):
  23. type = "Py_complex"
  24. def render(self, function, data):
  25. self.declare(data)
  26. data.return_conversion.append("""
  27. if (errno == EDOM) {
  28. PyErr_SetString(PyExc_ValueError, "math domain error");
  29. goto exit;
  30. }
  31. else if (errno == ERANGE) {
  32. PyErr_SetString(PyExc_OverflowError, "math range error");
  33. goto exit;
  34. }
  35. else {
  36. return_value = PyComplex_FromCComplex(_return_value);
  37. }
  38. """.strip())
  39. [python start generated code]*/
  40. /*[python end generated code: output=da39a3ee5e6b4b0d input=8b27adb674c08321]*/
  41. #if (FLT_RADIX != 2 && FLT_RADIX != 16)
  42. #error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16"
  43. #endif
  44. #ifndef M_LN2
  45. #define M_LN2 (0.6931471805599453094) /* natural log of 2 */
  46. #endif
  47. #ifndef M_LN10
  48. #define M_LN10 (2.302585092994045684) /* natural log of 10 */
  49. #endif
  50. /*
  51. CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log,
  52. inverse trig and inverse hyperbolic trig functions. Its log is used in the
  53. evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary
  54. overflow.
  55. */
  56. #define CM_LARGE_DOUBLE (DBL_MAX/4.)
  57. #define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE))
  58. #define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
  59. #define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
  60. /*
  61. CM_SCALE_UP is an odd integer chosen such that multiplication by
  62. 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
  63. CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
  64. square roots accurately when the real and imaginary parts of the argument
  65. are subnormal.
  66. */
  67. #if FLT_RADIX==2
  68. #define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1)
  69. #elif FLT_RADIX==16
  70. #define CM_SCALE_UP (4*DBL_MANT_DIG+1)
  71. #endif
  72. #define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
  73. /* forward declarations */
  74. static Py_complex cmath_asinh_impl(PyObject *, Py_complex);
  75. static Py_complex cmath_atanh_impl(PyObject *, Py_complex);
  76. static Py_complex cmath_cosh_impl(PyObject *, Py_complex);
  77. static Py_complex cmath_sinh_impl(PyObject *, Py_complex);
  78. static Py_complex cmath_sqrt_impl(PyObject *, Py_complex);
  79. static Py_complex cmath_tanh_impl(PyObject *, Py_complex);
  80. static PyObject * math_error(void);
  81. /* Code to deal with special values (infinities, NaNs, etc.). */
  82. /* special_type takes a double and returns an integer code indicating
  83. the type of the double as follows:
  84. */
  85. enum special_types {
  86. ST_NINF, /* 0, negative infinity */
  87. ST_NEG, /* 1, negative finite number (nonzero) */
  88. ST_NZERO, /* 2, -0. */
  89. ST_PZERO, /* 3, +0. */
  90. ST_POS, /* 4, positive finite number (nonzero) */
  91. ST_PINF, /* 5, positive infinity */
  92. ST_NAN /* 6, Not a Number */
  93. };
  94. static enum special_types
  95. special_type(double d)
  96. {
  97. if (Py_IS_FINITE(d)) {
  98. if (d != 0) {
  99. if (copysign(1., d) == 1.)
  100. return ST_POS;
  101. else
  102. return ST_NEG;
  103. }
  104. else {
  105. if (copysign(1., d) == 1.)
  106. return ST_PZERO;
  107. else
  108. return ST_NZERO;
  109. }
  110. }
  111. if (Py_IS_NAN(d))
  112. return ST_NAN;
  113. if (copysign(1., d) == 1.)
  114. return ST_PINF;
  115. else
  116. return ST_NINF;
  117. }
  118. #define SPECIAL_VALUE(z, table) \
  119. if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
  120. errno = 0; \
  121. return table[special_type((z).real)] \
  122. [special_type((z).imag)]; \
  123. }
  124. #define P Py_MATH_PI
  125. #define P14 0.25*Py_MATH_PI
  126. #define P12 0.5*Py_MATH_PI
  127. #define P34 0.75*Py_MATH_PI
  128. #define INF Py_HUGE_VAL
  129. #define N Py_NAN
  130. #define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
  131. /* First, the C functions that do the real work. Each of the c_*
  132. functions computes and returns the C99 Annex G recommended result
  133. and also sets errno as follows: errno = 0 if no floating-point
  134. exception is associated with the result; errno = EDOM if C99 Annex
  135. G recommends raising divide-by-zero or invalid for this result; and
  136. errno = ERANGE where the overflow floating-point signal should be
  137. raised.
  138. */
  139. static Py_complex acos_special_values[7][7];
  140. /*[clinic input]
  141. cmath.acos -> Py_complex_protected
  142. z: Py_complex_protected
  143. /
  144. Return the arc cosine of z.
  145. [clinic start generated code]*/
  146. static Py_complex
  147. cmath_acos_impl(PyObject *module, Py_complex z)
  148. /*[clinic end generated code: output=40bd42853fd460ae input=bd6cbd78ae851927]*/
  149. {
  150. Py_complex s1, s2, r;
  151. SPECIAL_VALUE(z, acos_special_values);
  152. if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
  153. /* avoid unnecessary overflow for large arguments */
  154. r.real = atan2(fabs(z.imag), z.real);
  155. /* split into cases to make sure that the branch cut has the
  156. correct continuity on systems with unsigned zeros */
  157. if (z.real < 0.) {
  158. r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
  159. M_LN2*2., z.imag);
  160. } else {
  161. r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
  162. M_LN2*2., -z.imag);
  163. }
  164. } else {
  165. s1.real = 1.-z.real;
  166. s1.imag = -z.imag;
  167. s1 = cmath_sqrt_impl(module, s1);
  168. s2.real = 1.+z.real;
  169. s2.imag = z.imag;
  170. s2 = cmath_sqrt_impl(module, s2);
  171. r.real = 2.*atan2(s1.real, s2.real);
  172. r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real);
  173. }
  174. errno = 0;
  175. return r;
  176. }
  177. static Py_complex acosh_special_values[7][7];
  178. /*[clinic input]
  179. cmath.acosh = cmath.acos
  180. Return the inverse hyperbolic cosine of z.
  181. [clinic start generated code]*/
  182. static Py_complex
  183. cmath_acosh_impl(PyObject *module, Py_complex z)
  184. /*[clinic end generated code: output=3e2454d4fcf404ca input=3f61bee7d703e53c]*/
  185. {
  186. Py_complex s1, s2, r;
  187. SPECIAL_VALUE(z, acosh_special_values);
  188. if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
  189. /* avoid unnecessary overflow for large arguments */
  190. r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
  191. r.imag = atan2(z.imag, z.real);
  192. } else {
  193. s1.real = z.real - 1.;
  194. s1.imag = z.imag;
  195. s1 = cmath_sqrt_impl(module, s1);
  196. s2.real = z.real + 1.;
  197. s2.imag = z.imag;
  198. s2 = cmath_sqrt_impl(module, s2);
  199. r.real = asinh(s1.real*s2.real + s1.imag*s2.imag);
  200. r.imag = 2.*atan2(s1.imag, s2.real);
  201. }
  202. errno = 0;
  203. return r;
  204. }
  205. /*[clinic input]
  206. cmath.asin = cmath.acos
  207. Return the arc sine of z.
  208. [clinic start generated code]*/
  209. static Py_complex
  210. cmath_asin_impl(PyObject *module, Py_complex z)
  211. /*[clinic end generated code: output=3b264cd1b16bf4e1 input=be0bf0cfdd5239c5]*/
  212. {
  213. /* asin(z) = -i asinh(iz) */
  214. Py_complex s, r;
  215. s.real = -z.imag;
  216. s.imag = z.real;
  217. s = cmath_asinh_impl(module, s);
  218. r.real = s.imag;
  219. r.imag = -s.real;
  220. return r;
  221. }
  222. static Py_complex asinh_special_values[7][7];
  223. /*[clinic input]
  224. cmath.asinh = cmath.acos
  225. Return the inverse hyperbolic sine of z.
  226. [clinic start generated code]*/
  227. static Py_complex
  228. cmath_asinh_impl(PyObject *module, Py_complex z)
  229. /*[clinic end generated code: output=733d8107841a7599 input=5c09448fcfc89a79]*/
  230. {
  231. Py_complex s1, s2, r;
  232. SPECIAL_VALUE(z, asinh_special_values);
  233. if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
  234. if (z.imag >= 0.) {
  235. r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
  236. M_LN2*2., z.real);
  237. } else {
  238. r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
  239. M_LN2*2., -z.real);
  240. }
  241. r.imag = atan2(z.imag, fabs(z.real));
  242. } else {
  243. s1.real = 1.+z.imag;
  244. s1.imag = -z.real;
  245. s1 = cmath_sqrt_impl(module, s1);
  246. s2.real = 1.-z.imag;
  247. s2.imag = z.real;
  248. s2 = cmath_sqrt_impl(module, s2);
  249. r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
  250. r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
  251. }
  252. errno = 0;
  253. return r;
  254. }
  255. /*[clinic input]
  256. cmath.atan = cmath.acos
  257. Return the arc tangent of z.
  258. [clinic start generated code]*/
  259. static Py_complex
  260. cmath_atan_impl(PyObject *module, Py_complex z)
  261. /*[clinic end generated code: output=b6bfc497058acba4 input=3b21ff7d5eac632a]*/
  262. {
  263. /* atan(z) = -i atanh(iz) */
  264. Py_complex s, r;
  265. s.real = -z.imag;
  266. s.imag = z.real;
  267. s = cmath_atanh_impl(module, s);
  268. r.real = s.imag;
  269. r.imag = -s.real;
  270. return r;
  271. }
  272. /* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
  273. C99 for atan2(0., 0.). */
  274. static double
  275. c_atan2(Py_complex z)
  276. {
  277. if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
  278. return Py_NAN;
  279. if (Py_IS_INFINITY(z.imag)) {
  280. if (Py_IS_INFINITY(z.real)) {
  281. if (copysign(1., z.real) == 1.)
  282. /* atan2(+-inf, +inf) == +-pi/4 */
  283. return copysign(0.25*Py_MATH_PI, z.imag);
  284. else
  285. /* atan2(+-inf, -inf) == +-pi*3/4 */
  286. return copysign(0.75*Py_MATH_PI, z.imag);
  287. }
  288. /* atan2(+-inf, x) == +-pi/2 for finite x */
  289. return copysign(0.5*Py_MATH_PI, z.imag);
  290. }
  291. if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
  292. if (copysign(1., z.real) == 1.)
  293. /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
  294. return copysign(0., z.imag);
  295. else
  296. /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
  297. return copysign(Py_MATH_PI, z.imag);
  298. }
  299. return atan2(z.imag, z.real);
  300. }
  301. static Py_complex atanh_special_values[7][7];
  302. /*[clinic input]
  303. cmath.atanh = cmath.acos
  304. Return the inverse hyperbolic tangent of z.
  305. [clinic start generated code]*/
  306. static Py_complex
  307. cmath_atanh_impl(PyObject *module, Py_complex z)
  308. /*[clinic end generated code: output=e83355f93a989c9e input=2b3fdb82fb34487b]*/
  309. {
  310. Py_complex r;
  311. double ay, h;
  312. SPECIAL_VALUE(z, atanh_special_values);
  313. /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
  314. if (z.real < 0.) {
  315. return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z)));
  316. }
  317. ay = fabs(z.imag);
  318. if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
  319. /*
  320. if abs(z) is large then we use the approximation
  321. atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
  322. of z.imag)
  323. */
  324. h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
  325. r.real = z.real/4./h/h;
  326. /* the two negations in the next line cancel each other out
  327. except when working with unsigned zeros: they're there to
  328. ensure that the branch cut has the correct continuity on
  329. systems that don't support signed zeros */
  330. r.imag = -copysign(Py_MATH_PI/2., -z.imag);
  331. errno = 0;
  332. } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
  333. /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
  334. if (ay == 0.) {
  335. r.real = INF;
  336. r.imag = z.imag;
  337. errno = EDOM;
  338. } else {
  339. r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
  340. r.imag = copysign(atan2(2., -ay)/2, z.imag);
  341. errno = 0;
  342. }
  343. } else {
  344. r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
  345. r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
  346. errno = 0;
  347. }
  348. return r;
  349. }
  350. /*[clinic input]
  351. cmath.cos = cmath.acos
  352. Return the cosine of z.
  353. [clinic start generated code]*/
  354. static Py_complex
  355. cmath_cos_impl(PyObject *module, Py_complex z)
  356. /*[clinic end generated code: output=fd64918d5b3186db input=6022e39b77127ac7]*/
  357. {
  358. /* cos(z) = cosh(iz) */
  359. Py_complex r;
  360. r.real = -z.imag;
  361. r.imag = z.real;
  362. r = cmath_cosh_impl(module, r);
  363. return r;
  364. }
  365. /* cosh(infinity + i*y) needs to be dealt with specially */
  366. static Py_complex cosh_special_values[7][7];
  367. /*[clinic input]
  368. cmath.cosh = cmath.acos
  369. Return the hyperbolic cosine of z.
  370. [clinic start generated code]*/
  371. static Py_complex
  372. cmath_cosh_impl(PyObject *module, Py_complex z)
  373. /*[clinic end generated code: output=2e969047da601bdb input=d6b66339e9cc332b]*/
  374. {
  375. Py_complex r;
  376. double x_minus_one;
  377. /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
  378. if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
  379. if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
  380. (z.imag != 0.)) {
  381. if (z.real > 0) {
  382. r.real = copysign(INF, cos(z.imag));
  383. r.imag = copysign(INF, sin(z.imag));
  384. }
  385. else {
  386. r.real = copysign(INF, cos(z.imag));
  387. r.imag = -copysign(INF, sin(z.imag));
  388. }
  389. }
  390. else {
  391. r = cosh_special_values[special_type(z.real)]
  392. [special_type(z.imag)];
  393. }
  394. /* need to set errno = EDOM if y is +/- infinity and x is not
  395. a NaN */
  396. if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
  397. errno = EDOM;
  398. else
  399. errno = 0;
  400. return r;
  401. }
  402. if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
  403. /* deal correctly with cases where cosh(z.real) overflows but
  404. cosh(z) does not. */
  405. x_minus_one = z.real - copysign(1., z.real);
  406. r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
  407. r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
  408. } else {
  409. r.real = cos(z.imag) * cosh(z.real);
  410. r.imag = sin(z.imag) * sinh(z.real);
  411. }
  412. /* detect overflow, and set errno accordingly */
  413. if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
  414. errno = ERANGE;
  415. else
  416. errno = 0;
  417. return r;
  418. }
  419. /* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for
  420. finite y */
  421. static Py_complex exp_special_values[7][7];
  422. /*[clinic input]
  423. cmath.exp = cmath.acos
  424. Return the exponential value e**z.
  425. [clinic start generated code]*/
  426. static Py_complex
  427. cmath_exp_impl(PyObject *module, Py_complex z)
  428. /*[clinic end generated code: output=edcec61fb9dfda6c input=8b9e6cf8a92174c3]*/
  429. {
  430. Py_complex r;
  431. double l;
  432. if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
  433. if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
  434. && (z.imag != 0.)) {
  435. if (z.real > 0) {
  436. r.real = copysign(INF, cos(z.imag));
  437. r.imag = copysign(INF, sin(z.imag));
  438. }
  439. else {
  440. r.real = copysign(0., cos(z.imag));
  441. r.imag = copysign(0., sin(z.imag));
  442. }
  443. }
  444. else {
  445. r = exp_special_values[special_type(z.real)]
  446. [special_type(z.imag)];
  447. }
  448. /* need to set errno = EDOM if y is +/- infinity and x is not
  449. a NaN and not -infinity */
  450. if (Py_IS_INFINITY(z.imag) &&
  451. (Py_IS_FINITE(z.real) ||
  452. (Py_IS_INFINITY(z.real) && z.real > 0)))
  453. errno = EDOM;
  454. else
  455. errno = 0;
  456. return r;
  457. }
  458. if (z.real > CM_LOG_LARGE_DOUBLE) {
  459. l = exp(z.real-1.);
  460. r.real = l*cos(z.imag)*Py_MATH_E;
  461. r.imag = l*sin(z.imag)*Py_MATH_E;
  462. } else {
  463. l = exp(z.real);
  464. r.real = l*cos(z.imag);
  465. r.imag = l*sin(z.imag);
  466. }
  467. /* detect overflow, and set errno accordingly */
  468. if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
  469. errno = ERANGE;
  470. else
  471. errno = 0;
  472. return r;
  473. }
  474. static Py_complex log_special_values[7][7];
  475. static Py_complex
  476. c_log(Py_complex z)
  477. {
  478. /*
  479. The usual formula for the real part is log(hypot(z.real, z.imag)).
  480. There are four situations where this formula is potentially
  481. problematic:
  482. (1) the absolute value of z is subnormal. Then hypot is subnormal,
  483. so has fewer than the usual number of bits of accuracy, hence may
  484. have large relative error. This then gives a large absolute error
  485. in the log. This can be solved by rescaling z by a suitable power
  486. of 2.
  487. (2) the absolute value of z is greater than DBL_MAX (e.g. when both
  488. z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
  489. Again, rescaling solves this.
  490. (3) the absolute value of z is close to 1. In this case it's
  491. difficult to achieve good accuracy, at least in part because a
  492. change of 1ulp in the real or imaginary part of z can result in a
  493. change of billions of ulps in the correctly rounded answer.
  494. (4) z = 0. The simplest thing to do here is to call the
  495. floating-point log with an argument of 0, and let its behaviour
  496. (returning -infinity, signaling a floating-point exception, setting
  497. errno, or whatever) determine that of c_log. So the usual formula
  498. is fine here.
  499. */
  500. Py_complex r;
  501. double ax, ay, am, an, h;
  502. SPECIAL_VALUE(z, log_special_values);
  503. ax = fabs(z.real);
  504. ay = fabs(z.imag);
  505. if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
  506. r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
  507. } else if (ax < DBL_MIN && ay < DBL_MIN) {
  508. if (ax > 0. || ay > 0.) {
  509. /* catch cases where hypot(ax, ay) is subnormal */
  510. r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
  511. ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
  512. }
  513. else {
  514. /* log(+/-0. +/- 0i) */
  515. r.real = -INF;
  516. r.imag = atan2(z.imag, z.real);
  517. errno = EDOM;
  518. return r;
  519. }
  520. } else {
  521. h = hypot(ax, ay);
  522. if (0.71 <= h && h <= 1.73) {
  523. am = ax > ay ? ax : ay; /* max(ax, ay) */
  524. an = ax > ay ? ay : ax; /* min(ax, ay) */
  525. r.real = m_log1p((am-1)*(am+1)+an*an)/2.;
  526. } else {
  527. r.real = log(h);
  528. }
  529. }
  530. r.imag = atan2(z.imag, z.real);
  531. errno = 0;
  532. return r;
  533. }
  534. /*[clinic input]
  535. cmath.log10 = cmath.acos
  536. Return the base-10 logarithm of z.
  537. [clinic start generated code]*/
  538. static Py_complex
  539. cmath_log10_impl(PyObject *module, Py_complex z)
  540. /*[clinic end generated code: output=2922779a7c38cbe1 input=cff5644f73c1519c]*/
  541. {
  542. Py_complex r;
  543. int errno_save;
  544. r = c_log(z);
  545. errno_save = errno; /* just in case the divisions affect errno */
  546. r.real = r.real / M_LN10;
  547. r.imag = r.imag / M_LN10;
  548. errno = errno_save;
  549. return r;
  550. }
  551. /*[clinic input]
  552. cmath.sin = cmath.acos
  553. Return the sine of z.
  554. [clinic start generated code]*/
  555. static Py_complex
  556. cmath_sin_impl(PyObject *module, Py_complex z)
  557. /*[clinic end generated code: output=980370d2ff0bb5aa input=2d3519842a8b4b85]*/
  558. {
  559. /* sin(z) = -i sin(iz) */
  560. Py_complex s, r;
  561. s.real = -z.imag;
  562. s.imag = z.real;
  563. s = cmath_sinh_impl(module, s);
  564. r.real = s.imag;
  565. r.imag = -s.real;
  566. return r;
  567. }
  568. /* sinh(infinity + i*y) needs to be dealt with specially */
  569. static Py_complex sinh_special_values[7][7];
  570. /*[clinic input]
  571. cmath.sinh = cmath.acos
  572. Return the hyperbolic sine of z.
  573. [clinic start generated code]*/
  574. static Py_complex
  575. cmath_sinh_impl(PyObject *module, Py_complex z)
  576. /*[clinic end generated code: output=38b0a6cce26f3536 input=d2d3fc8c1ddfd2dd]*/
  577. {
  578. Py_complex r;
  579. double x_minus_one;
  580. /* special treatment for sinh(+/-inf + iy) if y is finite and
  581. nonzero */
  582. if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
  583. if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
  584. && (z.imag != 0.)) {
  585. if (z.real > 0) {
  586. r.real = copysign(INF, cos(z.imag));
  587. r.imag = copysign(INF, sin(z.imag));
  588. }
  589. else {
  590. r.real = -copysign(INF, cos(z.imag));
  591. r.imag = copysign(INF, sin(z.imag));
  592. }
  593. }
  594. else {
  595. r = sinh_special_values[special_type(z.real)]
  596. [special_type(z.imag)];
  597. }
  598. /* need to set errno = EDOM if y is +/- infinity and x is not
  599. a NaN */
  600. if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
  601. errno = EDOM;
  602. else
  603. errno = 0;
  604. return r;
  605. }
  606. if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
  607. x_minus_one = z.real - copysign(1., z.real);
  608. r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
  609. r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
  610. } else {
  611. r.real = cos(z.imag) * sinh(z.real);
  612. r.imag = sin(z.imag) * cosh(z.real);
  613. }
  614. /* detect overflow, and set errno accordingly */
  615. if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
  616. errno = ERANGE;
  617. else
  618. errno = 0;
  619. return r;
  620. }
  621. static Py_complex sqrt_special_values[7][7];
  622. /*[clinic input]
  623. cmath.sqrt = cmath.acos
  624. Return the square root of z.
  625. [clinic start generated code]*/
  626. static Py_complex
  627. cmath_sqrt_impl(PyObject *module, Py_complex z)
  628. /*[clinic end generated code: output=b6507b3029c339fc input=7088b166fc9a58c7]*/
  629. {
  630. /*
  631. Method: use symmetries to reduce to the case when x = z.real and y
  632. = z.imag are nonnegative. Then the real part of the result is
  633. given by
  634. s = sqrt((x + hypot(x, y))/2)
  635. and the imaginary part is
  636. d = (y/2)/s
  637. If either x or y is very large then there's a risk of overflow in
  638. computation of the expression x + hypot(x, y). We can avoid this
  639. by rewriting the formula for s as:
  640. s = 2*sqrt(x/8 + hypot(x/8, y/8))
  641. This costs us two extra multiplications/divisions, but avoids the
  642. overhead of checking for x and y large.
  643. If both x and y are subnormal then hypot(x, y) may also be
  644. subnormal, so will lack full precision. We solve this by rescaling
  645. x and y by a sufficiently large power of 2 to ensure that x and y
  646. are normal.
  647. */
  648. Py_complex r;
  649. double s,d;
  650. double ax, ay;
  651. SPECIAL_VALUE(z, sqrt_special_values);
  652. if (z.real == 0. && z.imag == 0.) {
  653. r.real = 0.;
  654. r.imag = z.imag;
  655. return r;
  656. }
  657. ax = fabs(z.real);
  658. ay = fabs(z.imag);
  659. if (ax < DBL_MIN && ay < DBL_MIN) {
  660. /* here we catch cases where hypot(ax, ay) is subnormal */
  661. ax = ldexp(ax, CM_SCALE_UP);
  662. s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
  663. CM_SCALE_DOWN);
  664. } else {
  665. ax /= 8.;
  666. s = 2.*sqrt(ax + hypot(ax, ay/8.));
  667. }
  668. d = ay/(2.*s);
  669. if (z.real >= 0.) {
  670. r.real = s;
  671. r.imag = copysign(d, z.imag);
  672. } else {
  673. r.real = d;
  674. r.imag = copysign(s, z.imag);
  675. }
  676. errno = 0;
  677. return r;
  678. }
  679. /*[clinic input]
  680. cmath.tan = cmath.acos
  681. Return the tangent of z.
  682. [clinic start generated code]*/
  683. static Py_complex
  684. cmath_tan_impl(PyObject *module, Py_complex z)
  685. /*[clinic end generated code: output=7c5f13158a72eb13 input=fc167e528767888e]*/
  686. {
  687. /* tan(z) = -i tanh(iz) */
  688. Py_complex s, r;
  689. s.real = -z.imag;
  690. s.imag = z.real;
  691. s = cmath_tanh_impl(module, s);
  692. r.real = s.imag;
  693. r.imag = -s.real;
  694. return r;
  695. }
  696. /* tanh(infinity + i*y) needs to be dealt with specially */
  697. static Py_complex tanh_special_values[7][7];
  698. /*[clinic input]
  699. cmath.tanh = cmath.acos
  700. Return the hyperbolic tangent of z.
  701. [clinic start generated code]*/
  702. static Py_complex
  703. cmath_tanh_impl(PyObject *module, Py_complex z)
  704. /*[clinic end generated code: output=36d547ef7aca116c input=22f67f9dc6d29685]*/
  705. {
  706. /* Formula:
  707. tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
  708. (1+tan(y)^2 tanh(x)^2)
  709. To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
  710. as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
  711. by 4 exp(-2*x) instead, to avoid possible overflow in the
  712. computation of cosh(x).
  713. */
  714. Py_complex r;
  715. double tx, ty, cx, txty, denom;
  716. /* special treatment for tanh(+/-inf + iy) if y is finite and
  717. nonzero */
  718. if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
  719. if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
  720. && (z.imag != 0.)) {
  721. if (z.real > 0) {
  722. r.real = 1.0;
  723. r.imag = copysign(0.,
  724. 2.*sin(z.imag)*cos(z.imag));
  725. }
  726. else {
  727. r.real = -1.0;
  728. r.imag = copysign(0.,
  729. 2.*sin(z.imag)*cos(z.imag));
  730. }
  731. }
  732. else {
  733. r = tanh_special_values[special_type(z.real)]
  734. [special_type(z.imag)];
  735. }
  736. /* need to set errno = EDOM if z.imag is +/-infinity and
  737. z.real is finite */
  738. if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
  739. errno = EDOM;
  740. else
  741. errno = 0;
  742. return r;
  743. }
  744. /* danger of overflow in 2.*z.imag !*/
  745. if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
  746. r.real = copysign(1., z.real);
  747. r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
  748. } else {
  749. tx = tanh(z.real);
  750. ty = tan(z.imag);
  751. cx = 1./cosh(z.real);
  752. txty = tx*ty;
  753. denom = 1. + txty*txty;
  754. r.real = tx*(1.+ty*ty)/denom;
  755. r.imag = ((ty/denom)*cx)*cx;
  756. }
  757. errno = 0;
  758. return r;
  759. }
  760. /*[clinic input]
  761. cmath.log
  762. z as x: Py_complex
  763. base as y_obj: object = NULL
  764. /
  765. log(z[, base]) -> the logarithm of z to the given base.
  766. If the base is not specified, returns the natural logarithm (base e) of z.
  767. [clinic start generated code]*/
  768. static PyObject *
  769. cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj)
  770. /*[clinic end generated code: output=4effdb7d258e0d94 input=e1f81d4fcfd26497]*/
  771. {
  772. Py_complex y;
  773. errno = 0;
  774. x = c_log(x);
  775. if (y_obj != NULL) {
  776. y = PyComplex_AsCComplex(y_obj);
  777. if (PyErr_Occurred()) {
  778. return NULL;
  779. }
  780. y = c_log(y);
  781. x = _Py_c_quot(x, y);
  782. }
  783. if (errno != 0)
  784. return math_error();
  785. return PyComplex_FromCComplex(x);
  786. }
  787. /* And now the glue to make them available from Python: */
  788. static PyObject *
  789. math_error(void)
  790. {
  791. if (errno == EDOM)
  792. PyErr_SetString(PyExc_ValueError, "math domain error");
  793. else if (errno == ERANGE)
  794. PyErr_SetString(PyExc_OverflowError, "math range error");
  795. else /* Unexpected math error */
  796. PyErr_SetFromErrno(PyExc_ValueError);
  797. return NULL;
  798. }
  799. /*[clinic input]
  800. cmath.phase
  801. z: Py_complex
  802. /
  803. Return argument, also known as the phase angle, of a complex.
  804. [clinic start generated code]*/
  805. static PyObject *
  806. cmath_phase_impl(PyObject *module, Py_complex z)
  807. /*[clinic end generated code: output=50725086a7bfd253 input=5cf75228ba94b69d]*/
  808. {
  809. double phi;
  810. errno = 0;
  811. phi = c_atan2(z); /* should not cause any exception */
  812. if (errno != 0)
  813. return math_error();
  814. else
  815. return PyFloat_FromDouble(phi);
  816. }
  817. /*[clinic input]
  818. cmath.polar
  819. z: Py_complex
  820. /
  821. Convert a complex from rectangular coordinates to polar coordinates.
  822. r is the distance from 0 and phi the phase angle.
  823. [clinic start generated code]*/
  824. static PyObject *
  825. cmath_polar_impl(PyObject *module, Py_complex z)
  826. /*[clinic end generated code: output=d0a8147c41dbb654 input=26c353574fd1a861]*/
  827. {
  828. double r, phi;
  829. errno = 0;
  830. phi = c_atan2(z); /* should not cause any exception */
  831. r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
  832. if (errno != 0)
  833. return math_error();
  834. else
  835. return Py_BuildValue("dd", r, phi);
  836. }
  837. /*
  838. rect() isn't covered by the C99 standard, but it's not too hard to
  839. figure out 'spirit of C99' rules for special value handing:
  840. rect(x, t) should behave like exp(log(x) + it) for positive-signed x
  841. rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x
  842. rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0)
  843. gives nan +- i0 with the sign of the imaginary part unspecified.
  844. */
  845. static Py_complex rect_special_values[7][7];
  846. /*[clinic input]
  847. cmath.rect
  848. r: double
  849. phi: double
  850. /
  851. Convert from polar coordinates to rectangular coordinates.
  852. [clinic start generated code]*/
  853. static PyObject *
  854. cmath_rect_impl(PyObject *module, double r, double phi)
  855. /*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/
  856. {
  857. Py_complex z;
  858. errno = 0;
  859. /* deal with special values */
  860. if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
  861. /* if r is +/-infinity and phi is finite but nonzero then
  862. result is (+-INF +-INF i), but we need to compute cos(phi)
  863. and sin(phi) to figure out the signs. */
  864. if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
  865. && (phi != 0.))) {
  866. if (r > 0) {
  867. z.real = copysign(INF, cos(phi));
  868. z.imag = copysign(INF, sin(phi));
  869. }
  870. else {
  871. z.real = -copysign(INF, cos(phi));
  872. z.imag = -copysign(INF, sin(phi));
  873. }
  874. }
  875. else {
  876. z = rect_special_values[special_type(r)]
  877. [special_type(phi)];
  878. }
  879. /* need to set errno = EDOM if r is a nonzero number and phi
  880. is infinite */
  881. if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
  882. errno = EDOM;
  883. else
  884. errno = 0;
  885. }
  886. else if (phi == 0.0) {
  887. /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See
  888. bugs.python.org/issue18513. */
  889. z.real = r;
  890. z.imag = r * phi;
  891. errno = 0;
  892. }
  893. else {
  894. z.real = r * cos(phi);
  895. z.imag = r * sin(phi);
  896. errno = 0;
  897. }
  898. if (errno != 0)
  899. return math_error();
  900. else
  901. return PyComplex_FromCComplex(z);
  902. }
  903. /*[clinic input]
  904. cmath.isfinite = cmath.polar
  905. Return True if both the real and imaginary parts of z are finite, else False.
  906. [clinic start generated code]*/
  907. static PyObject *
  908. cmath_isfinite_impl(PyObject *module, Py_complex z)
  909. /*[clinic end generated code: output=ac76611e2c774a36 input=848e7ee701895815]*/
  910. {
  911. return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
  912. }
  913. /*[clinic input]
  914. cmath.isnan = cmath.polar
  915. Checks if the real or imaginary part of z not a number (NaN).
  916. [clinic start generated code]*/
  917. static PyObject *
  918. cmath_isnan_impl(PyObject *module, Py_complex z)
  919. /*[clinic end generated code: output=e7abf6e0b28beab7 input=71799f5d284c9baf]*/
  920. {
  921. return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
  922. }
  923. /*[clinic input]
  924. cmath.isinf = cmath.polar
  925. Checks if the real or imaginary part of z is infinite.
  926. [clinic start generated code]*/
  927. static PyObject *
  928. cmath_isinf_impl(PyObject *module, Py_complex z)
  929. /*[clinic end generated code: output=502a75a79c773469 input=363df155c7181329]*/
  930. {
  931. return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
  932. Py_IS_INFINITY(z.imag));
  933. }
  934. /*[clinic input]
  935. cmath.isclose -> bool
  936. a: Py_complex
  937. b: Py_complex
  938. *
  939. rel_tol: double = 1e-09
  940. maximum difference for being considered "close", relative to the
  941. magnitude of the input values
  942. abs_tol: double = 0.0
  943. maximum difference for being considered "close", regardless of the
  944. magnitude of the input values
  945. Determine whether two complex numbers are close in value.
  946. Return True if a is close in value to b, and False otherwise.
  947. For the values to be considered close, the difference between them must be
  948. smaller than at least one of the tolerances.
  949. -inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is
  950. not close to anything, even itself. inf and -inf are only close to themselves.
  951. [clinic start generated code]*/
  952. static int
  953. cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b,
  954. double rel_tol, double abs_tol)
  955. /*[clinic end generated code: output=8a2486cc6e0014d1 input=df9636d7de1d4ac3]*/
  956. {
  957. double diff;
  958. /* sanity check on the inputs */
  959. if (rel_tol < 0.0 || abs_tol < 0.0 ) {
  960. PyErr_SetString(PyExc_ValueError,
  961. "tolerances must be non-negative");
  962. return -1;
  963. }
  964. if ( (a.real == b.real) && (a.imag == b.imag) ) {
  965. /* short circuit exact equality -- needed to catch two infinities of
  966. the same sign. And perhaps speeds things up a bit sometimes.
  967. */
  968. return 1;
  969. }
  970. /* This catches the case of two infinities of opposite sign, or
  971. one infinity and one finite number. Two infinities of opposite
  972. sign would otherwise have an infinite relative tolerance.
  973. Two infinities of the same sign are caught by the equality check
  974. above.
  975. */
  976. if (Py_IS_INFINITY(a.real) || Py_IS_INFINITY(a.imag) ||
  977. Py_IS_INFINITY(b.real) || Py_IS_INFINITY(b.imag)) {
  978. return 0;
  979. }
  980. /* now do the regular computation
  981. this is essentially the "weak" test from the Boost library
  982. */
  983. diff = _Py_c_abs(_Py_c_diff(a, b));
  984. return (((diff <= rel_tol * _Py_c_abs(b)) ||
  985. (diff <= rel_tol * _Py_c_abs(a))) ||
  986. (diff <= abs_tol));
  987. }
  988. PyDoc_STRVAR(module_doc,
  989. "This module provides access to mathematical functions for complex\n"
  990. "numbers.");
  991. static PyMethodDef cmath_methods[] = {
  992. CMATH_ACOS_METHODDEF
  993. CMATH_ACOSH_METHODDEF
  994. CMATH_ASIN_METHODDEF
  995. CMATH_ASINH_METHODDEF
  996. CMATH_ATAN_METHODDEF
  997. CMATH_ATANH_METHODDEF
  998. CMATH_COS_METHODDEF
  999. CMATH_COSH_METHODDEF
  1000. CMATH_EXP_METHODDEF
  1001. CMATH_ISCLOSE_METHODDEF
  1002. CMATH_ISFINITE_METHODDEF
  1003. CMATH_ISINF_METHODDEF
  1004. CMATH_ISNAN_METHODDEF
  1005. CMATH_LOG_METHODDEF
  1006. CMATH_LOG10_METHODDEF
  1007. CMATH_PHASE_METHODDEF
  1008. CMATH_POLAR_METHODDEF
  1009. CMATH_RECT_METHODDEF
  1010. CMATH_SIN_METHODDEF
  1011. CMATH_SINH_METHODDEF
  1012. CMATH_SQRT_METHODDEF
  1013. CMATH_TAN_METHODDEF
  1014. CMATH_TANH_METHODDEF
  1015. {NULL, NULL} /* sentinel */
  1016. };
  1017. static int
  1018. cmath_exec(PyObject *mod)
  1019. {
  1020. if (_PyModule_Add(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
  1021. return -1;
  1022. }
  1023. if (_PyModule_Add(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
  1024. return -1;
  1025. }
  1026. // 2pi
  1027. if (_PyModule_Add(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
  1028. return -1;
  1029. }
  1030. if (_PyModule_Add(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
  1031. return -1;
  1032. }
  1033. Py_complex infj = {0.0, Py_INFINITY};
  1034. if (_PyModule_Add(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
  1035. return -1;
  1036. }
  1037. if (_PyModule_Add(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
  1038. return -1;
  1039. }
  1040. Py_complex nanj = {0.0, fabs(Py_NAN)};
  1041. if (_PyModule_Add(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
  1042. return -1;
  1043. }
  1044. /* initialize special value tables */
  1045. #define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
  1046. #define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
  1047. INIT_SPECIAL_VALUES(acos_special_values, {
  1048. C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
  1049. C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
  1050. C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
  1051. C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
  1052. C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
  1053. C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
  1054. C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
  1055. })
  1056. INIT_SPECIAL_VALUES(acosh_special_values, {
  1057. C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
  1058. C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
  1059. C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
  1060. C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
  1061. C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
  1062. C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
  1063. C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
  1064. })
  1065. INIT_SPECIAL_VALUES(asinh_special_values, {
  1066. C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
  1067. C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
  1068. C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
  1069. C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
  1070. C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
  1071. C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
  1072. C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
  1073. })
  1074. INIT_SPECIAL_VALUES(atanh_special_values, {
  1075. C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
  1076. C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
  1077. C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
  1078. C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
  1079. C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
  1080. C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
  1081. C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
  1082. })
  1083. INIT_SPECIAL_VALUES(cosh_special_values, {
  1084. C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
  1085. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1086. C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
  1087. C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
  1088. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1089. C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
  1090. C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
  1091. })
  1092. INIT_SPECIAL_VALUES(exp_special_values, {
  1093. C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
  1094. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1095. C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
  1096. C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
  1097. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1098. C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
  1099. C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
  1100. })
  1101. INIT_SPECIAL_VALUES(log_special_values, {
  1102. C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
  1103. C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
  1104. C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
  1105. C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
  1106. C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
  1107. C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
  1108. C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
  1109. })
  1110. INIT_SPECIAL_VALUES(sinh_special_values, {
  1111. C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
  1112. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1113. C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
  1114. C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
  1115. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1116. C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
  1117. C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
  1118. })
  1119. INIT_SPECIAL_VALUES(sqrt_special_values, {
  1120. C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
  1121. C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
  1122. C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
  1123. C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
  1124. C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
  1125. C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
  1126. C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
  1127. })
  1128. INIT_SPECIAL_VALUES(tanh_special_values, {
  1129. C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
  1130. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1131. C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
  1132. C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
  1133. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1134. C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
  1135. C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
  1136. })
  1137. INIT_SPECIAL_VALUES(rect_special_values, {
  1138. C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
  1139. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1140. C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
  1141. C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
  1142. C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
  1143. C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
  1144. C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
  1145. })
  1146. return 0;
  1147. }
  1148. static PyModuleDef_Slot cmath_slots[] = {
  1149. {Py_mod_exec, cmath_exec},
  1150. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  1151. {0, NULL}
  1152. };
  1153. static struct PyModuleDef cmathmodule = {
  1154. PyModuleDef_HEAD_INIT,
  1155. .m_name = "cmath",
  1156. .m_doc = module_doc,
  1157. .m_size = 0,
  1158. .m_methods = cmath_methods,
  1159. .m_slots = cmath_slots
  1160. };
  1161. PyMODINIT_FUNC
  1162. PyInit_cmath(void)
  1163. {
  1164. return PyModuleDef_Init(&cmathmodule);
  1165. }