PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gmpy_mpfr.c

http://gmpy.googlecode.com/
C | 2919 lines | 2354 code | 492 blank | 73 comment | 356 complexity | dc9c4e536aaf2cff3e22b6ad4dd1a020 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1
  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_CheckAndExp(x) && Pympfr_CheckAndExp(y)) {
  1476. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1477. return NULL;
  1478. }
  1479. mpfr_clear_flags();
  1480. result->rc = mpfr_div(result->f,
  1481. Pympfr_AS_MPFR(x),
  1482. Pympfr_AS_MPFR(y),
  1483. context->ctx.mpfr_round);
  1484. MPFR_CLEANUP_RESULT("division");
  1485. return (PyObject*)result;
  1486. }
  1487. else {
  1488. return Pybasic_truediv(x, y);
  1489. }
  1490. }
  1491. #ifdef PY2
  1492. static PyObject *
  1493. Pympfr_div2_fast(PyObject *x, PyObject *y)
  1494. {
  1495. PympfrObject *result;
  1496. if (Pympfr_CheckAndExp(x) && Pympfr_CheckAndExp(y)) {
  1497. if (!(result = (PympfrObject*)Pympfr_new(0))) {
  1498. return NULL;
  1499. }
  1500. mpfr_clear_flags();
  1501. result->rc = mpfr_div(result->f,
  1502. Pympfr_AS_MPFR(x),
  1503. Pympfr_AS_MPFR(y),
  1504. context->ctx.mpfr_round);
  1505. MPFR_CLEANUP_RESULT("division");
  1506. return (PyObject*)result;
  1507. }
  1508. else {
  1509. return Pybasic_div2(x, y);
  1510. }
  1511. }
  1512. #endif
  1513. static PyObject *
  1514. Pympfr_div(PyObject *self, PyObject *args)
  1515. {
  1516. PympfrObject *result;
  1517. PyObject *other;
  1518. PARSE_TWO_MPFR_ARGS(other, "div() requires 'mpfr','mpfr' arguments");
  1519. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1520. goto done;
  1521. mpfr_clear_flags();
  1522. result->rc = mpfr_div(result->f, Pympfr_AS_MPFR(self),
  1523. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1524. MPFR_CLEANUP_SELF_OTHER("div()");
  1525. }
  1526. PyDoc_STRVAR(doc_g_mpfr_fmod,
  1527. "fmod(x, y) -> mpfr\n\n"
  1528. "Return x - n*y where n is the integer quotient of x/y, rounded to 0.");
  1529. static PyObject *
  1530. Pympfr_fmod(PyObject *self, PyObject *args)
  1531. {
  1532. PympfrObject *result;
  1533. PyObject *other;
  1534. PARSE_TWO_MPFR_ARGS(other, "fmod() requires 'mpfr','mpfr' arguments");
  1535. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1536. goto done;
  1537. mpfr_clear_flags();
  1538. result->rc = mpfr_fmod(result->f, Pympfr_AS_MPFR(self),
  1539. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1540. MPFR_CLEANUP_SELF_OTHER("fmod()");
  1541. }
  1542. PyDoc_STRVAR(doc_g_mpfr_remainder,
  1543. "remainder(x, y) -> mpfr\n\n"
  1544. "Return x - n*y where n is the integer quotient of x/y, rounded to\n"
  1545. "the nearest integer and ties rounded to even.");
  1546. static PyObject *
  1547. Pympfr_remainder(PyObject *self, PyObject *args)
  1548. {
  1549. PympfrObject *result;
  1550. PyObject *other;
  1551. PARSE_TWO_MPFR_ARGS(other, "remainder() requires 'mpfr','mpfr' arguments");
  1552. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1553. goto done;
  1554. mpfr_clear_flags();
  1555. result->rc = mpfr_remainder(result->f, Pympfr_AS_MPFR(self),
  1556. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1557. MPFR_CLEANUP_SELF_OTHER("remainder()");
  1558. }
  1559. PyDoc_STRVAR(doc_g_mpfr_remquo,
  1560. "remquo(x, y) -> (mpfr, int)\n\n"
  1561. "Return a tuple containing the remainder(x,y) and the low bits of the\n"
  1562. "quotient.");
  1563. static PyObject *
  1564. Pympfr_remquo(PyObject* self, PyObject *args)
  1565. {
  1566. PyObject *result, *other;
  1567. PympfrObject *value;
  1568. long quobits = 0;
  1569. PARSE_TWO_MPFR_ARGS(other, "remquo() requires 'mpfr', 'mpfr' argument");
  1570. value = (PympfrObject*)Pympfr_new(0);
  1571. result = PyTuple_New(2);
  1572. if (!value || !result)
  1573. goto done;
  1574. mpfr_clear_flags();
  1575. value->rc = mpfr_remquo(value->f, &quobits, Pympfr_AS_MPFR(self),
  1576. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1577. SUBNORMALIZE(value);
  1578. MERGE_FLAGS;
  1579. CHECK_FLAGS("remquo()");
  1580. done:
  1581. Py_DECREF(self);
  1582. Py_DECREF(other);
  1583. if (PyErr_Occurred()) {
  1584. Py_XDECREF(result);
  1585. Py_XDECREF((PyObject*)value);
  1586. result = NULL;
  1587. }
  1588. else {
  1589. PyTuple_SET_ITEM(result, 0, (PyObject*)value);
  1590. PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong(quobits));
  1591. }
  1592. return result;
  1593. }
  1594. PyDoc_STRVAR(doc_g_mpfr_frexp,
  1595. "frexp(x) -> (int, mpfr)\n\n"
  1596. "Return a tuple containing the exponent and mantissa of x.");
  1597. static PyObject *
  1598. Pympfr_frexp(PyObject *self, PyObject *other)
  1599. {
  1600. PyObject *result;
  1601. PympfrObject *value;
  1602. mpfr_exp_t exp = 0;
  1603. PARSE_ONE_MPFR_OTHER("frexp() requires 'mpfr' argument");
  1604. value = (PympfrObject*)Pympfr_new(0);
  1605. result = PyTuple_New(2);
  1606. if (!value || !result)
  1607. goto done;
  1608. mpfr_clear_flags();
  1609. value->rc = mpfr_frexp(&exp, value->f, Pympfr_AS_MPFR(self),
  1610. context->ctx.mpfr_round);
  1611. MERGE_FLAGS;
  1612. CHECK_FLAGS("frexp()");
  1613. done:
  1614. Py_DECREF(self);
  1615. Py_DECREF(other);
  1616. if (PyErr_Occurred()) {
  1617. Py_XDECREF(result);
  1618. Py_XDECREF((PyObject*)value);
  1619. result = NULL;
  1620. }
  1621. else {
  1622. PyTuple_SET_ITEM(result, 0, PyIntOrLong_FromSsize_t((Py_ssize_t)exp));
  1623. PyTuple_SET_ITEM(result, 1, (PyObject*)value);
  1624. }
  1625. return result;
  1626. }
  1627. PyDoc_STRVAR(doc_g_mpfr_atan2,
  1628. "atan2(y, x) -> mpfr\n\n"
  1629. "Return arc-tangent of (y/x).");
  1630. static PyObject *
  1631. Pympfr_atan2(PyObject *self, PyObject *args)
  1632. {
  1633. PympfrObject *result;
  1634. PyObject *other;
  1635. PARSE_TWO_MPFR_ARGS(other, "atan2() requires 'mpfr','mpfr' arguments");
  1636. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1637. goto done;
  1638. mpfr_clear_flags();
  1639. result->rc = mpfr_atan2(result->f, Pympfr_AS_MPFR(self),
  1640. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1641. MPFR_CLEANUP_SELF_OTHER("atan2()");
  1642. }
  1643. PyDoc_STRVAR(doc_g_mpfr_agm,
  1644. "agm(x, y) -> mpfr\n\n"
  1645. "Return arithmetic-geometric mean of x and y.");
  1646. static PyObject *
  1647. Pympfr_agm(PyObject *self, PyObject *args)
  1648. {
  1649. PympfrObject *result;
  1650. PyObject *other;
  1651. PARSE_TWO_MPFR_ARGS(other, "agm() requires 'mpfr','mpfr' arguments");
  1652. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1653. goto done;
  1654. mpfr_clear_flags();
  1655. result->rc = mpfr_agm(result->f, Pympfr_AS_MPFR(self),
  1656. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1657. MPFR_CLEANUP_SELF_OTHER("agm()");
  1658. }
  1659. PyDoc_STRVAR(doc_g_mpfr_hypot,
  1660. "hypot(y, x) -> mpfr\n\n"
  1661. "Return square root of (x**2 + y**2).");
  1662. static PyObject *
  1663. Pympfr_hypot(PyObject *self, PyObject *args)
  1664. {
  1665. PympfrObject *result;
  1666. PyObject *other;
  1667. PARSE_TWO_MPFR_ARGS(other, "hypot() requires 'mpfr','mpfr' arguments");
  1668. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1669. goto done;
  1670. mpfr_clear_flags();
  1671. result->rc = mpfr_hypot(result->f, Pympfr_AS_MPFR(self),
  1672. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1673. MPFR_CLEANUP_SELF_OTHER("hypot()");
  1674. }
  1675. PyDoc_STRVAR(doc_g_mpfr_max2,
  1676. "max2(x, y) -> mpfr\n\n"
  1677. "Return the maximum number of x and y. This function is deprecated.\n"
  1678. "Please use maxnum() instead.");
  1679. PyDoc_STRVAR(doc_g_mpfr_maxnum,
  1680. "maxnum(x, y) -> mpfr\n\n"
  1681. "Return the maximum number of x and y. If x and y are not 'mpfr', they are\n"
  1682. "converted to 'mpfr'. The result is rounded to match the current\n"
  1683. "context. If only one of x or y is a number, then that number is returned.");
  1684. static PyObject *
  1685. Pympfr_max2(PyObject *self, PyObject *args)
  1686. {
  1687. PympfrObject *result;
  1688. PyObject *other;
  1689. PARSE_TWO_MPFR_ARGS(other, "max2() requires 'mpfr','mpfr' arguments");
  1690. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1691. goto done;
  1692. mpfr_clear_flags();
  1693. result->rc = mpfr_max(result->f, Pympfr_AS_MPFR(self),
  1694. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1695. MPFR_CLEANUP_SELF_OTHER("max2()");
  1696. }
  1697. PyDoc_STRVAR(doc_g_mpfr_min2,
  1698. "min2(x, y) -> mpfr\n\n"
  1699. "Return the minimum of x and y. This function is deprecated.\n"
  1700. "Please use minnum() instead.");
  1701. PyDoc_STRVAR(doc_g_mpfr_minnum,
  1702. "minnum(x, y) -> mpfr\n\n"
  1703. "Return the minimum of x and y. If x and y are not 'mpfr', they are\n"
  1704. "converted to 'mpfr'. The result is rounded to match the current\n"
  1705. "context. If only one of x or y is a number, then that number is returned.");
  1706. static PyObject *
  1707. Pympfr_min2(PyObject *self, PyObject *args)
  1708. {
  1709. PympfrObject *result;
  1710. PyObject *other;
  1711. PARSE_TWO_MPFR_ARGS(other, "min2() requires 'mpfr','mpfr' arguments");
  1712. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1713. goto done;
  1714. mpfr_clear_flags();
  1715. result->rc = mpfr_min(result->f, Pympfr_AS_MPFR(self),
  1716. Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1717. MPFR_CLEANUP_SELF_OTHER("min2()");
  1718. }
  1719. PyDoc_STRVAR(doc_g_mpfr_nexttoward,
  1720. "next_toward(y, x) -> mpfr\n\n"
  1721. "Return the next 'mpfr' from x in the direction of y.");
  1722. static PyObject *
  1723. Pympfr_nexttoward(PyObject *self, PyObject *args)
  1724. {
  1725. PympfrObject *result;
  1726. PyObject *other;
  1727. PARSE_TWO_MPFR_ARGS(other, "next_toward() requires 'mpfr','mpfr' arguments");
  1728. if (!(result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self)))))
  1729. goto done;
  1730. mpfr_clear_flags();
  1731. mpfr_set(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round);
  1732. mpfr_nexttoward(result->f, Pympfr_AS_MPFR(other));
  1733. result->rc = 0;
  1734. MPFR_CLEANUP_SELF_OTHER("next_toward()");
  1735. }
  1736. PyDoc_STRVAR(doc_g_mpfr_nextabove,
  1737. "next_above(x) -> mpfr\n\n"
  1738. "Return the next 'mpfr' from x toward +Infinity.");
  1739. static PyObject *
  1740. Pympfr_nextabove(PyObject *self, PyObject *other)
  1741. {
  1742. PympfrObject *result;
  1743. PARSE_ONE_MPFR_OTHER("next_above() requires 'mpfr' argument");
  1744. if (!(result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self)))))
  1745. goto done;
  1746. mpfr_clear_flags();
  1747. mpfr_set(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round);
  1748. mpfr_nextabove(result->f);
  1749. result->rc = 0;
  1750. MPFR_CLEANUP_SELF("next_above()");
  1751. }
  1752. PyDoc_STRVAR(doc_g_mpfr_nextbelow,
  1753. "next_below(x) -> mpfr\n\n"
  1754. "Return the next 'mpfr' from x toward -Infinity.");
  1755. static PyObject *
  1756. Pympfr_nextbelow(PyObject *self, PyObject *other)
  1757. {
  1758. PympfrObject *result;
  1759. PARSE_ONE_MPFR_OTHER("next_below() requires 'mpfr' argument");
  1760. if (!(result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self)))))
  1761. goto done;
  1762. mpfr_clear_flags();
  1763. mpfr_set(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round);
  1764. mpfr_nextbelow(result->f);
  1765. result->rc = 0;
  1766. MPFR_CLEANUP_SELF("next_below()");
  1767. }
  1768. static PyObject *
  1769. Pympfr_sin_cos(PyObject *self, PyObject *other)
  1770. {
  1771. PympfrObject *s, *c;
  1772. PyObject *result;
  1773. int code;
  1774. PARSE_ONE_MPFR_OTHER("sin_cos() requires 'mpfr' argument");
  1775. s = (PympfrObject*)Pympfr_new(0);
  1776. c = (PympfrObject*)Pympfr_new(0);
  1777. result = PyTuple_New(2);
  1778. if (!s || !c || !result)
  1779. goto done;
  1780. mpfr_clear_flags();
  1781. code = mpfr_sin_cos(s->f, c->f, Pympfr_AS_MPFR(self),
  1782. context->ctx.mpfr_round);
  1783. s->rc = code & 0x03;
  1784. c->rc = code >> 2;
  1785. if (s->rc == 2) s->rc = -1;
  1786. if (c->rc == 2) c->rc = -1;
  1787. SUBNORMALIZE(s);
  1788. SUBNORMALIZE(c);
  1789. MERGE_FLAGS;
  1790. CHECK_FLAGS("sin_cos()");
  1791. done:
  1792. Py_DECREF(self);
  1793. if (PyErr_Occurred()) {
  1794. Py_XDECREF((PyObject*)s);
  1795. Py_XDECREF((PyObject*)c);
  1796. Py_XDECREF(result);
  1797. result = NULL;
  1798. }
  1799. else {
  1800. PyTuple_SET_ITEM(result, 0, (PyObject*)s);
  1801. PyTuple_SET_ITEM(result, 1, (PyObject*)c);
  1802. }
  1803. return result;
  1804. }
  1805. PyDoc_STRVAR(doc_g_mpfr_sinh_cosh,
  1806. "sinh_cosh(x) -> (mpfr, mpfr)\n\n"
  1807. "Return a tuple containing the hyperbolic sine and cosine of x.");
  1808. static PyObject *
  1809. Pympfr_sinh_cosh(PyObject *self, PyObject *other)
  1810. {
  1811. PympfrObject *s, *c;
  1812. PyObject *result;
  1813. int code;
  1814. PARSE_ONE_MPFR_OTHER("sinh_cosh() requires 'mpfr' argument");
  1815. s = (PympfrObject*)Pympfr_new(0);
  1816. c = (PympfrObject*)Pympfr_new(0);
  1817. result = PyTuple_New(2);
  1818. if (!s || !c || !result)
  1819. goto done;
  1820. mpfr_clear_flags();
  1821. code = mpfr_sinh_cosh(s->f, c->f, Pympfr_AS_MPFR(self),
  1822. context->ctx.mpfr_round);
  1823. s->rc = code & 0x03;
  1824. c->rc = code >> 2;
  1825. if (s->rc == 2) s->rc = -1;
  1826. if (c->rc == 2) c->rc = -1;
  1827. SUBNORMALIZE(s);
  1828. SUBNORMALIZE(c);
  1829. MERGE_FLAGS;
  1830. CHECK_FLAGS("sin_cos()");
  1831. done:
  1832. Py_DECREF(self);
  1833. if (PyErr_Occurred()) {
  1834. Py_XDECREF((PyObject*)s);
  1835. Py_XDECREF((PyObject*)c);
  1836. Py_XDECREF(result);
  1837. result = NULL;
  1838. }
  1839. else {
  1840. PyTuple_SET_ITEM(result, 0, (PyObject*)s);
  1841. PyTuple_SET_ITEM(result, 1, (PyObject*)c);
  1842. }
  1843. return result;
  1844. }
  1845. static PyObject *
  1846. Pympfr_fma(PyObject *self, PyObject *args)
  1847. {
  1848. PympfrObject *result, *x, *y, *z;
  1849. if (PyTuple_GET_SIZE(args) != 3) {
  1850. TYPE_ERROR("fma() requires 'mpfr','mpfr','mpfr' arguments.");
  1851. return NULL;
  1852. }
  1853. result = (PympfrObject*)Pympfr_new(0);
  1854. x = Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0);
  1855. y = Pympfr_From_Real(PyTuple_GET_ITEM(args, 1), 0);
  1856. z = Pympfr_From_Real(PyTuple_GET_ITEM(args, 2), 0);
  1857. if (!result || !x || !y || !z) {
  1858. TYPE_ERROR("fma() requires 'mpfr','mpfr','mpfr' arguments.");
  1859. goto done;
  1860. }
  1861. mpfr_clear_flags();
  1862. result->rc = mpfr_fma(result->f, x->f, y->f, z->f,
  1863. context->ctx.mpfr_round);
  1864. SUBNORMALIZE(result);
  1865. MERGE_FLAGS;
  1866. CHECK_FLAGS("fma()");
  1867. done:
  1868. Py_XDECREF((PyObject*)x);
  1869. Py_XDECREF((PyObject*)y);
  1870. Py_XDECREF((PyObject*)z);
  1871. if (PyErr_Occurred()) {
  1872. Py_XDECREF(result);
  1873. result = NULL;
  1874. }
  1875. return (PyObject*)result;
  1876. }
  1877. static PyObject *
  1878. Pympfr_fms(PyObject *self, PyObject *args)
  1879. {
  1880. PympfrObject *result, *x, *y, *z;
  1881. if (PyTuple_GET_SIZE(args) != 3) {
  1882. TYPE_ERROR("fms() requires 'mpfr','mpfr','mpfr' arguments.");
  1883. return NULL;
  1884. }
  1885. result = (PympfrObject*)Pympfr_new(0);
  1886. x = Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0);
  1887. y = Pympfr_From_Real(PyTuple_GET_ITEM(args, 1), 0);
  1888. z = Pympfr_From_Real(PyTuple_GET_ITEM(args, 2), 0);
  1889. if (!result || !x || !y || !z) {
  1890. TYPE_ERROR("fms() requires 'mpfr','mpfr','mpfr' arguments.");
  1891. goto done;
  1892. }
  1893. mpfr_clear_flags();
  1894. result->rc = mpfr_fms(result->f, x->f, y->f, z->f,
  1895. context->ctx.mpfr_round);
  1896. SUBNORMALIZE(result);
  1897. MERGE_FLAGS;
  1898. CHECK_FLAGS("fms()");
  1899. done:
  1900. Py_XDECREF((PyObject*)x);
  1901. Py_XDECREF((PyObject*)y);
  1902. Py_XDECREF((PyObject*)z);
  1903. if (PyErr_Occurred()) {
  1904. Py_XDECREF(result);
  1905. result = NULL;
  1906. }
  1907. return (PyObject*)result;
  1908. }
  1909. PyDoc_STRVAR(doc_g_mpfr_factorial,
  1910. "factorial(n) -> mpfr\n\n"
  1911. "Return the floating-point approximation to the factorial of n.\n\n"
  1912. "See fac(n) to get the exact integer result.");
  1913. static PyObject *
  1914. Pympfr_factorial(PyObject *self, PyObject *other)
  1915. {
  1916. PympfrObject *result;
  1917. long n;
  1918. n = clong_From_Integer(other);
  1919. if ((n == -1) && PyErr_Occurred()) {
  1920. TYPE_ERROR("factorial() requires 'int' argument");
  1921. return NULL;
  1922. }
  1923. if (n < 0) {
  1924. VALUE_ERROR("factorial() of negative number");
  1925. return NULL;
  1926. }
  1927. if (!(result = (PympfrObject*)Pympfr_new(0)))
  1928. return NULL;
  1929. mpfr_clear_flags();
  1930. mpfr_fac_ui(result->f, n, context->ctx.mpfr_round);
  1931. MERGE_FLAGS;
  1932. CHECK_FLAGS("factorial()");
  1933. done:
  1934. return (PyObject*)result;
  1935. }
  1936. PyDoc_STRVAR(doc_g_mpfr_is_lessgreater,
  1937. "is_lessgreater(x,y) -> boolean\n\n"
  1938. "Return True if x > y or x < y. Return False if x == y or either x\n"
  1939. "and/or y is NaN.");
  1940. static PyObject *
  1941. Pympfr_is_lessgreater(PyObject *self, PyObject *args)
  1942. {
  1943. PyObject *other;
  1944. int temp;
  1945. PARSE_TWO_MPFR_ARGS(other, "is_lessgreater() requires 'mpfr','mpfr' arguments");
  1946. temp = mpfr_lessgreater_p(Pympfr_AS_MPFR(self), Pympfr_AS_MPFR(other));
  1947. Py_DECREF(self);
  1948. Py_DECREF(other);
  1949. if (temp)
  1950. Py_RETURN_TRUE;
  1951. else
  1952. Py_RETURN_FALSE;
  1953. }
  1954. PyDoc_STRVAR(doc_g_mpfr_is_unordered,
  1955. "is_unordered(x,y) -> boolean\n\n"
  1956. "Return True if either x and/or y is NaN.");
  1957. static PyObject *
  1958. Pympfr_is_unordered(PyObject *self, PyObject *args)
  1959. {
  1960. PyObject *other;
  1961. int temp;
  1962. PARSE_TWO_MPFR_ARGS(other, "unordered() requires 'mpfr','mpfr' arguments");
  1963. temp = mpfr_unordered_p(Pympfr_AS_MPFR(self), Pympfr_AS_MPFR(other));
  1964. Py_DECREF(self);
  1965. Py_DECREF(other);
  1966. if (temp)
  1967. Py_RETURN_TRUE;
  1968. else
  1969. Py_RETURN_FALSE;
  1970. }
  1971. PyDoc_STRVAR(doc_g_mpfr_check_range,
  1972. "check_range(x) -> mpfr\n\n"
  1973. "Return a new 'mpfr' with exponent that lies within the current range\n"
  1974. "of emin and emax.");
  1975. static PyObject *
  1976. Pympfr_check_range(PyObject *self, PyObject *other)
  1977. {
  1978. PympfrObject *result = NULL;
  1979. if (self && Pympfr_Check(self)) {
  1980. if ((result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(self))))) {
  1981. mpfr_set(result->f, Pympfr_AS_MPFR(self), context->ctx.mpfr_round);
  1982. result->round_mode = ((PympfrObject*)self)->round_mode;
  1983. result->rc = ((PympfrObject*)self)->rc;
  1984. mpfr_clear_flags();
  1985. result->rc = mpfr_check_range(result->f, result->rc,
  1986. result->round_mode);
  1987. }
  1988. }
  1989. else if (Pympfr_Check(other)) {
  1990. if ((result = (PympfrObject*)Pympfr_new(mpfr_get_prec(Pympfr_AS_MPFR(other))))) {
  1991. mpfr_set(result->f, Pympfr_AS_MPFR(other), context->ctx.mpfr_round);
  1992. result->round_mode = ((PympfrObject*)other)->round_mode;
  1993. result->rc = ((PympfrObject*)other)->rc;
  1994. mpfr_clear_flags();
  1995. result->rc = mpfr_check_range(result->f, result->rc,
  1996. result->round_mode);
  1997. }
  1998. }
  1999. else {
  2000. TYPE_ERROR("check_range() requires 'mpfr' argument");
  2001. }
  2002. MERGE_FLAGS;
  2003. CHECK_FLAGS("check_range()");
  2004. done:
  2005. return (PyObject*)result;
  2006. }
  2007. PyDoc_STRVAR(doc_g_mpfr_fsum,
  2008. "fsum(iterable) -> mpfr\n\n"
  2009. "Return an accurate sum of the values in the iterable.");
  2010. static PyObject *
  2011. Pympfr_fsum(PyObject *self, PyObject *other)
  2012. {
  2013. PympfrObject *temp, *result;
  2014. mpfr_ptr *tab;
  2015. int errcode;
  2016. Py_ssize_t i, seq_length = 0;
  2017. if (!(result = (PympfrObject*)Pympfr_new(0)))
  2018. return NULL;
  2019. if (!(other = PySequence_List(other))) {
  2020. Py_DECREF((PyObject*)result);
  2021. TYPE_ERROR("argument must be an iterable");
  2022. return NULL;
  2023. }
  2024. /* other contains a new list containing all the values from the
  2025. * iterable. Now make sure each item in the list is an mpfr.
  2026. */
  2027. seq_length = PyList_GET_SIZE(other);
  2028. for (i=0; i < seq_length; i++) {
  2029. if (!(temp = Pympfr_From_Real(PyList_GET_ITEM(other, i), 0))) {
  2030. Py_DECREF(other);
  2031. Py_DECREF((PyObject*)result);
  2032. TYPE_ERROR("all items in iterable must be real numbers");
  2033. return NULL;
  2034. }
  2035. errcode = PyList_SetItem(other, i,(PyObject*)temp);
  2036. if (errcode < 0) {
  2037. Py_DECREF(other);
  2038. Py_DECREF((PyObject*)result);
  2039. TYPE_ERROR("all items in iterable must be real numbers");
  2040. return NULL;
  2041. }
  2042. }
  2043. /* create an array of pointers to the mpfr_t field of a Pympfr object */
  2044. if (!(tab = (mpfr_ptr *)GMPY_MALLOC((sizeof(mpfr_srcptr) * seq_length)))) {
  2045. Py_DECREF(other);
  2046. Py_DECREF((PyObject*)result);
  2047. return PyErr_NoMemory();
  2048. }
  2049. for (i=0; i < seq_length; i++) {
  2050. temp = (PympfrObject*)PyList_GET_ITEM(other, i);
  2051. tab[i] = temp->f;
  2052. }
  2053. result->rc = mpfr_sum(result->f, tab, seq_length, context->ctx.mpfr_round);
  2054. Py_DECREF(other);
  2055. GMPY_FREE(tab);
  2056. return (PyObject*)result;
  2057. }
  2058. PyDoc_STRVAR(doc_g_mpfr_degrees,
  2059. "degrees(x) -> mpfr\n\n"
  2060. "Convert angle x from radians to degrees.");
  2061. static PyObject *
  2062. Pympfr_degrees(PyObject *self, PyObject *other)
  2063. {
  2064. PympfrObject *result, *temp;
  2065. PARSE_ONE_MPFR_OTHER("degrees() requires 'mpfr' argument");
  2066. result = (PympfrObject*)Pympfr_new(0);
  2067. temp = (PympfrObject*)Pympfr_new(context->ctx.mpfr_prec + 20);
  2068. if (!result || !temp) {
  2069. Py_XDECREF((PyObject*)temp);
  2070. Py_XDECREF((PyObject*)result);
  2071. Py_DECREF(other);
  2072. return NULL;
  2073. }
  2074. mpfr_clear_flags();
  2075. mpfr_const_pi(temp->f, MPFR_RNDN);
  2076. mpfr_ui_div(temp->f, 180, temp->f, MPFR_RNDN);
  2077. mpfr_mul(result->f, temp->f, Pympfr_AS_MPFR(self), MPFR_RNDN);
  2078. Py_DECREF((PyObject*)temp);
  2079. MPFR_CLEANUP_SELF("degrees()");
  2080. }
  2081. PyDoc_STRVAR(doc_g_mpfr_radians,
  2082. "radians(x) -> mpfr\n\n"
  2083. "Convert angle x from degrees to radians.");
  2084. static PyObject *
  2085. Pympfr_radians(PyObject *self, PyObject *other)
  2086. {
  2087. PympfrObject *result, *temp;
  2088. PARSE_ONE_MPFR_OTHER("radians() requires 'mpfr' argument");
  2089. result = (PympfrObject*)Pympfr_new(0);
  2090. temp = (PympfrObject*)Pympfr_new(context->ctx.mpfr_prec + 20);
  2091. if (!result || !temp) {
  2092. Py_XDECREF((PyObject*)temp);
  2093. Py_XDECREF((PyObject*)result);
  2094. Py_DECREF(other);
  2095. return NULL;
  2096. }
  2097. mpfr_clear_flags();
  2098. mpfr_const_pi(temp->f, MPFR_RNDN);
  2099. mpfr_div_ui(temp->f, temp->f, 180, MPFR_RNDN);
  2100. mpfr_mul(result->f, Pympfr_AS_MPFR(self), temp->f, MPFR_RNDN);
  2101. Py_DECREF((PyObject*)temp);
  2102. MPFR_CLEANUP_SELF("radians()");
  2103. }
  2104. PyDoc_STRVAR(doc_mpfr_format,
  2105. "x.__format__(fmt) -> string\n\n"
  2106. "Return a Python string by formatting 'x' using the format string\n"
  2107. "'fmt'. A valid format string consists of:\n"
  2108. " optional alignment code:\n"
  2109. " '<' -> left shifted in field\n"
  2110. " '>' -> right shifted in field\n"
  2111. " '^' -> centered in field\n"
  2112. " optional leading sign code\n"
  2113. " '+' -> always display leading sign\n"
  2114. " '-' -> only display minus for negative values\n"
  2115. " ' ' -> minus for negative values, space for positive values\n"
  2116. " optional width.precision\n"
  2117. " optional rounding mode:\n"
  2118. " 'U' -> round toward plus Infinity\n"
  2119. " 'D' -> round toward minus Infinity\n"
  2120. " 'Y' -> round away from zero\n"
  2121. " 'Z' -> round toward zero\n"
  2122. " 'N' -> round to nearest\n"
  2123. " optional conversion code:\n"
  2124. " 'a','A' -> hex format\n"
  2125. " 'b' -> binary format\n"
  2126. " 'e','E' -> scientific format\n"
  2127. " 'f','F' -> fixed point format\n"
  2128. " 'g','G' -> fixed or float format\n\n"
  2129. "The default format is '.6f'.");
  2130. static PyObject *
  2131. Pympfr_format(PyObject *self, PyObject *args)
  2132. {
  2133. PyObject *result = 0, *mpfrstr = 0;
  2134. char *buffer = 0, *newbuf = 0, *fmtcode = 0, *p1, *p2, *p3;
  2135. char mpfrfmt[100], fmt[30];
  2136. int buflen;
  2137. int seensign = 0, seenalign = 0, seendecimal = 0, seendigits = 0;
  2138. int seenround = 0, seenconv = 0;
  2139. if (!Pympfr_Check(self)) {
  2140. TYPE_ERROR("requires mpfr type");
  2141. return NULL;
  2142. }
  2143. if (!PyArg_ParseTuple(args, "s", &fmtcode))
  2144. return NULL;
  2145. p2 = mpfrfmt;
  2146. p3 = fmt;
  2147. *(p2++) = '%';
  2148. for (p1 = fmtcode; *p1 != '\00'; p1++) {
  2149. if (*p1 == '<' || *p1 == '>' || *p1 == '^') {
  2150. if (seenalign || seensign || seendecimal || seendigits || seenround) {
  2151. VALUE_ERROR("Invalid conversion specification");
  2152. return NULL;
  2153. }
  2154. else {
  2155. *(p3++) = *p1;
  2156. seenalign = 1;
  2157. continue;
  2158. }
  2159. }
  2160. if (*p1 == '+' || *p1 == ' ') {
  2161. if (seensign || seendecimal || seendigits || seenround) {
  2162. VALUE_ERROR("Invalid conversion specification");
  2163. return NULL;
  2164. }
  2165. else {
  2166. *(p2++) = *p1;
  2167. seensign = 1;
  2168. continue;
  2169. }
  2170. }
  2171. if (*p1 == '-') {
  2172. if (seensign || seendecimal || seendigits || seenround) {
  2173. VALUE_ERROR("Invalid conversion specification");
  2174. return NULL;
  2175. }
  2176. else {
  2177. seensign = 1;
  2178. continue;
  2179. }
  2180. }
  2181. if (*p1 == '.') {
  2182. if (seendecimal || seendigits || seenround) {
  2183. VALUE_ERROR("Invalid conversion specification");
  2184. return NULL;
  2185. }
  2186. else {
  2187. *(p2++) = *p1;
  2188. seendecimal = 1;
  2189. continue;
  2190. }
  2191. }
  2192. if (isdigit(*p1)) {
  2193. if (seendigits || seenround) {
  2194. VALUE_ERROR("Invalid conversion specification");
  2195. return NULL;
  2196. }
  2197. else if (seendecimal) {
  2198. *(p2++) = *p1;
  2199. continue;
  2200. }
  2201. else {
  2202. if (p3 == fmt) {
  2203. *(p3++) = '>';
  2204. seenalign = 1;
  2205. }
  2206. *(p3++) = *p1;
  2207. continue;
  2208. }
  2209. }
  2210. if (!seendigits) {
  2211. seendigits = 1;
  2212. *(p2++) = 'R';
  2213. }
  2214. if (*p1 == 'U' || *p1 == 'D' || *p1 == 'Y' || *p1 == 'Z' ||
  2215. *p1 == 'N' ) {
  2216. if (seenround) {
  2217. VALUE_ERROR("Invalid conversion specification");
  2218. return NULL;
  2219. }
  2220. else {
  2221. *(p2++) = *p1;
  2222. seenround = 1;
  2223. continue;
  2224. }
  2225. }
  2226. if (*p1 == 'a' || *p1 == 'A' || *p1 == 'b' || *p1 == 'e' ||
  2227. *p1 == 'E' || *p1 == 'f' || *p1 == 'F' || *p1 == 'g' ||
  2228. *p1 == 'G' ) {
  2229. *(p2++) = *p1;
  2230. seenconv = 1;
  2231. break;
  2232. }
  2233. VALUE_ERROR("Invalid conversion specification");
  2234. return NULL;
  2235. }
  2236. if (!seendigits)
  2237. *(p2++) = 'R';
  2238. if (!seenconv)
  2239. *(p2++) = 'f';
  2240. *(p2) = '\00';
  2241. *(p3) = '\00';
  2242. buflen = mpfr_asprintf(&buffer, mpfrfmt, Pympfr_AS_MPFR(self));
  2243. /* If there isn't a decimal point in the output and the output
  2244. * only consists of digits, then append .0 */
  2245. if (strlen(buffer) == strspn(buffer, "+- 0123456789")) {
  2246. newbuf = GMPY_MALLOC(buflen + 3);
  2247. if (!newbuf) {
  2248. mpfr_free_str(buffer);
  2249. return PyErr_NoMemory();
  2250. }
  2251. *newbuf = '\0';
  2252. strcat(newbuf, buffer);
  2253. strcat(newbuf, ".0");
  2254. mpfr_free_str(buffer);
  2255. mpfrstr = Py_BuildValue("s", newbuf);
  2256. GMPY_FREE(newbuf);
  2257. }
  2258. else {
  2259. mpfrstr = Py_BuildValue("s", buffer);
  2260. mpfr_free_str(buffer);
  2261. }
  2262. if (!mpfrstr) {
  2263. return NULL;
  2264. }
  2265. result = PyObject_CallMethod(mpfrstr, "__format__", "(s)", fmt);
  2266. Py_DECREF(mpfrstr);
  2267. return result;
  2268. }
  2269. PyDoc_STRVAR(doc_mpfr_sizeof,
  2270. "x.__sizeof__()\n\n"
  2271. "Returns the amount of memory consumed by x.");
  2272. static PyObject *
  2273. Pympfr_sizeof(PyObject *self, PyObject *other)
  2274. {
  2275. return PyIntOrLong_FromSize_t(sizeof(PympfrObject) + \
  2276. (((Pympfr_AS_MPFR(self))->_mpfr_prec + mp_bits_per_limb - 1) / \
  2277. mp_bits_per_limb) * sizeof(mp_limb_t));
  2278. }
  2279. #ifdef PY3
  2280. static PyNumberMethods mpfr_number_methods =
  2281. {
  2282. (binaryfunc) Pympfr_add_fast, /* nb_add */
  2283. (binaryfunc) Pympfr_sub_fast, /* nb_subtract */
  2284. (binaryfunc) Pympfr_mul_fast, /* nb_multiply */
  2285. (binaryfunc) Pybasic_rem, /* nb_remainder */
  2286. (binaryfunc) Pybasic_divmod, /* nb_divmod */
  2287. (ternaryfunc) Pympany_pow, /* nb_power */
  2288. (unaryfunc) Pympfr_neg, /* nb_negative */
  2289. (unaryfunc) Pympfr_pos, /* nb_positive */
  2290. (unaryfunc) Pympfr_abs, /* nb_absolute */
  2291. (inquiry) Pympfr_nonzero, /* nb_bool */
  2292. 0, /* nb_invert */
  2293. 0, /* nb_lshift */
  2294. 0, /* nb_rshift */
  2295. 0, /* nb_and */
  2296. 0, /* nb_xor */
  2297. 0, /* nb_or */
  2298. (unaryfunc) Pympfr_To_PyLong, /* nb_int */
  2299. 0, /* nb_reserved */
  2300. (unaryfunc) Pympfr_To_PyFloat, /* nb_float */
  2301. 0, /* nb_inplace_add */
  2302. 0, /* nb_inplace_subtract */
  2303. 0, /* nb_inplace_multiply */
  2304. 0, /* nb_inplace_remainder */
  2305. 0, /* nb_inplace_power */
  2306. 0, /* nb_inplace_lshift */
  2307. 0, /* nb_inplace_rshift */
  2308. 0, /* nb_inplace_and */
  2309. 0, /* nb_inplace_xor */
  2310. 0, /* nb_inplace_or */
  2311. (binaryfunc) Pybasic_floordiv, /* nb_floor_divide */
  2312. (binaryfunc) Pympfr_truediv_fast, /* nb_true_divide */
  2313. 0, /* nb_inplace_floor_divide */
  2314. 0, /* nb_inplace_true_divide */
  2315. 0, /* nb_index */
  2316. };
  2317. #else
  2318. static PyNumberMethods mpfr_number_methods =
  2319. {
  2320. (binaryfunc) Pympfr_add_fast, /* nb_add */
  2321. (binaryfunc) Pympfr_sub_fast, /* nb_subtract */
  2322. (binaryfunc) Pympfr_mul_fast, /* nb_multiply */
  2323. (binaryfunc) Pympfr_div2_fast, /* nb_divide */
  2324. (binaryfunc) Pybasic_rem, /* nb_remainder */
  2325. (binaryfunc) Pybasic_divmod, /* nb_divmod */
  2326. (ternaryfunc) Pympany_pow, /* nb_power */
  2327. (unaryfunc) Pympfr_neg, /* nb_negative */
  2328. (unaryfunc) Pympfr_pos, /* nb_positive */
  2329. (unaryfunc) Pympfr_abs, /* nb_absolute */
  2330. (inquiry) Pympfr_nonzero, /* nb_bool */
  2331. 0, /* nb_invert */
  2332. 0, /* nb_lshift */
  2333. 0, /* nb_rshift */
  2334. 0, /* nb_and */
  2335. 0, /* nb_xor */
  2336. 0, /* nb_or */
  2337. 0, /* nb_coerce */
  2338. (unaryfunc) Pympfr_To_PyInt, /* nb_int */
  2339. (unaryfunc) Pympfr_To_PyLong, /* nb_long */
  2340. (unaryfunc) Pympfr_To_PyFloat, /* nb_float */
  2341. 0, /* nb_oct */
  2342. 0, /* nb_hex */
  2343. 0, /* nb_inplace_add */
  2344. 0, /* nb_inplace_subtract */
  2345. 0, /* nb_inplace_multiply */
  2346. 0, /* nb_inplace_divide */
  2347. 0, /* nb_inplace_remainder */
  2348. 0, /* nb_inplace_power */
  2349. 0, /* nb_inplace_lshift */
  2350. 0, /* nb_inplace_rshift */
  2351. 0, /* nb_inplace_and */
  2352. 0, /* nb_inplace_xor */
  2353. 0, /* nb_inplace_or */
  2354. (binaryfunc) Pybasic_floordiv, /* nb_floor_divide */
  2355. (binaryfunc) Pympfr_truediv_fast, /* nb_true_divide */
  2356. 0, /* nb_inplace_floor_divide */
  2357. 0, /* nb_inplace_true_divide */
  2358. };
  2359. #endif
  2360. static PyGetSetDef Pympfr_getseters[] =
  2361. {
  2362. {"precision", (getter)Pympfr_getprec_attrib, NULL, "precision in bits", NULL},
  2363. {"rc", (getter)Pympfr_getrc_attrib, NULL, "return code", NULL},
  2364. {"imag", (getter)Pympfr_getimag_attrib, NULL, "imaginary component", NULL},
  2365. {"real", (getter)Pympfr_getreal_attrib, NULL, "real component", NULL},
  2366. {NULL}
  2367. };
  2368. static PyMethodDef Pympfr_methods [] =
  2369. {
  2370. { "__ceil__", Pympfr_ceil, METH_NOARGS, doc_mpfr_ceil },
  2371. { "__floor__", Pympfr_floor, METH_NOARGS, doc_mpfr_floor },
  2372. { "__format__", Pympfr_format, METH_VARARGS, doc_mpfr_format },
  2373. { "__round__", Pympfr_round10, METH_VARARGS, doc_g_mpfr_round10 },
  2374. { "__sizeof__", Pympfr_sizeof, METH_NOARGS, doc_mpfr_sizeof },
  2375. { "__trunc__", Pympfr_trunc, METH_NOARGS, doc_mpfr_trunc },
  2376. { "as_integer_ratio", Pympfr_integer_ratio, METH_NOARGS, doc_mpfr_integer_ratio },
  2377. { "as_mantissa_exp", Pympfr_mantissa_exp, METH_NOARGS, doc_mpfr_mantissa_exp },
  2378. { "as_simple_fraction", (PyCFunction)Pympfr_simple_fraction, METH_VARARGS | METH_KEYWORDS, doc_mpfr_simple_fraction },
  2379. { "conjugate", Pympfr_conjugate, METH_NOARGS, doc_mpfr_conjugate },
  2380. { "digits", Pympfr_digits, METH_VARARGS, doc_mpfr_digits },
  2381. { "is_integer", Pympfr_is_integer, METH_NOARGS, doc_mpfr_is_integer },
  2382. { NULL, NULL, 1 }
  2383. };
  2384. static PyTypeObject Pympfr_Type =
  2385. {
  2386. /* PyObject_HEAD_INIT(&PyType_Type) */
  2387. #ifdef PY3
  2388. PyVarObject_HEAD_INIT(NULL, 0)
  2389. #else
  2390. PyObject_HEAD_INIT(0)
  2391. 0, /* ob_size */
  2392. #endif
  2393. "mpfr", /* tp_name */
  2394. sizeof(PympfrObject), /* tp_basicsize */
  2395. 0, /* tp_itemsize */
  2396. /* methods */
  2397. (destructor) Pympfr_dealloc, /* tp_dealloc */
  2398. 0, /* tp_print */
  2399. 0, /* tp_getattr */
  2400. 0, /* tp_setattr */
  2401. 0, /* tp_reserved */
  2402. (reprfunc) Pympfr_To_Repr, /* tp_repr */
  2403. &mpfr_number_methods, /* tp_as_number */
  2404. 0, /* tp_as_sequence */
  2405. 0, /* tp_as_mapping */
  2406. (hashfunc) Pympfr_hash, /* tp_hash */
  2407. 0, /* tp_call */
  2408. (reprfunc) Pympfr_To_Str, /* tp_str */
  2409. (getattrofunc) 0, /* tp_getattro */
  2410. (setattrofunc) 0, /* tp_setattro */
  2411. 0, /* tp_as_buffer */
  2412. #ifdef PY3
  2413. Py_TPFLAGS_DEFAULT, /* tp_flags */
  2414. #else
  2415. Py_TPFLAGS_HAVE_RICHCOMPARE|Py_TPFLAGS_CHECKTYPES, /* tp_flags */
  2416. #endif
  2417. "Multiple precision real", /* tp_doc */
  2418. 0, /* tp_traverse */
  2419. 0, /* tp_clear */
  2420. (richcmpfunc)&mpany_richcompare, /* tp_richcompare */
  2421. 0, /* tp_weaklistoffset*/
  2422. 0, /* tp_iter */
  2423. 0, /* tp_iternext */
  2424. Pympfr_methods, /* tp_methods */
  2425. 0, /* tp_members */
  2426. Pympfr_getseters, /* tp_getset */
  2427. };