/src/gmpy_mpfr.c

http://gmpy.googlecode.com/ · C · 2919 lines · 2354 code · 492 blank · 73 comment · 356 complexity · dc9c4e536aaf2cff3e22b6ad4dd1a020 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * gmpy_mpfr.c *
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision *
  5. * libraries. *
  6. * *
  7. * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
  8. * 2008, 2009 Alex Martelli *
  9. * *
  10. * Copyright 2008, 2009, 2010, 2011, 2012, 2013 Case Van Horsen *
  11. * *
  12. * This file is part of GMPY2. *
  13. * *
  14. * GMPY2 is free software: you can redistribute it and/or modify it under *
  15. * the terms of the GNU Lesser General Public License as published by the *
  16. * Free Software Foundation, either version 3 of the License, or (at your *
  17. * option) any later version. *
  18. * *
  19. * GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
  22. * License for more details. *
  23. * *
  24. * You should have received a copy of the GNU Lesser General Public *
  25. * License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
  26. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  27. PyDoc_STRVAR(doc_g_mpfr_f2q,
  28. "f2q(x,[err]) -> mpq\n\n"
  29. "Return the 'best' mpq approximating x to within relative error 'err'.\n"
  30. "Default is the precision of x. Uses Stern-Brocot tree to find the\n"
  31. "'best' approximation. An 'mpz' is returned if the the denominator\n"
  32. "is 1. If 'err'<0, error sought is 2.0 ** err.");
  33. static PyObject *
  34. Pympfr_f2q(PyObject *self, PyObject *args)
  35. {
  36. PympfrObject *err = 0;
  37. PyObject *result;
  38. if (!PyArg_ParseTuple(args, "O&|O&", Pympfr_convert_arg, &self,
  39. Pympfr_convert_arg, &err)) {
  40. TYPE_ERROR("f2q() requires 'mpfr', ['mpfr'] arguments");
  41. return NULL;
  42. }
  43. result = (PyObject*)stern_brocot((PympfrObject*)self, err, 0, 1);
  44. Py_DECREF(self);
  45. Py_XDECREF((PyObject*)err);
  46. return result;
  47. }
  48. PyDoc_STRVAR(doc_mpfr,
  49. "mpfr() -> mpfr(0.0)\n\n"
  50. " If no argument is given, return mpfr(0.0).\n\n"
  51. "mpfr(n[, precison=0]) -> mpfr\n\n"
  52. " Return an 'mpfr' object after converting a numeric value. If\n"
  53. " no precision, or a precision of 0, is specified; the precison\n"
  54. " is taken from the current context.\n\n"
  55. "mpfr(s[, precision=0[, [base=0]]) -> mpfr\n\n"
  56. " Return 'mpfr' object after converting a string 's' made up of\n"
  57. " digits in the given base, possibly with fraction-part (with\n"
  58. " period as a separator) and/or exponent-part (with exponent\n"
  59. " marker 'e' for base<=10, else '@'). If no precision, or a\n"
  60. " precision of 0, is specified; the precison is taken from the\n"
  61. " current context. The base of the string representation must\n"
  62. " be 0 or in the interval 2 ... 62. If the base is 0, the leading\n"
  63. " digits of the string are used to identify the base: 0b implies\n"
  64. " base=2, 0x implies base=16, otherwise base=10 is assumed.\n");
  65. static PyObject *
  66. Pygmpy_mpfr(PyObject *self, PyObject *args, PyObject *keywds)
  67. {
  68. PympfrObject *result = 0;
  69. PyObject *arg0;
  70. int base = 0;
  71. Py_ssize_t argc;
  72. /* Assumes mpfr_prec_t is the same as a long. */
  73. mpfr_prec_t bits = 0;
  74. static char *kwlist_s[] = {"s", "precision", "base", NULL};
  75. static char *kwlist_n[] = {"n", "precision", NULL};
  76. argc = PyTuple_Size(args);
  77. if ((argc < 0) || (argc > 3)) {
  78. TYPE_ERROR("mpfr() requires 0 to 3 arguments");
  79. return NULL;
  80. }
  81. if (argc == 0) {
  82. if ((result = (PympfrObject*)Pympfr_new(0))) {
  83. mpfr_set_ui(result->f, 0, context->ctx.mpfr_round);
  84. }
  85. return (PyObject*)result;
  86. }
  87. arg0 = PyTuple_GetItem(args, 0);
  88. if (PyStrOrUnicode_Check(arg0)) {
  89. /* Can have both precision and/or base as keyword arguments. */
  90. if (PyArg_ParseTupleAndKeywords(args, keywds, "O|li", kwlist_s,
  91. &arg0, &bits, &base)) {
  92. if ((base!=0) && ((base<2)||(base>62))) {
  93. VALUE_ERROR("base for mpfr() must be 0 or in the "
  94. "interval 2 ... 62");
  95. }
  96. else if (bits < 0) {
  97. VALUE_ERROR("precision for mpfr() must be >= 0");
  98. }
  99. else {
  100. result = Pympfr_From_PyStr(arg0, base, bits);
  101. }
  102. }
  103. SUBNORMALIZE(result);
  104. return (PyObject*)result;
  105. }
  106. /* Optimize the common case */
  107. if (isReal(arg0) && argc == 1 && !keywds) {
  108. result = Pympfr_From_Real(arg0, bits);
  109. SUBNORMALIZE(result);
  110. return (PyObject*)result;
  111. }
  112. /* Can only have precision as keyword argument. */
  113. if (PyArg_ParseTupleAndKeywords(args, keywds, "O|l", kwlist_n, &arg0, &bits)) {
  114. if (bits < 0) {
  115. VALUE_ERROR("precision for mpfr() must be >= 0");
  116. }
  117. else {
  118. result = Pympfr_From_Real(arg0, bits);
  119. if (!result)
  120. TYPE_ERROR("mpfr() requires numeric or string argument");
  121. }
  122. }
  123. return (PyObject*)result;
  124. }
  125. /* Implement the .precision attribute of an mpfr. */
  126. static PyObject *
  127. Pympfr_getprec_attrib(PympfrObject *self, void *closure)
  128. {
  129. return PyIntOrLong_FromSsize_t((Py_ssize_t)mpfr_get_prec(self->f));
  130. }
  131. /* Implement the .rc attribute of an mpfr. */
  132. static PyObject *
  133. Pympfr_getrc_attrib(PympfrObject *self, void *closure)
  134. {
  135. return PyIntOrLong_FromLong((long)self->rc);
  136. }
  137. /* Implement the .imag attribute of an mpfr. */
  138. static PyObject *
  139. Pympfr_getimag_attrib(PympfrObject *self, void *closure)
  140. {
  141. PympfrObject *result;
  142. if ((result = (PympfrObject*)Pympfr_new(0)))
  143. mpfr_set_zero(result->f, 1);
  144. return (PyObject*)result;
  145. }
  146. /* Implement the .real attribute of an mpfr. */
  147. static PyObject *
  148. Pympfr_getreal_attrib(PympfrObject *self, void *closure)
  149. {
  150. return (PyObject*)Pympfr_From_Pympfr((PyObject*)self, 0);
  151. }
  152. /* Implement the nb_bool slot. */
  153. static int
  154. Pympfr_nonzero(PympfrObject *self)
  155. {
  156. return !mpfr_zero_p(self->f);
  157. }
  158. /* Implement the conjugate() method. */
  159. PyDoc_STRVAR(doc_mpfr_conjugate,
  160. "x.conjugate() -> mpfr\n\n"
  161. "Return the conjugate of x (which is just a copy of x since x is\n"
  162. "not a complex number).");
  163. static PyObject *
  164. Pympfr_conjugate(PyObject *self, PyObject *args)
  165. {
  166. return (PyObject*)Pympfr_From_Pympfr(self, 0);
  167. }
  168. /* Implement the nb_positive slot. */
  169. /* TODO: can probably just call Pympfr_From_Real. */
  170. static PyObject *
  171. Pympfr_pos(PympfrObject *self)
  172. {
  173. PympfrObject *result;
  174. if (!(result = (PympfrObject*)Pympfr_new(mpfr_get_prec(self->f))))
  175. return NULL;
  176. mpfr_clear_flags();
  177. /* Since result has the same precision as self, no rounding occurs. */
  178. mpfr_set(result->f, self->f, context->ctx.mpfr_round);
  179. result->round_mode = self->round_mode;
  180. result->rc = self->rc;
  181. /* Force the exponents to be valid. */
  182. result->rc = mpfr_check_range(result->f, result->rc, result->round_mode);
  183. /* Now round result to the current precision. */
  184. result->rc = mpfr_prec_round(result->f, context->ctx.mpfr_prec,
  185. context->ctx.mpfr_round);
  186. SUBNORMALIZE(result);
  187. MERGE_FLAGS;
  188. CHECK_FLAGS("__pos__");
  189. done:
  190. if (PyErr_Occurred()) {
  191. Py_XDECREF((PyObject*)result);
  192. result = NULL;
  193. }
  194. return (PyObject*)result;
  195. }
  196. PyDoc_STRVAR(doc_g_mpfr_get_emin_min,
  197. "get_emin_min() -> integer\n\n"
  198. "Return the minimum possible exponent that can be set for 'mpfr'.");
  199. static PyObject *
  200. Pympfr_get_emin_min(PyObject *self, PyObject *args)
  201. {
  202. return PyIntOrLong_FromSsize_t((Py_ssize_t)mpfr_get_emin_min());
  203. }
  204. PyDoc_STRVAR(doc_g_mpfr_get_emax_max,
  205. "get_emax_max() -> integer\n\n"
  206. "Return the maximum possible exponent that can be set for 'mpfr'.");
  207. static PyObject *
  208. Pympfr_get_emax_max(PyObject *self, PyObject *args)
  209. {
  210. return PyIntOrLong_FromSsize_t((Py_ssize_t)mpfr_get_emax_max());
  211. }
  212. PyDoc_STRVAR(doc_g_mpfr_get_max_precision,
  213. "get_max_precision() -> integer\n\n"
  214. "Return the maximum bits of precision that can be used for calculations.\n"
  215. "Note: to allow extra precision for intermediate calculations, avoid\n"
  216. "setting precision close the maximum precision.");
  217. static PyObject *
  218. Pympfr_get_max_precision(PyObject *self, PyObject *args)
  219. {
  220. return PyIntOrLong_FromSsize_t((Py_ssize_t)MPFR_PREC_MAX);
  221. }
  222. PyDoc_STRVAR(doc_g_mpfr_get_exp,
  223. "get_exp(mpfr) -> integer\n\n"
  224. "Return the exponent of an mpfr. Returns 0 for NaN or Infinity and\n"
  225. "sets the erange flag and will raise an exception if trap_erange\n"
  226. "is set.");
  227. static PyObject *
  228. Pympfr_get_exp(PyObject *self, PyObject *other)
  229. {
  230. PyObject *result = 0;
  231. Py_ssize_t exp;
  232. PARSE_ONE_MPFR_OTHER("get_exp() requires 'mpfr' argument");
  233. if (mpfr_regular_p(Pympfr_AS_MPFR(self))) {
  234. exp = (Py_ssize_t)mpfr_get_exp(Pympfr_AS_MPFR(self));
  235. result = PyIntOrLong_FromSsize_t((Py_ssize_t)exp);
  236. }
  237. else if (mpfr_zero_p(Pympfr_AS_MPFR(self))) {
  238. Py_DECREF(self);
  239. result = PyIntOrLong_FromSsize_t(0);
  240. }
  241. else {
  242. context->ctx.erange = 1;
  243. if (context->ctx.trap_erange) {
  244. GMPY_ERANGE("Can not get exponent from NaN or Infinity.");
  245. }
  246. else {
  247. result = PyIntOrLong_FromSsize_t(0);
  248. }
  249. }
  250. Py_DECREF(self);
  251. return result;
  252. }
  253. PyDoc_STRVAR(doc_g_mpfr_set_exp,
  254. "set_exp(mpfr, n) -> mpfr\n\n"
  255. "Set the exponent of an mpfr to n. If n is outside the range of\n"
  256. "valid exponents, set_exp() will set the erange flag and either\n"
  257. "return the original value or raise an exception if trap_erange\n"
  258. "is set.");
  259. static PyObject *
  260. Pympfr_set_exp(PyObject *self, PyObject *args)
  261. {
  262. PympfrObject *result = 0;
  263. long exp = 0;
  264. if (!PyArg_ParseTuple(args, "O&l", Pympfr_convert_arg, &self, &exp)) {
  265. TYPE_ERROR("set_exp() requires 'mpfr', 'integer' arguments");
  266. return NULL;
  267. }
  268. if (!(result = Pympfr_From_Pympfr(self, 0)))
  269. return NULL;
  270. Py_DECREF(self);
  271. result->rc = mpfr_set_exp(Pympfr_AS_MPFR(result), exp);
  272. if (result->rc) {
  273. context->ctx.erange = 1;
  274. if (context->ctx.trap_erange) {
  275. GMPY_ERANGE("New exponent is out-of-bounds.");
  276. Py_DECREF(result);
  277. return NULL;
  278. }
  279. }
  280. return (PyObject*)result;
  281. }
  282. PyDoc_STRVAR(doc_g_mpfr_set_sign,
  283. "set_sign(mpfr, bool) -> mpfr\n\n"
  284. "If 'bool' is True, then return an 'mpfr' with the sign bit set.");
  285. static PyObject *
  286. Pympfr_set_sign(PyObject *self, PyObject *args)
  287. {
  288. PympfrObject *result = 0;
  289. PyObject *boolean = 0;
  290. int s;
  291. if (!PyArg_ParseTuple(args, "O&O", Pympfr_convert_arg, &self, &boolean)) {
  292. TYPE_ERROR("set_sign() requires 'mpfr', 'boolean' arguments");
  293. return NULL;
  294. }
  295. if (!(result = (PympfrObject*)Pympfr_new(0)))
  296. return NULL;
  297. s = PyObject_IsTrue(boolean);
  298. if (s == -1) {
  299. TYPE_ERROR("set_sign() requires 'mpfr', 'boolean' arguments");
  300. Py_DECREF(self);
  301. Py_DECREF(boolean);
  302. Py_DECREF(result);
  303. return NULL;
  304. }
  305. result->rc = mpfr_setsign(Pympfr_AS_MPFR(result), Pympfr_AS_MPFR(self),
  306. s, context->ctx.mpfr_round);
  307. Py_DECREF(self);
  308. Py_DECREF(boolean);
  309. return (PyObject*)result;
  310. }
  311. PyDoc_STRVAR(doc_g_mpfr_copy_sign,
  312. "copy_sign(mpfr, mpfr) -> mpfr\n\n"
  313. "Return an 'mpfr' composed of the first argument with the sign of the\n"
  314. "second argument.");
  315. static PyObject *
  316. Pympfr_copy_sign(PyObject *self, PyObject *args)
  317. {
  318. PympfrObject *result = 0;
  319. PyObject *other = 0;
  320. if (!PyArg_ParseTuple(args, "O&O&", Pympfr_convert_arg, &self,
  321. Pympfr_convert_arg, &other)) {
  322. TYPE_ERROR("copy_sign() requires 'mpfr', 'mpfr' arguments");
  323. return NULL;
  324. }
  325. if (!(result = (PympfrObject*)Pympfr_new(0)))
  326. return NULL;
  327. result->rc = mpfr_copysign(Pympfr_AS_MPFR(result), Pympfr_AS_MPFR(self),
  328. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  329. Py_DECREF(self);
  330. Py_DECREF(other);
  331. return (PyObject*)result;
  332. }
  333. static PyObject *
  334. Pympfr_div_2exp(PyObject *self, PyObject *args)
  335. {
  336. PympfrObject *result = 0;
  337. unsigned long exp = 0;
  338. if (!PyArg_ParseTuple(args, "O&k", Pympfr_convert_arg, &self, &exp)) {
  339. TYPE_ERROR("div_2exp() requires 'mpfr', 'integer' arguments");
  340. return NULL;
  341. }
  342. if (!(result = (PympfrObject*)Pympfr_new(0)))
  343. return NULL;
  344. mpfr_clear_flags();
  345. result->rc = mpfr_div_2ui(Pympfr_AS_MPFR(result), Pympfr_AS_MPFR(self),
  346. exp, context->ctx.mpfr_round);
  347. MPFR_CLEANUP_SELF("div_2exp()");
  348. }
  349. static PyObject *
  350. Pympfr_mul_2exp(PyObject *self, PyObject *args)
  351. {
  352. PympfrObject *result = 0;
  353. unsigned long exp = 0;
  354. if (!PyArg_ParseTuple(args, "O&k", Pympfr_convert_arg, &self, &exp)) {
  355. TYPE_ERROR("mul_2exp() requires 'mpfr', 'integer' arguments");
  356. return NULL;
  357. }
  358. if (!(result = (PympfrObject*)Pympfr_new(0)))
  359. return NULL;
  360. mpfr_clear_flags();
  361. result->rc = mpfr_mul_2ui(Pympfr_AS_MPFR(result), Pympfr_AS_MPFR(self),
  362. exp, context->ctx.mpfr_round);
  363. MPFR_CLEANUP_SELF("mul_2exp()");
  364. }
  365. PyDoc_STRVAR(doc_g_mpfr_set_nan,
  366. "nan() -> mpfr\n\n"
  367. "Return an 'mpfr' initialized to NaN (Not-A-Number).");
  368. static PyObject *
  369. Pympfr_set_nan(PyObject *self, PyObject *other)
  370. {
  371. PympfrObject *result;
  372. if ((result = (PympfrObject*)Pympfr_new(0)))
  373. mpfr_set_nan(result->f);
  374. return (PyObject*)result;
  375. }
  376. PyDoc_STRVAR(doc_g_mpfr_set_inf,
  377. "inf(n) -> mpfr\n\n"
  378. "Return an 'mpfr' initialized to Infinity with the same sign as n.\n"
  379. "If n is not given, +Infinity is returned.");
  380. static PyObject *
  381. Pympfr_set_inf(PyObject *self, PyObject *args)
  382. {
  383. PympfrObject *result;
  384. long s = 1;
  385. if (PyTuple_Size(args) == 1) {
  386. s = clong_From_Integer(PyTuple_GET_ITEM(args, 0));
  387. if (s == -1 && PyErr_Occurred()) {
  388. TYPE_ERROR("inf() requires 'int' argument");
  389. return NULL;
  390. }
  391. }
  392. if ((result = (PympfrObject*)Pympfr_new(0)))
  393. mpfr_set_inf(result->f, s<0?-1:1);
  394. return (PyObject*)result;
  395. }
  396. PyDoc_STRVAR(doc_g_mpfr_set_zero,
  397. "zero(n) -> mpfr\n\n"
  398. "Return an 'mpfr' inialized to 0.0 with the same sign as n.\n"
  399. "If n is not given, +0.0 is returned.");
  400. static PyObject *
  401. Pympfr_set_zero(PyObject *self, PyObject *args)
  402. {
  403. PympfrObject *result;
  404. long s = 1;
  405. if (PyTuple_Size(args) == 1) {
  406. s = clong_From_Integer(PyTuple_GET_ITEM(args, 0));
  407. if (s == -1 && PyErr_Occurred()) {
  408. TYPE_ERROR("zero() requires 'int' argument");
  409. return NULL;
  410. }
  411. }
  412. if ((result = (PympfrObject*)Pympfr_new(0)))
  413. mpfr_set_zero(result->f, s<0?-1:1);
  414. return (PyObject*)result;
  415. }
  416. PyDoc_STRVAR(doc_g_mpfr_is_signed,
  417. "is_signed(x) -> boolean\n\n"
  418. "Return True if the sign bit of x is set.");
  419. static PyObject *
  420. Pympfr_is_signed(PyObject *self, PyObject *other)
  421. {
  422. int res;
  423. if(self && Pympfr_Check(self)) {
  424. Py_INCREF(self);
  425. }
  426. else if(Pympfr_Check(other)) {
  427. self = other;
  428. Py_INCREF((PyObject*)self);
  429. }
  430. else if (!(self = (PyObject*)Pympfr_From_Real(other, 0))) {
  431. TYPE_ERROR("is_signed() requires 'mpfr' argument");
  432. return NULL;
  433. }
  434. res = mpfr_signbit(Pympfr_AS_MPFR(self));
  435. Py_DECREF(self);
  436. if (res)
  437. Py_RETURN_TRUE;
  438. else
  439. Py_RETURN_FALSE;
  440. }
  441. #define MPFR_TEST_OTHER(NAME, msg) \
  442. static PyObject * \
  443. Pympfr_is_##NAME(PyObject *self, PyObject *other)\
  444. {\
  445. int res;\
  446. if(self && Pympfr_Check(self)) {\
  447. Py_INCREF(self);\
  448. }\
  449. else if(Pympfr_Check(other)) {\
  450. self = other;\
  451. Py_INCREF((PyObject*)self);\
  452. }\
  453. else if (!(self = (PyObject*)Pympfr_From_Real(other, 0))) {\
  454. PyErr_SetString(PyExc_TypeError, msg);\
  455. return NULL;\
  456. }\
  457. res = mpfr_##NAME##_p(Pympfr_AS_MPFR(self));\
  458. Py_DECREF(self);\
  459. if (res)\
  460. Py_RETURN_TRUE;\
  461. else\
  462. Py_RETURN_FALSE;\
  463. }
  464. MPFR_TEST_OTHER(nan, "is_nan() requires 'mpfr' argument");
  465. MPFR_TEST_OTHER(inf, "is_infinite() requires 'mpfr' argument");
  466. PyDoc_STRVAR(doc_g_mpfr_is_number,
  467. "is_number(x) -> boolean\n\n"
  468. "Return True if x is an actual number (i.e. not NaN or Infinity);\n"
  469. "False otherwise.\n"
  470. "Note: is_number() is deprecated; please use is_finite().");
  471. MPFR_TEST_OTHER(number, "is_finite() requires 'mpfr' argument");
  472. MPFR_TEST_OTHER(zero, "is_zero() requires 'mpfr' argument");
  473. PyDoc_STRVAR(doc_g_mpfr_is_regular,
  474. "is_regular(x) -> boolean\n\n"
  475. "Return True if x is not zero, NaN, or Infinity; False otherwise.");
  476. MPFR_TEST_OTHER(regular, "is_regular() requires 'mpfr' argument");
  477. PyDoc_STRVAR(doc_mpfr_is_integer,
  478. "x.is_integer() -> boolean\n\n"
  479. "Return True if x is an integer; False otherwise.");
  480. PyDoc_STRVAR(doc_g_mpfr_is_integer,
  481. "is_integer(x) -> boolean\n\n"
  482. "Return True if x is an integer; False otherwise.");
  483. MPFR_TEST_OTHER(integer, "is_integer() requires 'mpfr' argument");
  484. /* produce string for an mpfr with requested/defaulted parameters */
  485. PyDoc_STRVAR(doc_mpfr_digits,
  486. "x.digits([base=10[, prec=0]]) -> (mantissa, exponent, bits)\n\n"
  487. "Returns up to 'prec' digits in the given base. If 'prec' is 0, as many\n"
  488. "digits that are available are returned. No more digits than available\n"
  489. "given x's precision are returned. 'base' must be between 2 and 62,\n"
  490. "inclusive. The result is a three element tuple containing the mantissa,\n"
  491. "the exponent, and the number of bits of precision.");
  492. /* TODO: support keyword arguments. */
  493. static PyObject *
  494. Pympfr_digits(PyObject *self, PyObject *args)
  495. {
  496. int base = 10;
  497. int prec = 0;
  498. PyObject *result;
  499. if (self && Pympfr_Check(self)) {
  500. if (!PyArg_ParseTuple(args, "|ii", &base, &prec))
  501. return NULL;
  502. Py_INCREF(self);
  503. }
  504. else {
  505. if(!PyArg_ParseTuple(args, "O&|ii", Pympfr_convert_arg, &self,
  506. &base, &prec))
  507. return NULL;
  508. }
  509. result = Pympfr_To_PyStr((PympfrObject*)self, base, prec);
  510. Py_DECREF(self);
  511. return result;
  512. }
  513. PyDoc_STRVAR(doc_mpfr_integer_ratio,
  514. "x.as_integer_ratio() -> (num, den)\n\n"
  515. "Return the exact rational equivalent of an mpfr. Value is a tuple\n"
  516. "for compatibility with Python's float.as_integer_ratio().");
  517. static PyObject *
  518. Pympfr_integer_ratio(PyObject *self, PyObject *args)
  519. {
  520. PympzObject *num = 0, *den = 0;
  521. mpfr_exp_t temp, twocount;
  522. PyObject *result;
  523. if (mpfr_nan_p(Pympfr_AS_MPFR(self))) {
  524. VALUE_ERROR("Cannot pass NaN to mpfr.as_integer_ratio.");
  525. return NULL;
  526. }
  527. if (mpfr_inf_p(Pympfr_AS_MPFR(self))) {
  528. OVERFLOW_ERROR("Cannot pass Infinity to mpfr.as_integer_ratio.");
  529. return NULL;
  530. }
  531. num = (PympzObject*)Pympz_new();
  532. den = (PympzObject*)Pympz_new();
  533. if (!num || !den) {
  534. Py_XDECREF((PyObject*)num);
  535. Py_XDECREF((PyObject*)den);
  536. return NULL;
  537. }
  538. if (mpfr_zero_p(Pympfr_AS_MPFR(self))) {
  539. mpz_set_ui(num->z, 0);
  540. mpz_set_ui(den->z, 1);
  541. }
  542. else {
  543. temp = mpfr_get_z_2exp(num->z, Pympfr_AS_MPFR(self));
  544. twocount = (mpfr_exp_t)mpz_scan1(num->z, 0);
  545. if (twocount) {
  546. temp += twocount;
  547. mpz_div_2exp(num->z, num->z, twocount);
  548. }
  549. mpz_set_ui(den->z, 1);
  550. if (temp > 0)
  551. mpz_mul_2exp(num->z, num->z, temp);
  552. else if (temp < 0)
  553. mpz_mul_2exp(den->z, den->z, -temp);
  554. }
  555. result = Py_BuildValue("(NN)", (PyObject*)num, (PyObject*)den);
  556. if (!result) {
  557. Py_DECREF((PyObject*)num);
  558. Py_DECREF((PyObject*)den);
  559. }
  560. return result;
  561. }
  562. PyDoc_STRVAR(doc_mpfr_mantissa_exp,
  563. "x.as_mantissa_exp() -> (mantissa,exponent)\n\n"
  564. "Return the mantissa and exponent of an mpfr.");
  565. static PyObject *
  566. Pympfr_mantissa_exp(PyObject *self, PyObject *args)
  567. {
  568. PympzObject *mantissa = 0, *exponent = 0;
  569. mpfr_exp_t temp;
  570. PyObject *result;
  571. if (mpfr_nan_p(Pympfr_AS_MPFR(self))) {
  572. VALUE_ERROR("Cannot pass NaN to mpfr.as_mantissa_exp.");
  573. return NULL;
  574. }
  575. if (mpfr_inf_p(Pympfr_AS_MPFR(self))) {
  576. OVERFLOW_ERROR("Cannot pass Infinity to mpfr.as_mantissa_exp.");
  577. return NULL;
  578. }
  579. mantissa = (PympzObject*)Pympz_new();
  580. exponent = (PympzObject*)Pympz_new();
  581. if (!mantissa || !exponent) {
  582. Py_XDECREF((PyObject*)mantissa);
  583. Py_XDECREF((PyObject*)exponent);
  584. return NULL;
  585. }
  586. if (mpfr_zero_p(Pympfr_AS_MPFR(self))) {
  587. mpz_set_ui(mantissa->z, 0);
  588. mpz_set_ui(exponent->z, 1);
  589. }
  590. else {
  591. temp = mpfr_get_z_2exp(mantissa->z, Pympfr_AS_MPFR(self));
  592. mpz_set_si(exponent->z, temp);
  593. }
  594. result = Py_BuildValue("(NN)", (PyObject*)mantissa, (PyObject*)exponent);
  595. if (!result) {
  596. Py_DECREF((PyObject*)mantissa);
  597. Py_DECREF((PyObject*)exponent);
  598. }
  599. return result;
  600. }
  601. PyDoc_STRVAR(doc_mpfr_simple_fraction,
  602. "x.as_simple_fraction([precision=0]) -> mpq\n\n"
  603. "Return a simple rational approximation to x. The result will be\n"
  604. "accurate to 'precision' bits. If 'precision' is 0, the precision\n"
  605. "of 'x' will be used.");
  606. static PyObject *
  607. Pympfr_simple_fraction(PyObject *self, PyObject *args, PyObject *keywds)
  608. {
  609. mpfr_prec_t prec = 0;
  610. static char *kwlist[] = {"precision", NULL};
  611. if (!PyArg_ParseTupleAndKeywords(args, keywds, "|l", kwlist, &prec))
  612. return NULL;
  613. return (PyObject*)stern_brocot((PympfrObject*)self, 0, prec, 0);
  614. }
  615. static Py_hash_t
  616. _mpfr_hash(mpfr_t f)
  617. {
  618. #ifdef _PyHASH_MODULUS
  619. Py_uhash_t hash = 0;
  620. Py_ssize_t exp;
  621. size_t msize;
  622. int sign;
  623. /* Handle special cases first */
  624. if (!mpfr_number_p(f)) {
  625. if (mpfr_inf_p(f))
  626. if (mpfr_sgn(f) > 0)
  627. return _PyHASH_INF;
  628. else
  629. return -_PyHASH_INF;
  630. else
  631. return _PyHASH_NAN;
  632. }
  633. /* Calculate the number of limbs in the mantissa. */
  634. msize = (f->_mpfr_prec + mp_bits_per_limb - 1) / mp_bits_per_limb;
  635. /* Calculate the hash of the mantissa. */
  636. if (mpfr_sgn(f) > 0) {
  637. hash = mpn_mod_1(f->_mpfr_d, msize, _PyHASH_MODULUS);
  638. sign = 1;
  639. }
  640. else if (mpfr_sgn(f) < 0) {
  641. hash = mpn_mod_1(f->_mpfr_d, msize, _PyHASH_MODULUS);
  642. sign = -1;
  643. }
  644. else {
  645. return 0;
  646. }
  647. /* Calculate the final hash. */
  648. exp = f->_mpfr_exp - (msize * mp_bits_per_limb);
  649. exp = exp >= 0 ? exp % _PyHASH_BITS : _PyHASH_BITS-1-((-1-exp) % _PyHASH_BITS);
  650. hash = ((hash << exp) & _PyHASH_MODULUS) | hash >> (_PyHASH_BITS - exp);
  651. hash *= sign;
  652. if (hash == (Py_uhash_t)-1)
  653. hash = (Py_uhash_t)-2;
  654. return (Py_hash_t)hash;
  655. #else
  656. double temp;
  657. temp = mpfr_get_d(f, context->ctx.mpfr_round);
  658. return _Py_HashDouble(temp);
  659. #endif
  660. }
  661. static Py_hash_t
  662. Pympfr_hash(PympfrObject *self)
  663. {
  664. if (self->hash_cache == -1)
  665. self->hash_cache = _mpfr_hash(self->f);
  666. return self->hash_cache;
  667. }
  668. /* This function is used in gmpy_mpany. */
  669. static PyObject *
  670. Pympfr_pow(PyObject *base, PyObject *exp, PyObject *m)
  671. {
  672. PympfrObject *tempb, *tempe, *result;
  673. #ifdef WITHMPC
  674. PympcObject *mpc_result;
  675. #endif
  676. if (m != Py_None) {
  677. TYPE_ERROR("pow() 3rd argument not allowed unless all arguments are integers");
  678. return NULL;
  679. }
  680. tempb = Pympfr_From_Real(base, 0);
  681. tempe = Pympfr_From_Real(exp, 0);
  682. if (!tempe || !tempb) {
  683. Py_XDECREF((PyObject*)tempe);
  684. Py_XDECREF((PyObject*)tempb);
  685. Py_RETURN_NOTIMPLEMENTED;
  686. }
  687. result = (PympfrObject*)Pympfr_new(0);
  688. if (!result) {
  689. Py_DECREF((PyObject*)tempe);
  690. Py_DECREF((PyObject*)tempb);
  691. return NULL;
  692. }
  693. if (mpfr_zero_p(tempb->f) && (mpfr_sgn(tempe->f) < 0)) {
  694. context->ctx.divzero = 1;
  695. if (context->ctx.trap_divzero) {
  696. GMPY_DIVZERO("zero cannot be raised to a negative power");
  697. goto done;
  698. }
  699. }
  700. mpfr_clear_flags();
  701. result->rc = mpfr_pow(result->f, tempb->f,
  702. tempe->f, context->ctx.mpfr_round);
  703. #ifdef WITHMPC
  704. if (result && mpfr_nanflag_p() && context->ctx.allow_complex) {
  705. /* If we don't get a valid result, or the result is a nan, then just
  706. * return the original mpfr value. */
  707. if (!(mpc_result = (PympcObject*)Pympc_pow(base, exp, m)) ||
  708. MPC_IS_NAN_P(mpc_result)) {
  709. Py_XDECREF((PyObject*)mpc_result);
  710. context->ctx.invalid = 1;
  711. GMPY_INVALID("invalid operation in 'mpfr' pow()");
  712. goto done;
  713. }
  714. /* return a valid complex result */
  715. Py_DECREF(result);
  716. result = (PympfrObject*)mpc_result;
  717. goto done;
  718. }
  719. #endif
  720. SUBNORMALIZE(result)
  721. MERGE_FLAGS
  722. CHECK_FLAGS("pow()")
  723. done:
  724. Py_DECREF((PyObject*)tempe);
  725. Py_DECREF((PyObject*)tempb);
  726. if (PyErr_Occurred()) {
  727. Py_XDECREF((PyObject*)result);
  728. result = NULL;
  729. }
  730. return (PyObject*)result;
  731. }
  732. #define MPFR_CONST(NAME) \
  733. static PyObject * \
  734. Pympfr_##NAME(PyObject *self, PyObject *args, PyObject *keywds) \
  735. { \
  736. PympfrObject *result; \
  737. mpfr_prec_t bits = 0; \
  738. static char *kwlist[] = {"precision", NULL}; \
  739. if (!PyArg_ParseTupleAndKeywords(args, keywds, "|l", kwlist, &bits)) return NULL; \
  740. if ((result = (PympfrObject*)Pympfr_new(bits))) { \
  741. mpfr_clear_flags(); \
  742. result->rc = mpfr_##NAME(result->f, context->ctx.mpfr_round); \
  743. MERGE_FLAGS \
  744. CHECK_FLAGS(#NAME "()") \
  745. } \
  746. done: \
  747. return (PyObject*)result; \
  748. }
  749. PyDoc_STRVAR(doc_mpfr_const_pi,
  750. "const_pi([precision=0]) -> mpfr\n\n"
  751. "Return the constant pi using the specified precision. If no\n"
  752. "precision is specified, the default precision is used.");
  753. MPFR_CONST(const_pi)
  754. PyDoc_STRVAR(doc_mpfr_const_euler,
  755. "const_euler([precision=0]) -> mpfr\n\n"
  756. "Return the euler constant using the specified precision. If no\n"
  757. "precision is specified, the default precision is used.");
  758. MPFR_CONST(const_euler)
  759. PyDoc_STRVAR(doc_mpfr_const_log2,
  760. "const_log2([precision=0]) -> mpfr\n\n"
  761. "Return the log2 constant using the specified precision. If no\n"
  762. "precision is specified, the default precision is used.");
  763. MPFR_CONST(const_log2)
  764. PyDoc_STRVAR(doc_mpfr_const_catalan,
  765. "const_catalan([precision=0]) -> mpfr\n\n"
  766. "Return the catalan constant using the specified precision. If no\n"
  767. "precision is specified, the default precision is used.");
  768. MPFR_CONST(const_catalan)
  769. static PyObject *
  770. Pympfr_sqrt(PyObject *self, PyObject *other)
  771. {
  772. PympfrObject *result;
  773. PARSE_ONE_MPFR_OTHER("sqrt() requires 'mpfr' argument");
  774. #ifdef WITHMPC
  775. if (mpfr_sgn(Pympfr_AS_MPFR(self)) < 0 && context->ctx.allow_complex) {
  776. Py_DECREF(self);
  777. return Pympc_sqrt(self, other);
  778. }
  779. #endif
  780. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  781. Py_DECREF(self);
  782. return NULL;
  783. }
  784. mpfr_clear_flags();
  785. result->rc = mpfr_sqrt(result->f, Pympfr_AS_MPFR(self),
  786. context->ctx.mpfr_round);
  787. MPFR_CLEANUP_SELF("sqrt()");
  788. }
  789. PyDoc_STRVAR(doc_g_mpfr_rec_sqrt,
  790. "rec_sqrt(x) -> mpfr\n\n"
  791. "Return the reciprocal of the square root of x.");
  792. static PyObject *
  793. Pympfr_rec_sqrt(PyObject *self, PyObject *other)
  794. {
  795. PympfrObject *result;
  796. PARSE_ONE_MPFR_OTHER("rec_sqrt() requires 'mpfr' argument");
  797. if (!(result = (PympfrObject*)Pympfr_new(0)))
  798. goto done;
  799. mpfr_clear_flags();
  800. result->rc = mpfr_rec_sqrt(result->f, Pympfr_AS_MPFR(self),
  801. context->ctx.mpfr_round);
  802. MPFR_CLEANUP_SELF("rec_sqrt()");
  803. }
  804. PyDoc_STRVAR(doc_mpfr_root,
  805. "root(x, n) -> mpfr\n\n"
  806. "Return n-th root of x. The result always an 'mpfr'.");
  807. static PyObject *
  808. Pympfr_root(PyObject *self, PyObject *args)
  809. {
  810. long n;
  811. PympfrObject *result;
  812. PARSE_ONE_MPFR_REQ_CLONG(&n, "root() requires 'mpfr','int' arguments");
  813. if (!(result = (PympfrObject*)Pympfr_new(0)))
  814. goto done;
  815. if (n <= 0) {
  816. VALUE_ERROR("n must be > 0");
  817. goto done;
  818. }
  819. mpfr_clear_flags();
  820. result->rc = mpfr_root(result->f, Pympfr_AS_MPFR(self), n,
  821. context->ctx.mpfr_round);
  822. MPFR_CLEANUP_SELF("root()");
  823. }
  824. PyDoc_STRVAR(doc_g_mpfr_round2,
  825. "round2(x[, n]) -> mpfr\n\n"
  826. "Return x rounded to n bits. Uses default precision if n is not\n"
  827. "specified. See round_away() to access the mpfr_round() function.");
  828. static PyObject *
  829. Pympfr_round2(PyObject *self, PyObject *args)
  830. {
  831. mpfr_prec_t prec = context->ctx.mpfr_prec;
  832. PympfrObject *result = 0;
  833. PARSE_ONE_MPFR_OPT_CLONG(&prec,
  834. "round2() requires 'mpfr',['int'] arguments");
  835. if (prec < MPFR_PREC_MIN || prec > MPFR_PREC_MAX) {
  836. VALUE_ERROR("invalid precision");
  837. goto done;
  838. }
  839. if (!(result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self))))) {
  840. goto done;
  841. }
  842. mpfr_clear_flags();
  843. /* Duplicate the code from Pympfr_pos. */
  844. mpfr_set(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round);
  845. result->round_mode = ((PympfrObject*)self)->round_mode;
  846. result->rc = ((PympfrObject*)self)->rc;
  847. result->rc = mpfr_check_range(result->f, result->rc, result->round_mode);
  848. result->rc = mpfr_prec_round(result->f, prec, context->ctx.mpfr_round);
  849. MPFR_CLEANUP_SELF("round2()");
  850. }
  851. PyDoc_STRVAR(doc_g_mpfr_round10,
  852. "__round__(x[, n = 0]) -> mpfr\n\n"
  853. "Return x rounded to n decimal digits before (n < 0) or after (n > 0)\n"
  854. "the decimal point. Rounds to an integer if n is not specified.");
  855. static PyObject *
  856. Pympfr_round10(PyObject *self, PyObject *args)
  857. {
  858. Py_ssize_t digits = 0;
  859. mpz_t temp;
  860. PympfrObject *resultf = 0;
  861. PympzObject *resultz;
  862. /* If the size of args is 0, we just return an mpz. */
  863. if (PyTuple_GET_SIZE(args) == 0) {
  864. if ((resultz = (PympzObject*)Pympz_new())) {
  865. if (mpfr_nan_p(Pympfr_AS_MPFR(self))) {
  866. Py_DECREF((PyObject*)resultz);
  867. VALUE_ERROR("'mpz' does not support NaN");
  868. return NULL;
  869. }
  870. if (mpfr_inf_p(Pympfr_AS_MPFR(self))) {
  871. Py_DECREF((PyObject*)resultz);
  872. OVERFLOW_ERROR("'mpz' does not support Infinity");
  873. return NULL;
  874. }
  875. /* return code is ignored */
  876. mpfr_get_z(resultz->z, Pympfr_AS_MPFR(self), MPFR_RNDN);
  877. }
  878. return (PyObject*)resultz;
  879. }
  880. /* Now we need to return an mpfr, so handle the simple cases. */
  881. if (!mpfr_regular_p(Pympfr_AS_MPFR(self))) {
  882. Py_INCREF(self);
  883. return self;
  884. }
  885. if (PyTuple_GET_SIZE(args) > 1) {
  886. TYPE_ERROR("Too many arguments for __round__().");
  887. return NULL;
  888. }
  889. if (PyTuple_GET_SIZE(args) == 1) {
  890. digits = ssize_t_From_Integer(PyTuple_GET_ITEM(args, 0));
  891. if (digits == -1 && PyErr_Occurred()) {
  892. TYPE_ERROR("__round__() requires 'int' argument");
  893. return NULL;
  894. }
  895. }
  896. /* TODO: better error analysis, or else convert the mpfr to an exact
  897. * fraction, round the fraction, and then convert back to an mpfr.
  898. */
  899. resultf = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self))+100);
  900. if (!resultf)
  901. return NULL;
  902. mpz_inoc(temp);
  903. mpz_ui_pow_ui(temp, 10, digits > 0 ? digits : -digits);
  904. if (digits >= 0) {
  905. mpfr_mul_z(resultf->f, Pympfr_AS_MPFR(self), temp, MPFR_RNDN);
  906. }
  907. else {
  908. mpfr_div_z(resultf->f, Pympfr_AS_MPFR(self), temp, MPFR_RNDN);
  909. }
  910. mpfr_rint(resultf->f, resultf->f, MPFR_RNDN);
  911. if (digits >= 0) {
  912. mpfr_div_z(resultf->f, resultf->f, temp, MPFR_RNDN);
  913. }
  914. else {
  915. mpfr_mul_z(resultf->f, resultf->f, temp, MPFR_RNDN);
  916. }
  917. mpfr_prec_round(resultf->f, mpfr_get_prec(Pympfr_AS_MPFR(self)), MPFR_RNDN);
  918. mpz_cloc(temp);
  919. return((PyObject*)resultf);
  920. }
  921. PyDoc_STRVAR(doc_g_mpfr_reldiff,
  922. "reldiff(x, y) -> mpfr\n\n"
  923. "Return the relative difference between x and y. Result is equal to\n"
  924. "abs(x-y)/x.");
  925. static PyObject *
  926. Pympfr_reldiff(PyObject *self, PyObject *args)
  927. {
  928. PympfrObject *result;
  929. PyObject *other;
  930. PARSE_TWO_MPFR_ARGS(other, "reldiff() requires 'mpfr','mpfr' arguments");
  931. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  932. Py_DECREF(self);
  933. Py_DECREF(other);
  934. return NULL;
  935. }
  936. /* mpfr_reldiff doesn't guarantee correct rounding and doesn't appear
  937. * to set any exceptions.
  938. */
  939. mpfr_reldiff(result->f, Pympfr_AS_MPFR(self), Pympfr_AS_MPFR(other),
  940. context->ctx.mpfr_round);
  941. result->rc = 0;
  942. Py_DECREF(self);
  943. Py_DECREF(other);
  944. return (PyObject*)result;
  945. }
  946. static PyObject *
  947. Pympfr_sign(PyObject *self, PyObject *other)
  948. {
  949. long sign;
  950. PARSE_ONE_MPFR_OTHER("sign() requires 'mpfr' argument");
  951. mpfr_clear_flags();
  952. sign = mpfr_sgn(Pympfr_AS_MPFR(self));
  953. MERGE_FLAGS;
  954. CHECK_ERANGE("range error in 'mpfr' sign(), NaN argument");
  955. done:
  956. Py_DECREF((PyObject*)self);
  957. if (PyErr_Occurred())
  958. return NULL;
  959. else
  960. return PyIntOrLong_FromLong(sign);
  961. }
  962. #define MPFR_MONOP(NAME) \
  963. static PyObject * \
  964. Py##NAME(PympfrObject *x) \
  965. { \
  966. PympfrObject *r; \
  967. if (!(r = (PympfrObject*)Pympfr_new(0))) \
  968. return NULL; \
  969. if (Pympfr_CheckAndExp(x)) { \
  970. r->rc = NAME(r->f, x->f, context->ctx.mpfr_round); \
  971. } \
  972. else { \
  973. mpfr_set(r->f, x->f, context->ctx.mpfr_round); \
  974. r->round_mode = x->round_mode; \
  975. r->rc = x->rc; \
  976. mpfr_clear_flags(); \
  977. mpfr_check_range(r->f, r->rc, r->round_mode); \
  978. r->rc = NAME(r->f, r->f, context->ctx.mpfr_round); \
  979. MERGE_FLAGS; \
  980. CHECK_FLAGS(#NAME "()"); \
  981. } \
  982. done: \
  983. return (PyObject *) r; \
  984. }
  985. MPFR_MONOP(mpfr_abs)
  986. MPFR_MONOP(mpfr_neg)
  987. #define MPFR_UNIOP_NOROUND(NAME) \
  988. static PyObject * \
  989. Pympfr_##NAME(PyObject* self, PyObject *other) \
  990. { \
  991. PympfrObject *result; \
  992. PARSE_ONE_MPFR_OTHER(#NAME "() requires 'mpfr' argument"); \
  993. if (!(result = (PympfrObject*)Pympfr_new(0))) goto done; \
  994. mpfr_clear_flags(); \
  995. result->rc = mpfr_##NAME(result->f, Pympfr_AS_MPFR(self)); \
  996. MPFR_CLEANUP_SELF(#NAME "()"); \
  997. }
  998. PyDoc_STRVAR(doc_mpfr_ceil,
  999. "x.__ceil__() -> mpfr\n\n"
  1000. "Return an 'mpfr' that is the smallest integer >= x.");
  1001. PyDoc_STRVAR(doc_g_mpfr_ceil,
  1002. "ceil(x) ->mpfr\n\n"
  1003. "Return an 'mpfr' that is the smallest integer >= x.");
  1004. MPFR_UNIOP_NOROUND(ceil)
  1005. PyDoc_STRVAR(doc_mpfr_floor,
  1006. "x.__floor__() -> mpfr\n\n"
  1007. "Return an 'mpfr' that is the smallest integer <= x.");
  1008. PyDoc_STRVAR(doc_g_mpfr_floor,
  1009. "floor(x) -> mpfr\n\n"
  1010. "Return an 'mpfr' that is the smallest integer <= x.");
  1011. MPFR_UNIOP_NOROUND(floor);
  1012. PyDoc_STRVAR(doc_mpfr_trunc,
  1013. "x.__trunc__() -> mpfr\n\n"
  1014. "Return an 'mpfr' that is truncated towards 0. Same as\n"
  1015. "x.floor() if x>=0 or x.ceil() if x<0.");
  1016. PyDoc_STRVAR(doc_g_mpfr_trunc,
  1017. "trunc(x) -> mpfr\n\n"
  1018. "Return an 'mpfr' that is x truncated towards 0. Same as\n"
  1019. "x.floor() if x>=0 or x.ceil() if x<0.");
  1020. MPFR_UNIOP_NOROUND(trunc)
  1021. PyDoc_STRVAR(doc_g_mpfr_round_away,
  1022. "round_away(x) -> mpfr\n\n"
  1023. "Return an 'mpfr' that is x rounded to the nearest integer,\n"
  1024. "with ties rounded away from 0.");
  1025. static PyObject *
  1026. Pympfr_round_away(PyObject* self, PyObject *other)
  1027. {
  1028. PympfrObject *result;
  1029. PARSE_ONE_MPFR_OTHER("round_away() requires 'mpfr' argument");
  1030. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1031. goto done;
  1032. mpfr_clear_flags();
  1033. result->rc = mpfr_round(result->f, Pympfr_AS_MPFR(self));
  1034. MPFR_CLEANUP_SELF("round_away()");
  1035. }
  1036. #define MPFR_UNIOP(NAME) \
  1037. static PyObject * \
  1038. Pympfr_##NAME(PyObject* self, PyObject *other) \
  1039. { \
  1040. PympfrObject *result; \
  1041. PARSE_ONE_MPFR_OTHER(#NAME "() requires 'mpfr' argument"); \
  1042. if (!(result = (PympfrObject*)Pympfr_new(0))) goto done; \
  1043. mpfr_clear_flags(); \
  1044. result->rc = mpfr_##NAME(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round); \
  1045. MPFR_CLEANUP_SELF(#NAME "()"); \
  1046. }
  1047. PyDoc_STRVAR(doc_g_mpfr_rint,
  1048. "rint(x) -> mpfr\n\n"
  1049. "Return x rounded to the nearest integer using the current rounding\n"
  1050. "mode.");
  1051. MPFR_UNIOP(rint)
  1052. PyDoc_STRVAR(doc_g_mpfr_rint_ceil,
  1053. "rint_ceil(x) -> mpfr\n\n"
  1054. "Return x rounded to the nearest integer by first rounding to the\n"
  1055. "next higher or equal integer and then, if needed, using the current\n"
  1056. "rounding mode.");
  1057. MPFR_UNIOP(rint_ceil)
  1058. PyDoc_STRVAR(doc_g_mpfr_rint_floor,
  1059. "rint_floor(x) -> mpfr\n\n"
  1060. "Return x rounded to the nearest integer by first rounding to the\n"
  1061. "next lower or equal integer and then, if needed, using the current\n"
  1062. "rounding mode.");
  1063. MPFR_UNIOP(rint_floor)
  1064. PyDoc_STRVAR(doc_g_mpfr_rint_round,
  1065. "rint_round(x) -> mpfr\n\n"
  1066. "Return x rounded to the nearest integer by first rounding to the\n"
  1067. "nearest integer (ties away from 0) and then, if needed, using\n"
  1068. "the current rounding mode.");
  1069. MPFR_UNIOP(rint_round)
  1070. PyDoc_STRVAR(doc_g_mpfr_rint_trunc,
  1071. "rint_trunc(x) -> mpfr\n\n"
  1072. "Return x rounded to the nearest integer by first rounding towards\n"
  1073. "zero and then, if needed, using the current rounding mode.");
  1074. MPFR_UNIOP(rint_trunc)
  1075. PyDoc_STRVAR(doc_g_mpfr_frac,
  1076. "frac(x) -> mpfr\n\n"
  1077. "Return fractional part of x.");
  1078. MPFR_UNIOP(frac)
  1079. PyDoc_STRVAR(doc_g_mpfr_modf,
  1080. "modf(x) -> (mpfr, mpfr)\n\n"
  1081. "Return a tuple containing the integer and fractional portions\n"
  1082. "of x.");
  1083. static PyObject *
  1084. Pympfr_modf(PyObject *self, PyObject *other)
  1085. {
  1086. PympfrObject *s, *c;
  1087. PyObject *result;
  1088. int code;
  1089. PARSE_ONE_MPFR_OTHER("modf() requires 'mpfr' argument");
  1090. s = (PympfrObject*)Pympfr_new(0);
  1091. c = (PympfrObject*)Pympfr_new(0);
  1092. result = PyTuple_New(2);
  1093. if (!s || !c || !result)
  1094. goto done;
  1095. mpfr_clear_flags();
  1096. code = mpfr_modf(s->f, c->f, Pympfr_AS_MPFR(self),
  1097. context->ctx.mpfr_round);
  1098. s->rc = code & 0x03;
  1099. c->rc = code >> 2;
  1100. if (s->rc == 2) s->rc = -1;
  1101. if (c->rc == 2) c->rc = -1;
  1102. SUBNORMALIZE(s);
  1103. SUBNORMALIZE(c);
  1104. MERGE_FLAGS;
  1105. CHECK_FLAGS("modf()");
  1106. done:
  1107. Py_DECREF(self);
  1108. if (PyErr_Occurred()) {
  1109. Py_XDECREF((PyObject*)s);
  1110. Py_XDECREF((PyObject*)c);
  1111. Py_XDECREF(result);
  1112. result = NULL;
  1113. }
  1114. else {
  1115. PyTuple_SET_ITEM(result, 0, (PyObject*)s);
  1116. PyTuple_SET_ITEM(result, 1, (PyObject*)c);
  1117. }
  1118. return result;
  1119. }
  1120. /* Needed for square() in mpz_mpany.c */
  1121. MPFR_UNIOP(sqr)
  1122. PyDoc_STRVAR(doc_g_mpfr_cbrt,
  1123. "cbrt(x) -> mpfr\n\n"
  1124. "Return the cube root of x.");
  1125. MPFR_UNIOP(cbrt)
  1126. /* Called via gmpy_mpany so doc-string is there. */
  1127. MPFR_UNIOP(log)
  1128. PyDoc_STRVAR(doc_g_mpfr_log2,
  1129. "log2(x) -> mpfr\n\n"
  1130. "Return base-2 logarithm of x.");
  1131. MPFR_UNIOP(log2)
  1132. /* Called via gmpy_mpany so doc-string is there. */
  1133. MPFR_UNIOP(log10)
  1134. /* Called via gmpy_mpany so doc-string is there. */
  1135. MPFR_UNIOP(exp)
  1136. PyDoc_STRVAR(doc_g_mpfr_exp2,
  1137. "exp2(x) -> mpfr\n\n"
  1138. "Return 2**x.");
  1139. MPFR_UNIOP(exp2)
  1140. PyDoc_STRVAR(doc_g_mpfr_exp10,
  1141. "exp10(x) -> mpfr\n\n"
  1142. "Return 10**x.");
  1143. MPFR_UNIOP(exp10)
  1144. MPFR_UNIOP(sin)
  1145. MPFR_UNIOP(cos)
  1146. MPFR_UNIOP(tan)
  1147. PyDoc_STRVAR(doc_g_mpfr_sec,
  1148. "sec(x) -> mpfr\n\n"
  1149. "Return secant of x; x in radians.");
  1150. MPFR_UNIOP(sec)
  1151. PyDoc_STRVAR(doc_g_mpfr_csc,
  1152. "csc(x) -> mpfr\n\n"
  1153. "Return cosecant of x; x in radians.");
  1154. MPFR_UNIOP(csc)
  1155. PyDoc_STRVAR(doc_g_mpfr_cot,
  1156. "cot(x) -> mpfr\n\n"
  1157. "Return cotangent of x; x in radians.");
  1158. MPFR_UNIOP(cot)
  1159. static PyObject *
  1160. Pympfr_acos(PyObject* self, PyObject *other)
  1161. {
  1162. PympfrObject *result;
  1163. PARSE_ONE_MPFR_OTHER("acos() requires 'mpfr' argument");
  1164. #ifdef WITHMPC
  1165. if (!mpfr_nan_p(Pympfr_AS_MPFR(self)) &&
  1166. (mpfr_cmp_si(Pympfr_AS_MPFR(self), 1) > 0 ||
  1167. mpfr_cmp_si(Pympfr_AS_MPFR(self), -1) < 0) &&
  1168. context->ctx.allow_complex) {
  1169. Py_DECREF(self);
  1170. return Pympc_acos(self, other);
  1171. }
  1172. #endif
  1173. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1174. Py_DECREF(self);
  1175. return NULL;
  1176. }
  1177. mpfr_clear_flags();
  1178. result->rc = mpfr_acos(result->f, Pympfr_AS_MPFR(self),
  1179. context->ctx.mpfr_round);
  1180. MPFR_CLEANUP_SELF("acos()");
  1181. }
  1182. static PyObject *
  1183. Pympfr_asin(PyObject* self, PyObject *other)
  1184. {
  1185. PympfrObject *result;
  1186. PARSE_ONE_MPFR_OTHER("asin() requires 'mpfr' argument");
  1187. #ifdef WITHMPC
  1188. if (!mpfr_nan_p(Pympfr_AS_MPFR(self)) &&
  1189. (mpfr_cmp_si(Pympfr_AS_MPFR(self), 1) > 0 ||
  1190. mpfr_cmp_si(Pympfr_AS_MPFR(self), -1) < 0) &&
  1191. context->ctx.allow_complex) {
  1192. Py_DECREF(self);
  1193. return Pympc_asin(self, other);
  1194. }
  1195. #endif
  1196. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1197. Py_DECREF(self);
  1198. return NULL;
  1199. }
  1200. mpfr_clear_flags();
  1201. result->rc = mpfr_asin(result->f, Pympfr_AS_MPFR(self),
  1202. context->ctx.mpfr_round);
  1203. MPFR_CLEANUP_SELF("asin()");
  1204. }
  1205. MPFR_UNIOP(atan)
  1206. MPFR_UNIOP(cosh)
  1207. MPFR_UNIOP(sinh)
  1208. MPFR_UNIOP(tanh)
  1209. PyDoc_STRVAR(doc_g_mpfr_sech,
  1210. "sech(x) -> mpfr\n\n"
  1211. "Returns hyperbolic secant of x.");
  1212. MPFR_UNIOP(sech)
  1213. PyDoc_STRVAR(doc_g_mpfr_csch,
  1214. "csch(x) -> mpfr\n\n"
  1215. "Return hyperbolic cosecant of x.");
  1216. MPFR_UNIOP(csch)
  1217. PyDoc_STRVAR(doc_g_mpfr_coth,
  1218. "coth(x) -> mpfr\n\n"
  1219. "Return hyperbolic cotangent of x.");
  1220. MPFR_UNIOP(coth)
  1221. MPFR_UNIOP(acosh)
  1222. MPFR_UNIOP(asinh)
  1223. static PyObject *
  1224. Pympfr_atanh(PyObject* self, PyObject *other)
  1225. {
  1226. PympfrObject *result;
  1227. PARSE_ONE_MPFR_OTHER("atanh() requires 'mpfr' argument");
  1228. #ifdef WITHMPC
  1229. if (!mpfr_nan_p(Pympfr_AS_MPFR(self)) &&
  1230. (mpfr_cmp_si(Pympfr_AS_MPFR(self), 1) > 0 ||
  1231. mpfr_cmp_si(Pympfr_AS_MPFR(self), -1) < 0) &&
  1232. context->ctx.allow_complex) {
  1233. Py_DECREF(self);
  1234. return Pympc_atanh(self, other);
  1235. }
  1236. #endif
  1237. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1238. Py_DECREF(self);
  1239. return NULL;
  1240. }
  1241. mpfr_clear_flags();
  1242. result->rc = mpfr_asin(result->f, Pympfr_AS_MPFR(self),
  1243. context->ctx.mpfr_round);
  1244. MPFR_CLEANUP_SELF("atanh()");
  1245. }
  1246. PyDoc_STRVAR(doc_g_mpfr_log1p,
  1247. "log1p(x) -> mpfr\n\n"
  1248. "Return logarithm of (1+x).");
  1249. MPFR_UNIOP(log1p)
  1250. PyDoc_STRVAR(doc_g_mpfr_expm1,
  1251. "expm1(x) -> mpfr\n\n"
  1252. "Return exponential(x) - 1.");
  1253. MPFR_UNIOP(expm1)
  1254. PyDoc_STRVAR(doc_g_mpfr_eint,
  1255. "eint(x) -> mpfr\n\n"
  1256. "Return exponential integral of x.");
  1257. MPFR_UNIOP(eint)
  1258. PyDoc_STRVAR(doc_g_mpfr_li2,
  1259. "li2(x) -> mpfr\n\n"
  1260. "Return real part of dilogarithm of x.");
  1261. MPFR_UNIOP(li2)
  1262. PyDoc_STRVAR(doc_g_mpfr_gamma,
  1263. "gamma(x) -> mpfr\n\n"
  1264. "Return gamma of x.");
  1265. MPFR_UNIOP(gamma)
  1266. PyDoc_STRVAR(doc_g_mpfr_lngamma,
  1267. "lngamma(x) -> mpfr\n\n"
  1268. "Return logarithm of gamma(x).");
  1269. MPFR_UNIOP(lngamma)
  1270. PyDoc_STRVAR(doc_g_mpfr_lgamma,
  1271. "lgamma(x) -> (mpfr, int)\n\n"
  1272. "Return a tuple containing the logarithm of the absolute value of\n"
  1273. "gamma(x) and the sign of gamma(x)");
  1274. static PyObject *
  1275. Pympfr_lgamma(PyObject* self, PyObject *other)
  1276. {
  1277. PyObject *result;
  1278. PympfrObject *value;
  1279. int signp = 0;
  1280. PARSE_ONE_MPFR_OTHER("lgamma() requires 'mpfr' argument");
  1281. value = (PympfrObject*)Pympfr_new(0);
  1282. result = PyTuple_New(2);
  1283. if (!value || !result)
  1284. goto done;
  1285. mpfr_clear_flags();
  1286. value->rc = mpfr_lgamma(value->f, &signp, Pympfr_AS_MPFR(self),
  1287. context->ctx.mpfr_round);
  1288. SUBNORMALIZE(value);
  1289. MERGE_FLAGS;
  1290. CHECK_FLAGS("lgamma()");
  1291. done:
  1292. Py_DECREF(self);
  1293. if (PyErr_Occurred()) {
  1294. Py_XDECREF(result);
  1295. Py_XDECREF((PyObject*)value);
  1296. result = NULL;
  1297. }
  1298. else {
  1299. PyTuple_SET_ITEM(result, 0, (PyObject*)value);
  1300. PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong((long)signp));
  1301. }
  1302. return result;
  1303. }
  1304. PyDoc_STRVAR(doc_g_mpfr_digamma,
  1305. "digamma(x) -> mpfr\n\n"
  1306. "Return digamma of x.");
  1307. MPFR_UNIOP(digamma)
  1308. PyDoc_STRVAR(doc_g_mpfr_zeta,
  1309. "zeta(x) -> mpfr\n\n"
  1310. "Return Riemann zeta of x.");
  1311. MPFR_UNIOP(zeta)
  1312. PyDoc_STRVAR(doc_g_mpfr_erf,
  1313. "erf(x) -> mpfr\n\n"
  1314. "Return error function of x.");
  1315. MPFR_UNIOP(erf)
  1316. PyDoc_STRVAR(doc_g_mpfr_erfc,
  1317. "erfc(x) -> mpfr\n\n"
  1318. "Return complementary error function of x.");
  1319. MPFR_UNIOP(erfc)
  1320. PyDoc_STRVAR(doc_g_mpfr_j0,
  1321. "j0(x) -> mpfr\n\n"
  1322. "Return first kind Bessel function of order 0 of x.");
  1323. MPFR_UNIOP(j0)
  1324. PyDoc_STRVAR(doc_g_mpfr_j1,
  1325. "j1(x) -> mpfr\n\n"
  1326. "Return first kind Bessel function of order 1 of x.");
  1327. MPFR_UNIOP(j1)
  1328. PyDoc_STRVAR(doc_g_mpfr_jn,
  1329. "jn(x,n) -> mpfr\n\n"
  1330. "Return the first kind Bessel function of order n of x.");
  1331. static PyObject *
  1332. Pympfr_jn(PyObject *self, PyObject *args)
  1333. {
  1334. PympfrObject *result;
  1335. long n = 0;
  1336. PARSE_ONE_MPFR_REQ_CLONG(&n, "jn() requires 'mpfr','int' arguments");
  1337. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1338. goto done;
  1339. mpfr_clear_flags();
  1340. result->rc = mpfr_jn(result->f, n, Pympfr_AS_MPFR(self),
  1341. context->ctx.mpfr_round);
  1342. MPFR_CLEANUP_SELF("jn()");
  1343. }
  1344. PyDoc_STRVAR(doc_g_mpfr_y0,
  1345. "y0(x) -> mpfr\n\n"
  1346. "Return second kind Bessel function of order 0 of x.");
  1347. MPFR_UNIOP(y0)
  1348. PyDoc_STRVAR(doc_g_mpfr_y1,
  1349. "y1(x) -> mpfr\n\n"
  1350. "Return second kind Bessel function of order 1 of x.");
  1351. MPFR_UNIOP(y1)
  1352. PyDoc_STRVAR(doc_g_mpfr_yn,
  1353. "yn(x,n) -> mpfr\n\n"
  1354. "Return the second kind Bessel function of order n of x.");
  1355. static PyObject *
  1356. Pympfr_yn(PyObject *self, PyObject *args)
  1357. {
  1358. PympfrObject *result;
  1359. long n = 0;
  1360. PARSE_ONE_MPFR_REQ_CLONG(&n, "yn() requires 'mpfr','int' arguments");
  1361. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1362. goto done;
  1363. mpfr_clear_flags();
  1364. result->rc = mpfr_yn(result->f, n, Pympfr_AS_MPFR(self),
  1365. context->ctx.mpfr_round);
  1366. MPFR_CLEANUP_SELF("yn()");
  1367. }
  1368. PyDoc_STRVAR(doc_g_mpfr_ai,
  1369. "ai(x) -> mpfr\n\n"
  1370. "Return Airy function of x.");
  1371. MPFR_UNIOP(ai)
  1372. static PyObject *
  1373. Pympfr_add_fast(PyObject *x, PyObject *y)
  1374. {
  1375. PympfrObject *result;
  1376. if (Pympfr_CheckAndExp(x) && Pympfr_CheckAndExp(y)) {
  1377. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1378. return NULL;
  1379. }
  1380. mpfr_clear_flags();
  1381. result->rc = mpfr_add(result->f,
  1382. Pympfr_AS_MPFR(x),
  1383. Pympfr_AS_MPFR(y),
  1384. context->ctx.mpfr_round);
  1385. MPFR_CLEANUP_RESULT("addition");
  1386. return (PyObject*)result;
  1387. }
  1388. else {
  1389. return Pybasic_add(x, y);
  1390. }
  1391. }
  1392. static PyObject *
  1393. Pympfr_add(PyObject *self, PyObject *args)
  1394. {
  1395. PympfrObject *result;
  1396. PyObject *other;
  1397. PARSE_TWO_MPFR_ARGS(other, "add() requires 'mpfr','mpfr' arguments");
  1398. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1399. goto done;
  1400. mpfr_clear_flags();
  1401. result->rc = mpfr_add(result->f, Pympfr_AS_MPFR(self),
  1402. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1403. MPFR_CLEANUP_SELF_OTHER("add()");
  1404. }
  1405. static PyObject *
  1406. Pympfr_sub_fast(PyObject *x, PyObject *y)
  1407. {
  1408. PympfrObject *result;
  1409. if (Pympfr_CheckAndExp(x) && Pympfr_CheckAndExp(y)) {
  1410. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1411. return NULL;
  1412. }
  1413. mpfr_clear_flags();
  1414. result->rc = mpfr_sub(result->f,
  1415. Pympfr_AS_MPFR(x),
  1416. Pympfr_AS_MPFR(y),
  1417. context->ctx.mpfr_round);
  1418. MPFR_CLEANUP_RESULT("subtraction");
  1419. return (PyObject*)result;
  1420. }
  1421. else {
  1422. return Pybasic_sub(x, y);
  1423. }
  1424. }
  1425. static PyObject *
  1426. Pympfr_sub(PyObject *self, PyObject *args)
  1427. {
  1428. PympfrObject *result;
  1429. PyObject *other;
  1430. PARSE_TWO_MPFR_ARGS(other, "sub() requires 'mpfr','mpfr' arguments");
  1431. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1432. goto done;
  1433. mpfr_clear_flags();
  1434. result->rc = mpfr_sub(result->f, Pympfr_AS_MPFR(self),
  1435. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1436. MPFR_CLEANUP_SELF_OTHER("sub()");
  1437. }
  1438. static PyObject *
  1439. Pympfr_mul_fast(PyObject *x, PyObject *y)
  1440. {
  1441. PympfrObject *result;
  1442. if (Pympfr_CheckAndExp(x) && Pympfr_CheckAndExp(y)) {
  1443. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1444. return NULL;
  1445. }
  1446. mpfr_clear_flags();
  1447. result->rc = mpfr_mul(result->f,
  1448. Pympfr_AS_MPFR(x),
  1449. Pympfr_AS_MPFR(y),
  1450. context->ctx.mpfr_round);
  1451. MPFR_CLEANUP_RESULT("multiplication");
  1452. return (PyObject*)result;
  1453. }
  1454. else {
  1455. return Pybasic_mul(x, y);
  1456. }
  1457. }
  1458. static PyObject *
  1459. Pympfr_mul(PyObject *self, PyObject *args)
  1460. {
  1461. PympfrObject *result;
  1462. PyObject *other;
  1463. PARSE_TWO_MPFR_ARGS(other, "mul() requires 'mpfr','mpfr' arguments");
  1464. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1465. goto done;
  1466. mpfr_clear_flags();
  1467. result->rc = mpfr_mul(result->f, Pympfr_AS_MPFR(self),
  1468. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1469. MPFR_CLEANUP_SELF_OTHER("mul()");
  1470. }
  1471. static PyObject *
  1472. Pympfr_truediv_fast(PyObject *x, PyObject *y)
  1473. {
  1474. PympfrObject *result;
  1475. if (Pympfr_Check