/ src / gmpy2_math.c
gmpy2_math.c
   1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
   2   * gmpy2_math.c                                                            *
   3   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
   4   * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision   *
   5   * libraries.                                                              *
   6   *                                                                         *
   7   * Copyright 2000 - 2009 Alex Martelli                                     *
   8   *                                                                         *
   9   * Copyright 2008 - 2021 Case Van Horsen                                   *
  10   *                                                                         *
  11   * This file is part of GMPY2.                                             *
  12   *                                                                         *
  13   * GMPY2 is free software: you can redistribute it and/or modify it under  *
  14   * the terms of the GNU Lesser General Public License as published by the  *
  15   * Free Software Foundation, either version 3 of the License, or (at your  *
  16   * option) any later version.                                              *
  17   *                                                                         *
  18   * GMPY2 is distributed in the hope that it will be useful, but WITHOUT    *
  19   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   *
  20   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public    *
  21   * License for more details.                                               *
  22   *                                                                         *
  23   * You should have received a copy of the GNU Lesser General Public        *
  24   * License along with GMPY2; if not, see <http://www.gnu.org/licenses/>    *
  25   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  26  
  27  PyDoc_STRVAR(GMPy_doc_context_sin,
  28  "context.sin(x) -> number\n\n"
  29  "Return sine of x; x in radians.");
  30  
  31  PyDoc_STRVAR(GMPy_doc_function_sin,
  32  "sin(x) -> number\n\n"
  33  "Return sine of x; x in radians.");
  34  
  35  GMPY_MPFR_MPC_UNIOP_EX(Sin, sin)
  36  
  37  PyDoc_STRVAR(GMPy_doc_context_cos,
  38  "context.cos(x) -> number\n\n"
  39  "Return cosine of x; x in radians.");
  40  
  41  PyDoc_STRVAR(GMPy_doc_function_cos,
  42  "cos(x) -> number\n\n"
  43  "Return cosine of x; x in radians.");
  44  
  45  GMPY_MPFR_MPC_UNIOP_EX(Cos, cos)
  46  
  47  PyDoc_STRVAR(GMPy_doc_context_tan,
  48  "context.tan(x) -> number\n\n"
  49  "Return tangent of x; x in radians.");
  50  
  51  PyDoc_STRVAR(GMPy_doc_function_tan,
  52  "tan(x) -> number\n\n"
  53  "Return tangent of x; x in radians.");
  54  
  55  GMPY_MPFR_MPC_UNIOP_EX(Tan, tan)
  56  
  57  PyDoc_STRVAR(GMPy_doc_context_atan,
  58  "context.atan(x) -> number\n\n"
  59  "Return inverse tangent of x; result in radians.");
  60  
  61  PyDoc_STRVAR(GMPy_doc_function_atan,
  62  "atan(x) -> number\n\n"
  63  "Return inverse tangent of x; result in radians.");
  64  
  65  GMPY_MPFR_MPC_UNIOP_EX(Atan, atan)
  66  
  67  PyDoc_STRVAR(GMPy_doc_context_sinh,
  68  "context.sinh(x) -> number\n\n"
  69  "Return hyperbolic sine of x.");
  70  
  71  PyDoc_STRVAR(GMPy_doc_function_sinh,
  72  "sinh(x) -> number\n\n"
  73  "Return hyperbolic sine of x.");
  74  
  75  GMPY_MPFR_MPC_UNIOP_EX(Sinh, sinh)
  76  
  77  PyDoc_STRVAR(GMPy_doc_context_cosh,
  78  "context.cosh(x) -> number\n\n"
  79  "Return hyperbolic cosine of x.");
  80  
  81  PyDoc_STRVAR(GMPy_doc_function_cosh,
  82  "cosh(x) -> number\n\n"
  83  "Return hyperbolic cosine of x.");
  84  
  85  GMPY_MPFR_MPC_UNIOP_EX(Cosh, cosh)
  86  
  87  PyDoc_STRVAR(GMPy_doc_context_tanh,
  88  "context.tanh(x) -> number\n\n"
  89  "Return hyperbolic tangent of x.");
  90  
  91  PyDoc_STRVAR(GMPy_doc_function_tanh,
  92  "tanh(x) -> number\n\n"
  93  "Return hyperbolic tangent of x.");
  94  
  95  GMPY_MPFR_MPC_UNIOP_EX(Tanh, tanh)
  96  
  97  PyDoc_STRVAR(GMPy_doc_context_asinh,
  98  "context.asinh(x) -> number\n\n"
  99  "Return inverse hyperbolic sine of x.");
 100  
 101  PyDoc_STRVAR(GMPy_doc_function_asinh,
 102  "asinh(x) -> number\n\n"
 103  "Return inverse hyperbolic sine of x.");
 104  
 105  GMPY_MPFR_MPC_UNIOP_EX(Asinh, asinh)
 106  
 107  PyDoc_STRVAR(GMPy_doc_context_acosh,
 108  "context.acosh(x) -> number\n\n"
 109  "Return inverse hyperbolic cosine of x.");
 110  
 111  PyDoc_STRVAR(GMPy_doc_function_acosh,
 112  "acosh(x) -> number\n\n"
 113  "Return inverse hyperbolic cosine of x.");
 114  
 115  GMPY_MPFR_MPC_UNIOP_EX(Acosh, acosh)
 116  
 117  /* Section 2:
 118   * These functions accept a single argument and return an mpfr result.
 119   *
 120   * GMPY_MPFR_UNIOP(NAME, FUNC) creates the following functions:
 121   *     GMPy_Real_NAME(x, context)
 122   *     GMPy_Number_NAME(x, context)
 123   *     GMPy_Context_NAME(self, other)
 124   *     - called with METH_O
 125   */
 126  
 127  PyDoc_STRVAR(GMPy_doc_context_sec,
 128  "context.sec(x) -> number\n\n"
 129  "Return secant of x; x in radians.");
 130  
 131  PyDoc_STRVAR(GMPy_doc_function_sec,
 132  "sec(x) -> number\n\n"
 133  "Return secant of x; x in radians.");
 134  
 135  GMPY_MPFR_UNIOP_EX(Sec, sec)
 136  
 137  PyDoc_STRVAR(GMPy_doc_context_csc,
 138  "context.csc(x) -> number\n\n"
 139  "Return cosecant of x; x in radians.");
 140  
 141  PyDoc_STRVAR(GMPy_doc_function_csc,
 142  "csc(x) -> number\n\n"
 143  "Return cosecant of x; x in radians.");
 144  
 145  GMPY_MPFR_UNIOP_EX(Csc, csc)
 146  
 147  PyDoc_STRVAR(GMPy_doc_context_cot,
 148  "context.cot(x) -> number\n\n"
 149  "Return cotangent of x; x in radians.");
 150  
 151  PyDoc_STRVAR(GMPy_doc_function_cot,
 152  "cot(x) -> number\n\n"
 153  "Return cotangent of x; x in radians.");
 154  
 155  GMPY_MPFR_UNIOP_EX(Cot, cot)
 156  
 157  PyDoc_STRVAR(GMPy_doc_context_sech,
 158  "context.sech(x) -> number\n\n"
 159  "Return hyperbolic secant of x.");
 160  
 161  PyDoc_STRVAR(GMPy_doc_function_sech,
 162  "sech(x) -> number\n\n"
 163  "Return hyperbolic secant of x.");
 164  
 165  GMPY_MPFR_UNIOP_EX(Sech, sech)
 166  
 167  PyDoc_STRVAR(GMPy_doc_context_csch,
 168  "context.csch(x) -> number\n\n"
 169  "Return hyperbolic cosecant of x.");
 170  
 171  PyDoc_STRVAR(GMPy_doc_function_csch,
 172  "csch(x) -> number\n\n"
 173  "Return hyperbolic cosecant of x.");
 174  
 175  GMPY_MPFR_UNIOP_EX(Csch, csch)
 176  
 177  PyDoc_STRVAR(GMPy_doc_context_coth,
 178  "context.coth(x) -> number\n\n"
 179  "Return hyperbolic cotangent of x.");
 180  
 181  PyDoc_STRVAR(GMPy_doc_function_coth,
 182  "coth(x) -> number\n\n"
 183  "Return hyperbolic cotangent of x.");
 184  
 185  GMPY_MPFR_UNIOP_EX(Coth, coth)
 186  
 187  PyDoc_STRVAR(GMPy_doc_context_rec_sqrt,
 188  "context.rec_sqrt(x) -> number\n\n"
 189  "Return the reciprocal of the square root of x.");
 190  
 191  PyDoc_STRVAR(GMPy_doc_function_rec_sqrt,
 192  "rec_sqrt(x) -> number\n\n"
 193  "Return the reciprocal of the square root of x.");
 194  
 195  GMPY_MPFR_UNIOP_EX(RecSqrt, rec_sqrt)
 196  
 197  PyDoc_STRVAR(GMPy_doc_context_rint,
 198  "context.rint(x) -> number\n\n"
 199  "Return x rounded to the nearest integer using the context rounding\n"
 200  "mode.");
 201  
 202  PyDoc_STRVAR(GMPy_doc_function_rint,
 203  "rint(x) -> number\n\n"
 204  "Return x rounded to the nearest integer using the current rounding\n"
 205  "mode.");
 206  
 207  GMPY_MPFR_UNIOP_EX(Rint, rint)
 208  
 209  PyDoc_STRVAR(GMPy_doc_context_rint_ceil,
 210  "context.rint_ceil(x) -> number\n\n"
 211  "Return x rounded to the nearest integer by first rounding to the\n"
 212  "next higher or equal integer and then, if needed, using the context\n"
 213  "rounding mode.");
 214  
 215  PyDoc_STRVAR(GMPy_doc_function_rint_ceil,
 216  "rint_ceil(x) -> number\n\n"
 217  "Return x rounded to the nearest integer by first rounding to the\n"
 218  "next higher or equal integer and then, if needed, using the current\n"
 219  "rounding mode.");
 220  
 221  GMPY_MPFR_UNIOP_EX(RintCeil, rint_ceil)
 222  
 223  PyDoc_STRVAR(GMPy_doc_context_rint_floor,
 224  "context.rint_floor(x) -> number\n\n"
 225  "Return x rounded to the nearest integer by first rounding to the\n"
 226  "next lower or equal integer and then, if needed, using the context\n"
 227  "rounding mode.");
 228  
 229  PyDoc_STRVAR(GMPy_doc_function_rint_floor,
 230  "rint_floor(x) -> number\n\n"
 231  "Return x rounded to the nearest integer by first rounding to the\n"
 232  "next lower or equal integer and then, if needed, using the current\n"
 233  "rounding mode.");
 234  
 235  GMPY_MPFR_UNIOP_EX(RintFloor, rint_floor)
 236  
 237  PyDoc_STRVAR(GMPy_doc_context_rint_round,
 238  "context.rint_round(x) -> number\n\n"
 239  "Return x rounded to the nearest integer by first rounding to the\n"
 240  "nearest integer (ties away from 0) and then, if needed, using\n"
 241  "the context rounding mode.");
 242  
 243  PyDoc_STRVAR(GMPy_doc_function_rint_round,
 244  "rint_round(x) -> number\n\n"
 245  "Return x rounded to the nearest integer by first rounding to the\n"
 246  "nearest integer (ties away from 0) and then, if needed, using\n"
 247  "the current rounding mode.");
 248  
 249  GMPY_MPFR_UNIOP_EX(RintRound, rint_round)
 250  
 251  PyDoc_STRVAR(GMPy_doc_context_rint_trunc,
 252  "context.rint_trunc(x) -> number\n\n"
 253  "Return x rounded to the nearest integer by first rounding towards\n"
 254  "zero and then, if needed, using the context rounding mode.");
 255  
 256  PyDoc_STRVAR(GMPy_doc_function_rint_trunc,
 257  "rint_trunc(x) -> number\n\n"
 258  "Return x rounded to the nearest integer by first rounding towards\n"
 259  "zero and then, if needed, using the current rounding mode.");
 260  
 261  GMPY_MPFR_UNIOP_EX(RintTrunc, rint_trunc)
 262  
 263  PyDoc_STRVAR(GMPy_doc_context_frac,
 264  "context.frac(x) -> number\n\n"
 265  "Return fractional part of x.");
 266  
 267  PyDoc_STRVAR(GMPy_doc_function_frac,
 268  "frac(x) -> number\n\n"
 269  "Return fractional part of x.");
 270  
 271  GMPY_MPFR_UNIOP_EX(Frac, frac)
 272  
 273  PyDoc_STRVAR(GMPy_doc_context_cbrt,
 274  "context.cbrt(x) -> number\n\n"
 275  "Return the cube root of x.");
 276  
 277  PyDoc_STRVAR(GMPy_doc_function_cbrt,
 278  "cbrt(x) -> number\n\n"
 279  "Return the cube root of x.");
 280  
 281  GMPY_MPFR_UNIOP_EX(Cbrt, cbrt)
 282  
 283  PyDoc_STRVAR(GMPy_doc_context_log2,
 284  "context.log2(x) -> number\n\n"
 285  "Return base-2 logarithm of x.");
 286  
 287  PyDoc_STRVAR(GMPy_doc_function_log2,
 288  "log2(x) -> number\n\n"
 289  "Return base-2 logarithm of x.");
 290  
 291  GMPY_MPFR_UNIOP_EX(Log2, log2)
 292  
 293  PyDoc_STRVAR(GMPy_doc_context_exp2,
 294  "context.exp2(x) -> number\n\n"
 295  "Return 2**x.");
 296  
 297  PyDoc_STRVAR(GMPy_doc_function_exp2,
 298  "exp2(x) -> number\n\n"
 299  "Return 2**x.");
 300  
 301  GMPY_MPFR_UNIOP_EX(Exp2, exp2)
 302  
 303  PyDoc_STRVAR(GMPy_doc_context_exp10,
 304  "context.exp10(x) -> number\n\n"
 305  "Return 10**x.");
 306  
 307  PyDoc_STRVAR(GMPy_doc_function_exp10,
 308  "exp10(x) -> number\n\n"
 309  "Return 10**x.");
 310  
 311  GMPY_MPFR_UNIOP_EX(Exp10, exp10)
 312  
 313  PyDoc_STRVAR(GMPy_doc_context_log1p,
 314  "context.log1p(x) -> number\n\n"
 315  "Return natural logarithm of (1+x).");
 316  
 317  PyDoc_STRVAR(GMPy_doc_function_log1p,
 318  "log1p(x) -> number\n\n"
 319  "Return natural logarithm of (1+x).");
 320  
 321  GMPY_MPFR_UNIOP_EX(Log1p, log1p)
 322  
 323  PyDoc_STRVAR(GMPy_doc_context_expm1,
 324  "context.expm1(x) -> number\n\n"
 325  "Return exp(x) - 1.");
 326  
 327  PyDoc_STRVAR(GMPy_doc_function_expm1,
 328  "expm1(x) -> number\n\n"
 329  "Return exp(x) - 1.");
 330  
 331  GMPY_MPFR_UNIOP_EX(Expm1, expm1)
 332  
 333  PyDoc_STRVAR(GMPy_doc_context_eint,
 334  "context.eint(x) -> number\n\n"
 335  "Return exponential integral of x.");
 336  
 337  PyDoc_STRVAR(GMPy_doc_function_eint,
 338  "eint(x) -> number\n\n"
 339  "Return exponential integral of x.");
 340  
 341  GMPY_MPFR_UNIOP_EX(Eint, eint)
 342  
 343  PyDoc_STRVAR(GMPy_doc_context_li2,
 344  "context.li2(x) -> number\n\n"
 345  "Return real part of dilogarithm of x.");
 346  
 347  PyDoc_STRVAR(GMPy_doc_function_li2,
 348  "li2(x) -> number\n\n"
 349  "Return real part of dilogarithm of x.");
 350  
 351  GMPY_MPFR_UNIOP_EX(Li2, li2)
 352  
 353  PyDoc_STRVAR(GMPy_doc_context_gamma,
 354  "context.gamma(x) -> number\n\n"
 355  "Return gamma of x.");
 356  
 357  PyDoc_STRVAR(GMPy_doc_function_gamma,
 358  "gamma(x) -> number\n\n"
 359  "Return gamma of x.");
 360  
 361  GMPY_MPFR_UNIOP_EX(Gamma, gamma)
 362  
 363  PyDoc_STRVAR(GMPy_doc_context_lngamma,
 364  "context.lngamma(x) -> number\n\n"
 365  "Return natural logarithm of gamma(x).");
 366  
 367  PyDoc_STRVAR(GMPy_doc_function_lngamma,
 368  "lngamma(x) -> number\n\n"
 369  "Return natural logarithm of gamma(x).");
 370  
 371  GMPY_MPFR_UNIOP_EX(Lngamma, lngamma)
 372  
 373  PyDoc_STRVAR(GMPy_doc_context_digamma,
 374  "context.digamma(x) -> number\n\n"
 375  "Return digamma of x.");
 376  
 377  PyDoc_STRVAR(GMPy_doc_function_digamma,
 378  "digamma(x) -> number\n\n"
 379  "Return digamma of x.");
 380  
 381  GMPY_MPFR_UNIOP_EX(Digamma, digamma)
 382  
 383  PyDoc_STRVAR(GMPy_doc_context_zeta,
 384  "context.zeta(x) -> number\n\n"
 385  "Return Riemann zeta of x.");
 386  
 387  PyDoc_STRVAR(GMPy_doc_function_zeta,
 388  "zeta(x) -> number\n\n"
 389  "Return Riemann zeta of x.");
 390  
 391  GMPY_MPFR_UNIOP_EX(Zeta, zeta)
 392  
 393  PyDoc_STRVAR(GMPy_doc_context_erf,
 394  "context.erf(x) -> number\n\n"
 395  "Return error function of x.");
 396  
 397  PyDoc_STRVAR(GMPy_doc_function_erf,
 398  "erf(x) -> number\n\n"
 399  "Return error function of x.");
 400  
 401  GMPY_MPFR_UNIOP_EX(Erf, erf)
 402  
 403  PyDoc_STRVAR(GMPy_doc_context_erfc,
 404  "context.erfc(x) -> number\n\n"
 405  "Return complementary error function of x.");
 406  
 407  PyDoc_STRVAR(GMPy_doc_function_erfc,
 408  "erfc(x) -> number\n\n"
 409  "Return complementary error function of x.");
 410  
 411  GMPY_MPFR_UNIOP_EX(Erfc, erfc)
 412  
 413  PyDoc_STRVAR(GMPy_doc_context_j0,
 414  "context.j0(x) -> number\n\n"
 415  "Return first kind Bessel function of order 0 of x.");
 416  
 417  PyDoc_STRVAR(GMPy_doc_function_j0,
 418  "j0(x) -> number\n\n"
 419  "Return first kind Bessel function of order 0 of x.");
 420  
 421  GMPY_MPFR_UNIOP_EX(J0, j0)
 422  
 423  PyDoc_STRVAR(GMPy_doc_context_j1,
 424  "context.j1(x) -> number\n\n"
 425  "Return first kind Bessel function of order 1 of x.");
 426  
 427  PyDoc_STRVAR(GMPy_doc_function_j1,
 428  "j1(x) -> number\n\n"
 429  "Return first kind Bessel function of order 1 of x.");
 430  
 431  GMPY_MPFR_UNIOP_EX(J1, j1)
 432  
 433  PyDoc_STRVAR(GMPy_doc_context_y0,
 434  "context.y0(x) -> number\n\n"
 435  "Return second kind Bessel function of order 0 of x.");
 436  
 437  PyDoc_STRVAR(GMPy_doc_function_y0,
 438  "y0(x) -> number\n\n"
 439  "Return second kind Bessel function of order 0 of x.");
 440  
 441  GMPY_MPFR_UNIOP_EX(Y0, y0)
 442  
 443  PyDoc_STRVAR(GMPy_doc_context_y1,
 444  "context.y1(x) -> number\n\n"
 445  "Return second kind Bessel function of order 1 of x.");
 446  
 447  PyDoc_STRVAR(GMPy_doc_function_y1,
 448  "y1(x) -> number\n\n"
 449  "Return second kind Bessel function of order 1 of x.");
 450  
 451  GMPY_MPFR_UNIOP_EX(Y1, y1)
 452  
 453  PyDoc_STRVAR(GMPy_doc_context_ai,
 454  "context.ai(x) -> number\n\n"
 455  "Return Airy function of x.");
 456  
 457  PyDoc_STRVAR(GMPy_doc_function_ai,
 458  "ai(x) -> number\n\n"
 459  "Return Airy function of x.");
 460  
 461  GMPY_MPFR_UNIOP_EX(Ai, ai)
 462  
 463  /* Section 3:
 464   * The following functions may return an mpc result for certain mpfr arguments.
 465   * Since the expectional values vary between functions, the 'Real' and 'Complex'
 466   * functions do not use macros. However, they do use a macro to create the
 467   * higher-level functions.
 468   *
 469   * GMPY_MPFR_MPC_UNIOP_TEMPLATE(NAME, FUNC) creates the following functions:
 470   *     GMPy_Number_NAME(x, context)
 471   *     - assumes GMPy_Real_NAME & GMPy_Complex_NAME exist
 472   *     GMPy_Context_NAME(self, other)
 473   *     - called with METH_O
 474   */
 475  
 476  PyDoc_STRVAR(GMPy_doc_context_acos,
 477  "context.acos(x) -> number\n\n"
 478  "Return inverse cosine of x; result in radians.");
 479  
 480  PyDoc_STRVAR(GMPy_doc_function_acos,
 481  "acos(x) -> number\n\n"
 482  "Return inverse cosine of x; result in radians.");
 483  
 484  static PyObject *
 485  _GMPy_MPFR_Acos(PyObject *x, CTXT_Object *context)
 486  {
 487      MPFR_Object *result;
 488  
 489      if (!mpfr_nan_p(MPFR(x)) &&
 490              (mpfr_cmp_si(MPFR(x), 1) > 0 || mpfr_cmp_si(MPFR(x), -1) < 0) &&
 491              context->ctx.allow_complex
 492         ) {
 493          return GMPy_Complex_Acos(x, context);
 494      }
 495  
 496      if (!(result = GMPy_MPFR_New(0, context))) {
 497          return NULL;
 498      }
 499  
 500      mpfr_clear_flags();
 501  
 502      result->rc = mpfr_acos(result->f, MPFR(x), GET_MPFR_ROUND(context));
 503      _GMPy_MPFR_Cleanup(&result, context);
 504      return (PyObject*)result;
 505  }
 506  
 507  static PyObject *
 508  GMPy_Real_Acos(PyObject *x, CTXT_Object *context)
 509  {
 510      PyObject *result, *tempx;
 511  
 512      CHECK_CONTEXT(context);
 513  
 514      if (!(tempx = (PyObject*)GMPy_MPFR_From_Real(x, 1, context))) {
 515          return NULL;
 516      }
 517  
 518      result = _GMPy_MPFR_Acos(tempx, context);
 519      Py_DECREF(tempx);
 520      return result;
 521  }
 522  
 523  static PyObject *
 524  _GMPy_MPC_Acos(PyObject *x, CTXT_Object *context)
 525  {
 526      MPC_Object *result = NULL;
 527  
 528      CHECK_CONTEXT(context);
 529  
 530      if (!(result = GMPy_MPC_New(0, 0, context))) {
 531          return NULL;
 532      }
 533  
 534      result->rc = mpc_acos(result->c, MPC(x), GET_MPC_ROUND(context));
 535      _GMPy_MPC_Cleanup(&result, context);
 536      return (PyObject*)result;
 537  }
 538  
 539  static PyObject *
 540  GMPy_Complex_Acos(PyObject *x, CTXT_Object *context)
 541  {
 542      PyObject *result, *tempx;
 543  
 544      CHECK_CONTEXT(context);
 545  
 546      if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) {
 547          return NULL;
 548      }
 549  
 550      result = _GMPy_MPC_Acos(tempx, context);
 551      Py_DECREF(tempx);
 552      return result;
 553  }
 554  
 555  GMPY_MPFR_MPC_UNIOP_TEMPLATE_EX(Acos, acos)
 556  
 557  PyDoc_STRVAR(GMPy_doc_context_asin,
 558  "context.asin(x) -> number\n\n"
 559  "Return inverse sine of x; result in radians.");
 560  
 561  PyDoc_STRVAR(GMPy_doc_function_asin,
 562  "asin(x) -> number\n\n"
 563  "Return inverse sine of x; result in radians.");
 564  
 565  static PyObject *
 566  _GMPy_MPFR_Asin(PyObject *x, CTXT_Object *context)
 567  {
 568      MPFR_Object *result;
 569  
 570      CHECK_CONTEXT(context);
 571  
 572      if (!mpfr_nan_p(MPFR(x)) &&
 573              (mpfr_cmp_si(MPFR(x), 1) > 0 || mpfr_cmp_si(MPFR(x), -1) < 0) &&
 574              context->ctx.allow_complex
 575         ) {
 576          return GMPy_Complex_Asin(x, context);
 577      }
 578  
 579      if (!(result = GMPy_MPFR_New(0, context))) {
 580          return NULL;
 581      }
 582  
 583      mpfr_clear_flags();
 584  
 585      result->rc = mpfr_asin(result->f, MPFR(x), GET_MPFR_ROUND(context));
 586      _GMPy_MPFR_Cleanup(&result, context);
 587      return (PyObject*)result;
 588  }
 589  
 590  static PyObject *
 591  GMPy_Real_Asin(PyObject *x, CTXT_Object *context)
 592  {
 593      PyObject *result, *tempx;
 594  
 595      CHECK_CONTEXT(context);
 596  
 597      if (!(tempx = (PyObject*)GMPy_MPFR_From_Real(x, 1, context))) {
 598          return NULL;
 599      }
 600  
 601      result = _GMPy_MPFR_Asin(tempx, context);
 602      Py_DECREF(tempx);
 603      return result;
 604  }
 605  
 606  static PyObject *
 607  _GMPy_MPC_Asin(PyObject *x, CTXT_Object *context)
 608  {
 609      MPC_Object *result;
 610  
 611      CHECK_CONTEXT(context);
 612  
 613      if (!(result = GMPy_MPC_New(0, 0, context))) {
 614          return NULL;
 615      }
 616  
 617      result->rc = mpc_asin(result->c, MPC(x), GET_MPC_ROUND(context));
 618      _GMPy_MPC_Cleanup(&result, context);
 619      return (PyObject*)result;
 620  }
 621  
 622  static PyObject *
 623  GMPy_Complex_Asin(PyObject *x, CTXT_Object *context)
 624  {
 625      PyObject *result, *tempx;
 626  
 627      CHECK_CONTEXT(context);
 628  
 629      if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) {
 630          return NULL;
 631      }
 632  
 633      result = _GMPy_MPC_Asin(tempx, context);
 634      Py_DECREF(tempx);
 635      return result;
 636  }
 637  
 638  GMPY_MPFR_MPC_UNIOP_TEMPLATE_EX(Asin, asin)
 639  
 640  PyDoc_STRVAR(GMPy_doc_context_atanh,
 641  "context.atanh(x) -> number\n\n"
 642  "Return inverse hyperbolic tanget of x.");
 643  
 644  PyDoc_STRVAR(GMPy_doc_function_atanh,
 645  "atanh(x) -> number\n\n"
 646  "Return inverse hyperbolic tangent of x.");
 647  
 648  static PyObject *
 649  _GMPy_MPFR_Atanh(PyObject *x, CTXT_Object *context)
 650  {
 651      MPFR_Object *result;
 652  
 653      CHECK_CONTEXT(context);
 654  
 655      if (!mpfr_nan_p(MPFR(x)) &&
 656              (mpfr_cmp_si(MPFR(x), 1) > 0 || mpfr_cmp_si(MPFR(x), -1) < 0) &&
 657              context->ctx.allow_complex
 658         ) {
 659          return GMPy_Complex_Atanh(x, context);
 660      }
 661  
 662      if (!(result = GMPy_MPFR_New(0, context))) {
 663          return NULL;
 664      }
 665  
 666      mpfr_clear_flags();
 667  
 668      result->rc = mpfr_atanh(result->f, MPFR(x), GET_MPFR_ROUND(context));
 669      _GMPy_MPFR_Cleanup(&result, context);
 670      return (PyObject*)result;
 671  }
 672  
 673  static PyObject *
 674  GMPy_Real_Atanh(PyObject *x, CTXT_Object *context)
 675  {
 676      PyObject *result, *tempx;
 677  
 678      CHECK_CONTEXT(context);
 679  
 680      if (!(tempx = (PyObject*)GMPy_MPFR_From_Real(x, 1, context))) {
 681          return NULL;
 682      }
 683  
 684      result = _GMPy_MPFR_Atanh(tempx, context);
 685      Py_DECREF(tempx);
 686      return result;
 687  }
 688  
 689  static PyObject *
 690  _GMPy_MPC_Atanh(PyObject *x, CTXT_Object *context)
 691  {
 692      MPC_Object *result;
 693  
 694      CHECK_CONTEXT(context);
 695  
 696      if (!(result = GMPy_MPC_New(0, 0, context))) {
 697          return NULL;
 698      }
 699  
 700      result->rc = mpc_atanh(result->c, MPC(x), GET_MPC_ROUND(context));
 701      _GMPy_MPC_Cleanup(&result, context);
 702      return (PyObject*)result;
 703  }
 704  
 705  static PyObject *
 706  GMPy_Complex_Atanh(PyObject *x, CTXT_Object *context)
 707  {
 708      PyObject *result, *tempx;
 709  
 710      CHECK_CONTEXT(context);
 711  
 712      if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) {
 713          return NULL;
 714      }
 715  
 716      result = _GMPy_MPC_Atanh(tempx, context);
 717      Py_DECREF(tempx);
 718      return result;
 719  }
 720  
 721  GMPY_MPFR_MPC_UNIOP_TEMPLATE_EX(Atanh, atanh)
 722  
 723  PyDoc_STRVAR(GMPy_doc_function_atan2,
 724  "atan2(y, x) -> number\n\n"
 725  "Return arc-tangent of (y/x); result in radians.");
 726  
 727  PyDoc_STRVAR(GMPy_doc_context_atan2,
 728  "context.atan2(y, x) -> number\n\n"
 729  "Return arc-tangent of (y/x); result in radians.");
 730  
 731  GMPY_MPFR_BINOP_EX(Atan2, atan2)
 732  
 733  PyDoc_STRVAR(GMPy_doc_function_hypot,
 734  "hypot(x, y) -> number\n\n"
 735  "Return square root of (x**2 + y**2).");
 736  
 737  PyDoc_STRVAR(GMPy_doc_context_hypot,
 738  "context.hypot(x, y) -> number\n\n"
 739  "Return square root of (x**2 + y**2).");
 740  
 741  GMPY_MPFR_BINOP_EX(Hypot, hypot)
 742  
 743  static PyObject *
 744  _GMPy_MPFR_Sin_Cos(PyObject *x, CTXT_Object *context)
 745  {
 746      MPFR_Object *s, *c;
 747      PyObject *result;
 748      int code;
 749  
 750      CHECK_CONTEXT(context);
 751  
 752      s = GMPy_MPFR_New(0, context);
 753      c = GMPy_MPFR_New(0, context);
 754      result = PyTuple_New(2);
 755      if (!s || !c || !result) {
 756          Py_XDECREF((PyObject*)s);
 757          Py_XDECREF((PyObject*)c);
 758          Py_XDECREF(result);
 759          return NULL;
 760      }
 761  
 762      mpfr_clear_flags();
 763  
 764      code = mpfr_sin_cos(s->f, c->f, MPFR(x), GET_MPFR_ROUND(context));
 765  
 766      s->rc = code & 0x03;
 767      c->rc = code >> 2;
 768      if (s->rc == 2) s->rc = -1;
 769      if (c->rc == 2) c->rc = -1;
 770  
 771      _GMPy_MPFR_Cleanup(&s, context);
 772      _GMPy_MPFR_Cleanup(&c, context);
 773  
 774      if (!s || !c) {
 775          Py_XDECREF((PyObject*)s);
 776          Py_XDECREF((PyObject*)c);
 777          Py_DECREF(result);
 778          return NULL;
 779      }
 780  
 781      PyTuple_SET_ITEM(result, 0, (PyObject*)s);
 782      PyTuple_SET_ITEM(result, 1, (PyObject*)c);
 783      return result;
 784  }
 785  
 786  static PyObject *
 787  GMPy_Real_Sin_Cos(PyObject *x, CTXT_Object *context)
 788  {
 789      PyObject *result, *tempx;
 790  
 791      CHECK_CONTEXT(context);
 792  
 793      if (!(tempx = (PyObject*)GMPy_MPFR_From_Real(x, 1, context))) {
 794          return NULL;
 795      }
 796  
 797      result = _GMPy_MPFR_Sin_Cos(tempx, context);
 798      Py_DECREF(tempx);
 799      return result;
 800  }
 801  
 802  static PyObject *
 803  _GMPy_MPC_Sin_Cos(PyObject *x, CTXT_Object *context)
 804  {
 805      MPC_Object *s, *c;
 806      PyObject *result;
 807      int code;
 808  
 809      CHECK_CONTEXT(context);
 810  
 811      s = GMPy_MPC_New(0, 0, context);
 812      c = GMPy_MPC_New(0, 0, context);
 813      result = PyTuple_New(2);
 814      if (!s || !c || !result) {
 815          Py_XDECREF((PyObject*)s);
 816          Py_XDECREF((PyObject*)c);
 817          Py_XDECREF(result);
 818          return NULL;
 819      }
 820  
 821      code = mpc_sin_cos(s->c, c->c, MPC(x), GET_MPC_ROUND(context), GET_MPC_ROUND(context));
 822  
 823      s->rc = MPC_INEX1(code);
 824      c->rc = MPC_INEX2(code);
 825  
 826      _GMPy_MPC_Cleanup(&s, context);
 827      _GMPy_MPC_Cleanup(&c, context);
 828  
 829      if (!s || !c) {
 830          Py_XDECREF((PyObject*)s);
 831          Py_XDECREF((PyObject*)c);
 832          Py_XDECREF(result);
 833          return NULL;
 834      }
 835  
 836      PyTuple_SET_ITEM(result, 0, (PyObject*)s);
 837      PyTuple_SET_ITEM(result, 1, (PyObject*)c);
 838      return result;
 839  }
 840  
 841  static PyObject *
 842  GMPy_Complex_Sin_Cos(PyObject *x, CTXT_Object *context)
 843  {
 844      PyObject *result, *tempx;
 845  
 846      CHECK_CONTEXT(context);
 847  
 848      if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) {
 849          return NULL;
 850      }
 851  
 852      result = _GMPy_MPC_Sin_Cos(tempx, context);
 853      Py_DECREF(tempx);
 854      return result;
 855  }
 856  
 857  PyDoc_STRVAR(GMPy_doc_context_sin_cos,
 858  "context.sin_cos(x) -> (number, number)\n\n"
 859  "Return a tuple containing the sine and cosine of x; x in radians.");
 860  
 861  PyDoc_STRVAR(GMPy_doc_function_sin_cos,
 862  "sin_cos(x) -> (number, number)\n\n"
 863  "Return a tuple containing the sine and cosine of x; x in radians.");
 864  
 865  GMPY_MPFR_MPC_UNIOP_TEMPLATE_EX(Sin_Cos, sin_cos)
 866  
 867  static PyObject *
 868  _GMPy_MPFR_Sinh_Cosh(PyObject *x, CTXT_Object *context)
 869  {
 870      MPFR_Object *s, *c;
 871      PyObject *result;
 872      int code;
 873  
 874      CHECK_CONTEXT(context);
 875  
 876      s = GMPy_MPFR_New(0, context);
 877      c = GMPy_MPFR_New(0, context);
 878      result = PyTuple_New(2);
 879      if (!s || !c || !result) {
 880          Py_XDECREF((PyObject*)s);
 881          Py_XDECREF((PyObject*)c);
 882          Py_XDECREF(result);
 883          return NULL;
 884      }
 885  
 886      mpfr_clear_flags();
 887  
 888      code = mpfr_sinh_cosh(s->f, c->f, MPFR(x), GET_MPFR_ROUND(context));
 889  
 890      s->rc = code & 0x03;
 891      c->rc = code >> 2;
 892      if (s->rc == 2) s->rc = -1;
 893      if (c->rc == 2) c->rc = -1;
 894  
 895      _GMPy_MPFR_Cleanup(&s, context);
 896      _GMPy_MPFR_Cleanup(&c, context);
 897  
 898      if (!s || !c) {
 899          Py_XDECREF((PyObject*)s);
 900          Py_XDECREF((PyObject*)c);
 901          Py_XDECREF(result);
 902          return NULL;
 903      }
 904  
 905      PyTuple_SET_ITEM(result, 0, (PyObject*)s);
 906      PyTuple_SET_ITEM(result, 1, (PyObject*)c);
 907      return result;
 908  }
 909  
 910  static PyObject *
 911  GMPy_Real_Sinh_Cosh(PyObject *x, CTXT_Object *context)
 912  {
 913      PyObject *result, *tempx;
 914  
 915      CHECK_CONTEXT(context);
 916  
 917      if (!(tempx = (PyObject*)GMPy_MPFR_From_Real(x, 1, context))) {
 918          return NULL;
 919      }
 920  
 921      result = _GMPy_MPFR_Sinh_Cosh(tempx, context);
 922      Py_DECREF(tempx);
 923      return result;
 924  }
 925  
 926  PyDoc_STRVAR(GMPy_doc_context_sinh_cosh,
 927  "context.sinh_cosh(x) -> (number, number)\n\n"
 928  "Return a tuple containing the hyperbolic sine and cosine of x.");
 929  
 930  PyDoc_STRVAR(GMPy_doc_function_sinh_cosh,
 931  "sinh_cosh(x) -> (number, number)\n\n"
 932  "Return a tuple containing the hyperbolic sine and cosine of x.");
 933  
 934  GMPY_MPFR_UNIOP_TEMPLATE_EX(Sinh_Cosh, sinh_cosh)
 935  
 936  PyDoc_STRVAR(GMPy_doc_function_degrees,
 937  "degrees(x) -> mpfr\n\n"
 938  "Convert angle x from radians to degrees.\n"
 939  "Note: In rare cases the result may not be correctly rounded.");
 940  
 941  PyDoc_STRVAR(GMPy_doc_context_degrees,
 942  "context.degrees(x) -> mpfr\n\n"
 943  "Convert angle x from radians to degrees.\n"
 944  "Note: In rare cases the result may not be correctly rounded.");
 945  
 946  static PyObject *
 947  GMPy_Context_Degrees(PyObject *self, PyObject *other)
 948  {
 949      MPFR_Object *result, *tempx, *temp;
 950      CTXT_Object *context = NULL;
 951  
 952      if (self && CTXT_Check(self)) {
 953          context = (CTXT_Object*)self;
 954      }
 955      else {
 956          CHECK_CONTEXT(context);
 957      }
 958  
 959      result = GMPy_MPFR_New(0, context);
 960      temp = GMPy_MPFR_New(context->ctx.mpfr_prec + 100, context);
 961      tempx = GMPy_MPFR_From_Real(other, 1, context);
 962      if (!result || !temp || !tempx) {
 963          Py_XDECREF((PyObject*)temp);
 964          Py_XDECREF((PyObject*)tempx);
 965          Py_XDECREF((PyObject*)result);
 966          return NULL;
 967      }
 968  
 969      mpfr_const_pi(temp->f, MPFR_RNDN);
 970      mpfr_ui_div(temp->f, 180, temp->f, MPFR_RNDN);
 971  
 972      mpfr_clear_flags();
 973  
 974      mpfr_mul(result->f, temp->f, tempx->f, MPFR_RNDN);
 975  
 976      Py_DECREF((PyObject*)temp);
 977      Py_DECREF((PyObject*)tempx);
 978      _GMPy_MPFR_Cleanup(&result, context);
 979      return (PyObject*)result;
 980  }
 981  
 982  PyDoc_STRVAR(GMPy_doc_function_radians,
 983  "radians(x) -> mpfr\n\n"
 984  "Convert angle x from degrees to radians.\n"
 985  "Note: In rare cases the result may not be correctly rounded.");
 986  
 987  PyDoc_STRVAR(GMPy_doc_context_radians,
 988  "context.radians(x) -> mpfr\n\n"
 989  "Convert angle x from degrees to radians.\n"
 990  "Note: In rare cases the result may not be correctly rounded.");
 991  
 992  static PyObject *
 993  GMPy_Context_Radians(PyObject *self, PyObject *other)
 994  {
 995      MPFR_Object *result, *tempx, *temp;
 996      CTXT_Object *context = NULL;
 997  
 998      if (self && CTXT_Check(self)) {
 999          context = (CTXT_Object*)self;
1000      }
1001      else {
1002          CHECK_CONTEXT(context);
1003      }
1004  
1005      result = GMPy_MPFR_New(0, context);
1006      temp = GMPy_MPFR_New(context->ctx.mpfr_prec + 100, context);
1007      tempx = GMPy_MPFR_From_Real(other, 1, context);
1008      if (!result || !temp || !tempx) {
1009          Py_XDECREF((PyObject*)temp);
1010          Py_XDECREF((PyObject*)tempx);
1011          Py_XDECREF((PyObject*)result);
1012          return NULL;
1013      }
1014  
1015      mpfr_const_pi(temp->f, MPFR_RNDN);
1016      mpfr_div_ui(temp->f, temp->f, 180, MPFR_RNDN);
1017  
1018      mpfr_clear_flags();
1019  
1020      mpfr_mul(result->f, tempx->f, temp->f, MPFR_RNDN);
1021  
1022      Py_DECREF((PyObject*)temp);
1023      Py_DECREF((PyObject*)tempx);
1024      _GMPy_MPFR_Cleanup(&result, context);
1025      return (PyObject*)result;
1026  }
1027  
1028  PyDoc_STRVAR(GMPy_doc_context_log10,
1029  "context.log10(x) -> number\n\n"
1030  "Return the base-10 logarithm of x.");
1031  
1032  PyDoc_STRVAR(GMPy_doc_function_log10,
1033  "log10(x) -> number\n\n"
1034  "Return the base-10 logarithm of x.");
1035  
1036  GMPY_MPFR_MPC_UNIOP_EX(Log10, log10)
1037  
1038  PyDoc_STRVAR(GMPy_doc_context_log,
1039  "context.log(x) -> number\n\n"
1040  "Return the natural logarithm of x.");
1041  
1042  PyDoc_STRVAR(GMPy_doc_function_log,
1043  "log(x) -> number\n\n"
1044  "Return the natural logarithm of x.");
1045  
1046  GMPY_MPFR_MPC_UNIOP_EX(Log, log)
1047  
1048  PyDoc_STRVAR(GMPy_doc_context_exp,
1049  "context.exp(x) -> number\n\n"
1050  "Return the exponential of x.");
1051  
1052  PyDoc_STRVAR(GMPy_doc_function_exp,
1053  "exp(x) -> number\n\n"
1054  "Return the exponential of x.");
1055  
1056  GMPY_MPFR_MPC_UNIOP_EX(Exp, exp)
1057  
1058  PyDoc_STRVAR(GMPy_doc_context_sqrt,
1059  "context.sqrt(x) -> number\n\n"
1060  "Return the square root of x.");
1061  
1062  PyDoc_STRVAR(GMPy_doc_function_sqrt,
1063  "sqrt(x) -> number\n\n"
1064  "Return the square root of x.");
1065  
1066  static PyObject *
1067  GMPy_RealWithType_Sqrt(PyObject *x, int xtype, CTXT_Object *context)
1068  {
1069      MPFR_Object *result = NULL;
1070  
1071      CHECK_CONTEXT(context);
1072  
1073      if (IS_TYPE_MPFR(xtype)) {
1074          if (mpfr_sgn(MPFR(x)) < 0 && context->ctx.allow_complex) {
1075              return GMPy_ComplexWithType_Sqrt(x, xtype, context);
1076          }
1077         
1078          if (!(result = GMPy_MPFR_New(0, context))) {
1079              return NULL;
1080          }
1081  
1082          mpfr_clear_flags();
1083          result->rc = mpfr_sqrt(result->f, MPFR(x), GET_MPFR_ROUND(context));
1084          _GMPy_MPFR_Cleanup(&result, context);
1085          return (PyObject*)result;
1086      }
1087  
1088      if (IS_TYPE_REAL(xtype)) {
1089          MPFR_Object *tempx = NULL;
1090  
1091          if (!(tempx = GMPy_MPFR_From_RealWithType(x, xtype, 1, context))) {
1092              return NULL;
1093          }
1094  
1095          if (mpfr_sgn(MPFR(tempx)) < 0 && context->ctx.allow_complex) {
1096              PyObject *res = NULL;
1097              
1098              res = GMPy_ComplexWithType_Sqrt((PyObject*)tempx, OBJ_TYPE_MPFR, context);
1099              Py_DECREF(tempx);
1100              return res;
1101          }   
1102          if (!(result = GMPy_MPFR_New(0, context))) {
1103              Py_DECREF((PyObject*)tempx);
1104              return NULL;
1105          }
1106          
1107          mpfr_clear_flags();
1108          result->rc = mpfr_sqrt(result->f, MPFR(tempx), GET_MPFR_ROUND(context));
1109          Py_DECREF((PyObject*)tempx);
1110          _GMPy_MPFR_Cleanup(&result, context);
1111          return (PyObject*)result;
1112      }
1113  
1114      TYPE_ERROR("sqrt() argument type not supported");
1115      return NULL;
1116  }
1117  
1118  static PyObject *
1119  GMPy_ComplexWithType_Sqrt(PyObject *x, int xtype, CTXT_Object *context)
1120  {
1121      MPC_Object *result = NULL;
1122  
1123      CHECK_CONTEXT(context);
1124  
1125      if (!(result = GMPy_MPC_New(0, 0, context))) {
1126          return NULL;
1127      }
1128  
1129      if (IS_TYPE_MPC(xtype)) {
1130          result->rc = mpc_sqrt(result->c, MPC(x), GET_MPFR_ROUND(context));
1131          _GMPy_MPC_Cleanup(&result, context);
1132          return (PyObject*)result;
1133      }
1134  
1135      if (IS_TYPE_COMPLEX(xtype)) {
1136          MPC_Object *tempx = NULL;
1137                  
1138          if (!(tempx = GMPy_MPC_From_ComplexWithType(x, xtype, 1, 1, context))) {
1139              Py_DECREF(result);
1140              return NULL;
1141          }
1142  
1143          result->rc = mpc_sqrt(result->c, MPC(tempx), GET_MPFR_ROUND(context));
1144          Py_DECREF(tempx);
1145          _GMPy_MPC_Cleanup(&result, context);
1146          return (PyObject*)result;
1147      }
1148  
1149      TYPE_ERROR("sqrt() argument type not supported");
1150      return NULL;
1151  }
1152  
1153  GMPY_MPFR_MPC_UNIOP_TEMPLATE_EXWT(Sqrt, sqrt)
1154  
1155  PyDoc_STRVAR(GMPy_doc_function_root,
1156  "root(x, n) -> mpfr\n\n"
1157  "Return n-th root of x. The result always an 'mpfr'.\n"
1158  "Note: not IEEE 754-2008 compliant; result differs when\n"
1159  "x = -0 and n is even. See rootn().");
1160  
1161  PyDoc_STRVAR(GMPy_doc_context_root,
1162  "context.root(x, n) -> mpfr\n\n"
1163  "Return n-th root of x. The result always an 'mpfr'.\n"
1164  "Note: not IEEE 754-2008 compliant; result differs when\n"
1165  "x = -0 and n is even. See rootn().");
1166  
1167  PyDoc_STRVAR(GMPy_doc_function_rootn,
1168  "rootn(x, n) -> mpfr\n\n"
1169  "Return n-th root of x. The result always an 'mpfr'.\n"
1170  "Note: this is IEEE 754-2008 compliant version of root().");
1171  
1172  PyDoc_STRVAR(GMPy_doc_context_rootn,
1173  "context.rootn(x, n) -> mpfr\n\n"
1174  "Return n-th root of x. The result always an 'mpfr'.\n"
1175  "Note: this is IEEE 754-2008 compliant version of root().");
1176  
1177  #if MPFR_VERSION_MAJOR > 3
1178  
1179  /* Since mpfr_root is deprecated in MPFR 4, we use mpfr_rootn_ui to
1180   * mimic the behavior of mpfr_root. And the converse will be true when
1181   * using MPFR 3.
1182   */
1183  
1184  static PyObject *
1185  GMPy_Real_Rootn(PyObject *x, PyObject *y, CTXT_Object *context)
1186  {
1187      MPFR_Object *result = NULL, *tempx = NULL;
1188      unsigned long n;
1189  
1190      CHECK_CONTEXT(context);
1191  
1192      result = GMPy_MPFR_New(0, context);
1193      tempx = GMPy_MPFR_From_Real(x, 1, context);
1194      n = c_ulong_From_Integer(y);
1195  
1196      if (!result || !tempx || (n == (unsigned long)(-1) && PyErr_Occurred())) {
1197          Py_XDECREF((PyObject*)tempx);
1198          Py_XDECREF((PyObject*)result);
1199          return NULL;
1200      }
1201  
1202      mpfr_clear_flags();
1203      result->rc = mpfr_rootn_ui(result->f, tempx->f, n, GET_MPFR_ROUND(context));
1204      Py_DECREF((PyObject*)tempx);
1205      _GMPy_MPFR_Cleanup(&result, context);
1206      return (PyObject*)result;
1207  }
1208  
1209  static PyObject *
1210  GMPy_Real_Root(PyObject *x, PyObject *y, CTXT_Object *context)
1211  {
1212      MPFR_Object *result = NULL, *tempx = NULL;
1213      unsigned long n;
1214  
1215      CHECK_CONTEXT(context);
1216  
1217      result = GMPy_MPFR_New(0, context);
1218      tempx = GMPy_MPFR_From_Real(x, 1, context);
1219      n = c_ulong_From_Integer(y);
1220  
1221      if (!result || !tempx || (n == (unsigned long)(-1) && PyErr_Occurred())) {
1222          Py_XDECREF((PyObject*)tempx);
1223          Py_XDECREF((PyObject*)result);
1224          return NULL;
1225      }
1226  
1227      mpfr_clear_flags();
1228  
1229      /* Mimic the non-compliant IEEE 752-2008 behavior. */
1230  
1231      if (mpfr_zero_p(tempx->f)) {
1232          mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context));
1233      }
1234      else {
1235          result->rc = mpfr_rootn_ui(result->f, tempx->f, n, GET_MPFR_ROUND(context));
1236      }
1237  
1238      Py_DECREF((PyObject*)tempx);
1239      _GMPy_MPFR_Cleanup(&result, context);
1240      return (PyObject*)result;
1241  }
1242  #else
1243  static PyObject *
1244  GMPy_Real_Rootn(PyObject *x, PyObject *y, CTXT_Object *context)
1245  {
1246      MPFR_Object *result = NULL, *tempx = NULL;
1247      unsigned long n;
1248  
1249      CHECK_CONTEXT(context);
1250  
1251      result = GMPy_MPFR_New(0, context);
1252      tempx = GMPy_MPFR_From_Real(x, 1, context);
1253      n = c_ulong_From_Integer(y);
1254  
1255      if (!result || !tempx || (n == (unsigned long)(-1) && PyErr_Occurred())) {
1256          Py_XDECREF((PyObject*)tempx);
1257          Py_XDECREF((PyObject*)result);
1258          return NULL;
1259      }
1260  
1261      mpfr_clear_flags();
1262  
1263      /* Mimic the compliant IEEE 752-2008 behavior. */
1264  
1265      if (mpfr_zero_p(tempx->f) && mpfr_signbit(tempx->f)) {
1266          if ((n & 1)) {
1267              /* Odd, so result is -0. */
1268              mpfr_set_zero(result->f, -1);
1269          }
1270          else {
1271              /* Even, so result is 0. */
1272              mpfr_set_zero(result->f, 1);
1273          }
1274      }
1275      else {
1276          result->rc = mpfr_root(result->f, tempx->f, n, GET_MPFR_ROUND(context));
1277      }
1278      Py_DECREF((PyObject*)tempx);
1279      _GMPy_MPFR_Cleanup(&result, context);
1280      return (PyObject*)result;
1281  }
1282  
1283  static PyObject *
1284  GMPy_Real_Root(PyObject *x, PyObject *y, CTXT_Object *context)
1285  {
1286      MPFR_Object *result = NULL, *tempx = NULL;
1287      unsigned long n;
1288  
1289      CHECK_CONTEXT(context);
1290  
1291      result = GMPy_MPFR_New(0, context);
1292      tempx = GMPy_MPFR_From_Real(x, 1, context);
1293      n = c_ulong_From_Integer(y);
1294  
1295      if (!result || !tempx || (n == (unsigned long)(-1) && PyErr_Occurred())) {
1296          Py_XDECREF((PyObject*)tempx);
1297          Py_XDECREF((PyObject*)result);
1298          return NULL;
1299      }
1300  
1301      mpfr_clear_flags();
1302      result->rc = mpfr_root(result->f, tempx->f, n, GET_MPFR_ROUND(context));
1303      Py_DECREF((PyObject*)tempx);
1304      _GMPy_MPFR_Cleanup(&result, context);
1305      return (PyObject*)result;
1306  }
1307  #endif
1308  
1309  static PyObject *
1310  GMPy_Number_Rootn(PyObject *x, PyObject *y, CTXT_Object *context)
1311  {
1312      if (IS_REAL(x) && PyIntOrLong_Check(y))
1313          return GMPy_Real_Rootn(x, y, context);
1314      TYPE_ERROR("rootn() argument type not supported");
1315      return NULL;
1316  }
1317  
1318  static PyObject *
1319  GMPy_Context_Rootn(PyObject *self, PyObject *args)
1320  {
1321      CTXT_Object *context = NULL;
1322      if (PyTuple_GET_SIZE(args) != 2) {
1323          TYPE_ERROR("rootn() requires 2 arguments");
1324          return NULL;
1325      }
1326      if (self && CTXT_Check(self)) {
1327          context = (CTXT_Object*)self;
1328      }
1329      else {
1330          CHECK_CONTEXT(context);
1331      }
1332      return GMPy_Number_Rootn(PyTuple_GET_ITEM(args, 0), PyTuple_GET_ITEM(args, 1), context);
1333  }
1334  
1335  static PyObject *
1336  GMPy_Number_Root(PyObject *x, PyObject *y, CTXT_Object *context)
1337  {
1338      if (IS_REAL(x) && PyIntOrLong_Check(y))
1339          return GMPy_Real_Root(x, y, context);
1340      TYPE_ERROR("root() argument type not supported");
1341      return NULL;
1342  }
1343  
1344  static PyObject *
1345  GMPy_Context_Root(PyObject *self, PyObject *args)
1346  {
1347      CTXT_Object *context = NULL;
1348      if (PyTuple_GET_SIZE(args) != 2) {
1349          TYPE_ERROR("root() requires 2 arguments");
1350          return NULL;
1351      }
1352      if (self && CTXT_Check(self)) {
1353          context = (CTXT_Object*)self;
1354      }
1355      else {
1356          CHECK_CONTEXT(context);
1357      }
1358      return GMPy_Number_Root(PyTuple_GET_ITEM(args, 0), PyTuple_GET_ITEM(args, 1), context);
1359  }
1360  
1361  
1362  
1363  
1364  PyDoc_STRVAR(GMPy_doc_function_jn,
1365  "jn(x,n) -> mpfr\n\n"
1366  "Return the first kind Bessel function of order n of x.");
1367  
1368  PyDoc_STRVAR(GMPy_doc_context_jn,
1369  "context.jn(x,n) -> mpfr\n\n"
1370  "Return the first kind Bessel function of order n of x.");
1371  
1372  GMPY_MPFR_BINOP_REAL_LONG(Jn, jn)
1373  
1374  PyDoc_STRVAR(GMPy_doc_function_yn,
1375  "yn(x,n) -> mpfr\n\n"
1376  "Return the second kind Bessel function of order n of x.");
1377  
1378  PyDoc_STRVAR(GMPy_doc_context_yn,
1379  "context.yn(x,n) -> mpfr\n\n"
1380  "Return the second kind Bessel function of order n of x.");
1381  
1382  GMPY_MPFR_BINOP_REAL_LONG(Yn, yn)
1383  
1384  PyDoc_STRVAR(GMPy_doc_function_agm,
1385  "agm(x, y) -> mpfr\n\n"
1386  "Return arithmetic-geometric mean of x and y.");
1387  
1388  PyDoc_STRVAR(GMPy_doc_context_agm,
1389  "context.agm(x, y) -> mpfr\n\n"
1390  "Return arithmetic-geometric mean of x and y.");
1391  
1392  GMPY_MPFR_BINOP(AGM, agm)
1393  
1394  PyDoc_STRVAR(GMPy_doc_function_maxnum,
1395  "maxnum(x, y) -> mpfr\n\n"
1396  "Return the maximum number of x and y. If x and y are not 'mpfr', they are\n"
1397  "converted to 'mpfr'. The result is rounded to match the current context.\n"
1398  "If only one of x or y is a number, then that number is returned.");
1399  
1400  PyDoc_STRVAR(GMPy_doc_context_maxnum,
1401  "context.maxnum(x, y) -> mpfr\n\n"
1402  "Return the maximum number of x and y. If x and y are not 'mpfr', they are\n"
1403  "converted to 'mpfr'. The result is rounded to match the specified context.\n"
1404  "If only one of x or y is a number, then that number is returned.");
1405  
1406  GMPY_MPFR_BINOP(Maxnum, max)
1407  
1408  PyDoc_STRVAR(GMPy_doc_function_minnum,
1409  "minnum(x, y) -> mpfr\n\n"
1410  "Return the minimum number of x and y. If x and y are not 'mpfr', they are\n"
1411  "converted to 'mpfr'. The result is rounded to match the current context.\n"
1412  "If only one of x or y is a number, then that number is returned.");
1413  
1414  PyDoc_STRVAR(GMPy_doc_context_minnum,
1415  "context.minnum(x, y) -> mpfr\n\n"
1416  "Return the minimum number of x and y. If x and y are not 'mpfr', they are\n"
1417  "converted to 'mpfr'. The result is rounded to match the specified context.\n"
1418  "If only one of x or y is a number, then that number is returned.");
1419  
1420  GMPY_MPFR_BINOP(Minnum, min)
1421  
1422  PyDoc_STRVAR(GMPy_doc_function_remainder,
1423  "remainder(x, y) -> mpfr\n\n"
1424  "Return x - n*y where n is the integer quotient of x/y, rounded to\n"
1425  "the nearest integer and ties rounded to even.");
1426  
1427  PyDoc_STRVAR(GMPy_doc_context_remainder,
1428  "context.remainder(x, y) -> mpfr\n\n"
1429  "Return x - n*y where n is the integer quotient of x/y, rounded to\n"
1430  "the nearest integer and ties rounded to even.");
1431  
1432  GMPY_MPFR_BINOP(Remainder, remainder)
1433  
1434  PyDoc_STRVAR(GMPy_doc_function_fmod,
1435  "fmod(x, y) -> mpfr\n\n"
1436  "Return x - n*y where n is the integer quotient of x/y, rounded to 0.");
1437  
1438  PyDoc_STRVAR(GMPy_doc_context_fmod,
1439  "context.fmod(x, y) -> mpfr\n\n"
1440  "Return x - n*y where n is the integer quotient of x/y, rounded to 0.");
1441  
1442  GMPY_MPFR_BINOP(Fmod, fmod)
1443  
1444  PyDoc_STRVAR(GMPy_doc_function_round2,
1445  "round2(x[, n]) -> mpfr\n\n"
1446  "Return x rounded to n bits. Uses default precision if n is not\n"
1447  "specified. See round_away() to access the mpfr_round() function.");
1448  
1449  PyDoc_STRVAR(GMPy_doc_context_round2,
1450  "context.round2(x[, n]) -> mpfr\n\n"
1451  "Return x rounded to n bits. Uses default precision if n is not\n"
1452  "specified. See round_away() to access the mpfr_round() function.");
1453  
1454  static PyObject *
1455  GMPy_Real_Round2(PyObject *x, PyObject *y, CTXT_Object *context)
1456  {
1457      MPFR_Object *result, *tempx;
1458      long n;
1459  
1460      CHECK_CONTEXT(context);
1461      n = GET_MPFR_PREC(context);
1462  
1463      if (y) {
1464          n = PyIntOrLong_AsLong(y);
1465          if ( (n == -1 && PyErr_Occurred()) || n < MPFR_PREC_MIN || n > MPFR_PREC_MAX) {
1466              VALUE_ERROR("invalid precision");
1467              return NULL;
1468          }
1469      }
1470  
1471      if (!(tempx = GMPy_MPFR_From_Real(x, 1, context))) {
1472          return NULL;
1473      }
1474      if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) {
1475          Py_DECREF((PyObject*)tempx);
1476          return NULL;
1477      }
1478  
1479      mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context));
1480      Py_DECREF((PyObject*)tempx);
1481  
1482      mpfr_clear_flags();
1483  
1484      result->rc = mpfr_prec_round(result->f, n, GET_MPFR_ROUND(context));
1485      _GMPy_MPFR_Cleanup(&result, context);
1486      return (PyObject*)result;
1487  }
1488  
1489  static PyObject *
1490  GMPy_Number_Round2(PyObject *x, PyObject *y, CTXT_Object *context)
1491  {
1492      if (IS_REAL(x) && (!y || PyIntOrLong_Check(y)))
1493          return GMPy_Real_Round2(x, y, context);
1494  
1495      TYPE_ERROR("round2() argument type not supported");
1496      return NULL;
1497  }
1498  
1499  static PyObject *
1500  GMPy_Context_Round2(PyObject *self, PyObject *args)
1501  {
1502      CTXT_Object *context = NULL;
1503  
1504      if (PyTuple_GET_SIZE(args) < 1 || PyTuple_GET_SIZE(args) > 2) {
1505          TYPE_ERROR("round2() requires 1 or 2 arguments");
1506          return NULL;
1507      }
1508  
1509      if (self && CTXT_Check(self)) {
1510          context = (CTXT_Object*)self;
1511      }
1512      else {
1513          CHECK_CONTEXT(context);
1514      }
1515  
1516      if (PyTuple_GET_SIZE(args) == 1) {
1517          return GMPy_Number_Round2(PyTuple_GET_ITEM(args, 0), NULL, context);
1518      }
1519      else {
1520          return GMPy_Number_Round2(PyTuple_GET_ITEM(args, 0), PyTuple_GET_ITEM(args, 1), context);
1521      }
1522  }
1523  
1524  PyDoc_STRVAR(GMPy_doc_function_reldiff,
1525  "reldiff(x, y) -> mpfr\n\n"
1526  "Return the relative difference between x and y. Result is equal to\n"
1527  "abs(x-y)/x.");
1528  
1529  PyDoc_STRVAR(GMPy_doc_context_reldiff,
1530  "context.reldiff(x, y) -> mpfr\n\n"
1531  "Return the relative difference between x and y. Result is equal to\n"
1532  "abs(x-y)/x.");
1533  
1534  static PyObject *
1535  GMPy_Real_RelDiff(PyObject *x, PyObject *y, CTXT_Object *context)
1536  {
1537      MPFR_Object *tempx, *tempy, *result;
1538  
1539      CHECK_CONTEXT(context);
1540  
1541      result = GMPy_MPFR_New(0, context);
1542      tempx = GMPy_MPFR_From_Real(x, 1, context);
1543      tempy = GMPy_MPFR_From_Real(y, 1, context);
1544      if (!result || !tempx || !tempy) {
1545          Py_XDECREF((PyObject*)result);
1546          Py_XDECREF((PyObject*)tempx);
1547          Py_XDECREF((PyObject*)tempy);
1548          return NULL;
1549      }
1550  
1551      mpfr_clear_flags();
1552  
1553      mpfr_reldiff(result->f, tempx->f, tempy->f, GET_MPFR_ROUND(context));
1554      result->rc = 0;
1555      _GMPy_MPFR_Cleanup(&result, context);
1556      Py_DECREF((PyObject*)tempx);
1557      Py_DECREF((PyObject*)tempy);
1558      return (PyObject*)result;
1559  }
1560  
1561  GMPY_MPFR_BINOP_TEMPLATE(RelDiff, reldiff)
1562  
1563  PyDoc_STRVAR(GMPy_doc_mpfr_ceil_method,
1564  "x.__ceil__() -> mpfr\n\n"
1565  "Return an 'mpfr' that is the smallest integer >= x.");
1566  
1567  PyDoc_STRVAR(GMPy_doc_function_ceil,
1568  "ceil(x) ->mpfr\n\n"
1569  "Return an 'mpfr' that is the smallest integer >= x.");
1570  
1571  PyDoc_STRVAR(GMPy_doc_context_ceil,
1572  "context.ceil(x) ->mpfr\n\n"
1573  "Return an 'mpfr' that is the smallest integer >= x.");
1574  
1575  GMPY_MPFR_UNIOP_NOROUND(Ceil, ceil)
1576  
1577  PyDoc_STRVAR(GMPy_doc_mpfr_floor_method,
1578  "x.__floor__() -> mpfr\n\n"
1579  "Return an 'mpfr' that is the smallest integer <= x.");
1580  
1581  PyDoc_STRVAR(GMPy_doc_function_floor,
1582  "floor(x) -> mpfr\n\n"
1583  "Return an 'mpfr' that is the smallest integer <= x.");
1584  
1585  PyDoc_STRVAR(GMPy_doc_context_floor,
1586  "context.floor(x) -> mpfr\n\n"
1587  "Return an 'mpfr' that is the smallest integer <= x.");
1588  
1589  GMPY_MPFR_UNIOP_NOROUND(Floor, floor);
1590  
1591  PyDoc_STRVAR(GMPy_doc_mpfr_trunc_method,
1592  "x.__trunc__() -> mpfr\n\n"
1593  "Return an 'mpfr' that is truncated towards 0. Same as\n"
1594  "x.floor() if x>=0 or x.ceil() if x<0.");
1595  
1596  PyDoc_STRVAR(GMPy_doc_function_trunc,
1597  "trunc(x) -> mpfr\n\n"
1598  "Return an 'mpfr' that is x truncated towards 0. Same as\n"
1599  "x.floor() if x>=0 or x.ceil() if x<0.");
1600  
1601  PyDoc_STRVAR(GMPy_doc_context_trunc,
1602  "context.trunc(x) -> mpfr\n\n"
1603  "Return an 'mpfr' that is x truncated towards 0. Same as\n"
1604  "x.floor() if x>=0 or x.ceil() if x<0.");
1605  
1606  GMPY_MPFR_UNIOP_NOROUND(Trunc, trunc)
1607  
1608  PyDoc_STRVAR(GMPy_doc_function_round_away,
1609  "round_away(x) -> mpfr\n\n"
1610  "Return an 'mpfr' that is x rounded to the nearest integer,\n"
1611  "with ties rounded away from 0.");
1612  
1613  PyDoc_STRVAR(GMPy_doc_context_round_away,
1614  "context.round_away(x) -> mpfr\n\n"
1615  "Return an 'mpfr' that is x rounded to the nearest integer,\n"
1616  "with ties rounded away from 0.");
1617  
1618  GMPY_MPFR_UNIOP_NOROUND_NOMETHOD(RoundAway, round)
1619  
1620  PyDoc_STRVAR(GMPy_doc_function_modf,
1621  "modf(x) -> (mpfr, mpfr)\n\n"
1622  "Return a tuple containing the integer and fractional portions\n"
1623  "of x.");
1624  
1625  PyDoc_STRVAR(GMPy_doc_context_modf,
1626  "context.modf(x) -> (mpfr, mpfr)\n\n"
1627  "Return a tuple containing the integer and fractional portions\n"
1628  "of x.");
1629  
1630  static PyObject *
1631  GMPy_Real_Modf(PyObject *x, CTXT_Object *context)
1632  {
1633      MPFR_Object *s, *c, *tempx;
1634      PyObject *result;
1635      int code;
1636  
1637      CHECK_CONTEXT(context);
1638  
1639      tempx = GMPy_MPFR_From_Real(x, 1, context);
1640      s = GMPy_MPFR_New(0, context);
1641      c = GMPy_MPFR_New(0, context);
1642      result = PyTuple_New(2);
1643      if (! tempx || !s || !c || !result) {
1644          Py_XDECREF((PyObject*)tempx);
1645          Py_XDECREF((PyObject*)s);
1646          Py_XDECREF((PyObject*)c);
1647          Py_XDECREF(result);
1648          return NULL;
1649      }
1650  
1651      mpfr_clear_flags();
1652  
1653      code = mpfr_modf(s->f, c->f, tempx->f, GET_MPFR_ROUND(context));
1654      Py_DECREF((PyObject*)tempx);
1655  
1656      s->rc = code & 0x03;
1657      c->rc = code >> 2;
1658      if (s->rc == 2) s->rc = -1;
1659      if (c->rc == 2) c->rc = -1;
1660  
1661      _GMPy_MPFR_Cleanup(&s, context);
1662      _GMPy_MPFR_Cleanup(&c, context);
1663  
1664      if (!s || !c) {
1665          Py_XDECREF((PyObject*)s);
1666          Py_XDECREF((PyObject*)c);
1667          Py_DECREF(result);
1668          return NULL;
1669      }
1670  
1671      PyTuple_SET_ITEM(result, 0, (PyObject*)s);
1672      PyTuple_SET_ITEM(result, 1, (PyObject*)c);
1673      return result;
1674  }
1675  
1676  GMPY_MPFR_UNIOP_TEMPLATE(Modf, modf)
1677  
1678  PyDoc_STRVAR(GMPy_doc_function_lgamma,
1679  "lgamma(x) -> (mpfr, int)\n\n"
1680  "Return a tuple containing the logarithm of the absolute value of\n"
1681  "gamma(x) and the sign of gamma(x)");
1682  
1683  PyDoc_STRVAR(GMPy_doc_context_lgamma,
1684  "context.lgamma(x) -> (mpfr, int)\n\n"
1685  "Return a tuple containing the logarithm of the absolute value of\n"
1686  "gamma(x) and the sign of gamma(x)");
1687  
1688  static PyObject *
1689  GMPy_Real_Lgamma(PyObject *x, CTXT_Object *context)
1690  {
1691      PyObject *result;
1692      MPFR_Object *value, *tempx;
1693      int signp = 0;
1694  
1695      CHECK_CONTEXT(context)
1696  
1697      tempx = GMPy_MPFR_From_Real(x, 1, context);
1698      value = GMPy_MPFR_New(0, context);
1699      result = PyTuple_New(2);
1700      if (!tempx || !value || !result) {
1701          Py_XDECREF((PyObject*)tempx);
1702          Py_XDECREF((PyObject*)value);
1703          Py_XDECREF(result);
1704          return NULL;
1705      }
1706  
1707      mpfr_clear_flags();
1708  
1709      value->rc = mpfr_lgamma(value->f, &signp, tempx->f, GET_MPFR_ROUND(context));
1710      Py_DECREF((PyObject*)tempx);
1711  
1712      _GMPy_MPFR_Cleanup(&value, context);
1713  
1714      if (!value) {
1715          Py_DECREF(result);
1716          return NULL;
1717      }
1718  
1719      PyTuple_SET_ITEM(result, 0, (PyObject*)value);
1720      PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong((long)signp));
1721      return result;
1722  }
1723  
1724  GMPY_MPFR_UNIOP_TEMPLATE(Lgamma, lgamma)
1725  
1726  PyDoc_STRVAR(GMPy_doc_function_remquo,
1727  "remquo(x, y) -> (mpfr, int)\n\n"
1728  "Return a tuple containing the remainder(x,y) and the low bits of the\n"
1729  "quotient.");
1730  
1731  PyDoc_STRVAR(GMPy_doc_context_remquo,
1732  "context.remquo(x, y) -> (mpfr, int)\n\n"
1733  "Return a tuple containing the remainder(x,y) and the low bits of the\n"
1734  "quotient.");
1735  
1736  static PyObject *
1737  GMPy_Real_RemQuo(PyObject *x, PyObject *y, CTXT_Object *context)
1738  {
1739      PyObject *result;
1740      MPFR_Object *value, *tempx, *tempy;
1741      long quobits = 0;
1742  
1743      CHECK_CONTEXT(context);
1744  
1745      value = GMPy_MPFR_New(0, context);
1746      tempx = GMPy_MPFR_From_Real(x, 1, context);
1747      tempy = GMPy_MPFR_From_Real(y, 1, context);
1748      result = PyTuple_New(2);
1749      if (!value || !tempx || !tempx || !result) {
1750          Py_XDECREF((PyObject*)tempx);
1751          Py_XDECREF((PyObject*)tempy);
1752          Py_XDECREF((PyObject*)value);
1753          Py_XDECREF(result);
1754          return NULL;
1755      }
1756  
1757      mpfr_clear_flags();
1758  
1759      value->rc = mpfr_remquo(value->f, &quobits, tempx->f, tempy->f, GET_MPFR_ROUND(context));
1760      Py_DECREF((PyObject*)tempx);
1761      Py_DECREF((PyObject*)tempy);
1762      _GMPy_MPFR_Cleanup(&value, context);
1763  
1764      PyTuple_SET_ITEM(result, 0, (PyObject*)value);
1765      PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong(quobits));
1766      return result;
1767  }
1768  
1769  GMPY_MPFR_BINOP_TEMPLATE(RemQuo, remquo);
1770  
1771  PyDoc_STRVAR(GMPy_doc_function_frexp,
1772  "frexp(x) -> (int, mpfr)\n\n"
1773  "Return a tuple containing the exponent and mantissa of x.");
1774  
1775  PyDoc_STRVAR(GMPy_doc_context_frexp,
1776  "context.frexp(x) -> (int, mpfr)\n\n"
1777  "Return a tuple containing the exponent and mantissa of x.");
1778  
1779  static PyObject *
1780  GMPy_Real_Frexp(PyObject *x, CTXT_Object *context)
1781  {
1782      PyObject *result;
1783      MPFR_Object *value, *tempx;
1784      mpfr_exp_t exp = 0;
1785  
1786      CHECK_CONTEXT(context);
1787  
1788      value = GMPy_MPFR_New(0, context);
1789      tempx = GMPy_MPFR_From_Real(x, 1, context);
1790      result = PyTuple_New(2);
1791      if (!value || !result || !tempx) {
1792          Py_XDECREF((PyObject*)tempx);
1793          Py_XDECREF((PyObject*)value);
1794          Py_XDECREF(result);
1795          return NULL;
1796      }
1797  
1798      mpfr_clear_flags();
1799  
1800      value->rc = mpfr_frexp(&exp, value->f, tempx->f, GET_MPFR_ROUND(context));
1801      Py_DECREF((PyObject*)tempx);
1802      _GMPy_MPFR_Cleanup(&value, context);
1803  
1804      PyTuple_SET_ITEM(result, 0, PyIntOrLong_FromSsize_t((Py_ssize_t)exp));
1805      PyTuple_SET_ITEM(result, 1, (PyObject*)value);
1806      return result;
1807  }
1808  
1809  GMPY_MPFR_UNIOP_TEMPLATE(Frexp, frexp)
1810  
1811  PyDoc_STRVAR(GMPy_doc_function_next_toward,
1812  "next_toward(x, y) -> mpfr\n\n"
1813  "Return the next 'mpfr' from x in the direction of y. The result has\n"
1814  "the same precision as x.");
1815  
1816  PyDoc_STRVAR(GMPy_doc_context_next_toward,
1817  "context.next_toward(x, y) -> mpfr\n\n"
1818  "Return the next 'mpfr' from x in the direction of y. The result has\n"
1819  "the same precision as x.");
1820  
1821  static PyObject *
1822  GMPy_Context_NextToward(PyObject *self, PyObject *args)
1823  {
1824      MPFR_Object *result, *tempx, *tempy;
1825      CTXT_Object *context = NULL;
1826      int direction;
1827      mpfr_rnd_t temp_round;
1828  
1829      if (self && CTXT_Check(self)) {
1830          context = (CTXT_Object*)self;
1831      }
1832      else {
1833          CHECK_CONTEXT(context);
1834      }
1835  
1836      if (PyTuple_GET_SIZE(args) != 2) {
1837          TYPE_ERROR("next_toward() requires 2 arguments");
1838          return NULL;
1839      }
1840  
1841      tempx = GMPy_MPFR_From_Real(PyTuple_GET_ITEM(args, 0), 1, context);
1842      tempy = GMPy_MPFR_From_Real(PyTuple_GET_ITEM(args, 1), 1, context);
1843      if (!tempx || !tempy) {
1844          TYPE_ERROR("next_toward() argument type not supported");
1845          Py_XDECREF((PyObject*)tempx);
1846          Py_XDECREF((PyObject*)tempy);
1847          return NULL;
1848      }
1849  
1850      if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) {
1851          Py_DECREF((PyObject*)tempx);
1852          Py_DECREF((PyObject*)tempy);
1853          return NULL;
1854      }
1855  
1856      mpfr_clear_flags();
1857  
1858      mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context));
1859      mpfr_nexttoward(result->f, tempy->f);
1860      result->rc = 0;
1861      direction = mpfr_signbit(tempy->f);
1862      Py_DECREF((PyObject*)tempx);
1863      Py_DECREF((PyObject*)tempy);
1864      temp_round = GET_MPFR_ROUND(context);
1865      if (direction)
1866          context->ctx.mpfr_round = MPFR_RNDD;
1867      else
1868           context->ctx.mpfr_round = MPFR_RNDU;
1869      _GMPy_MPFR_Cleanup(&result, context);
1870      context->ctx.mpfr_round = temp_round;
1871      return (PyObject*)result;
1872  }
1873  
1874  PyDoc_STRVAR(GMPy_doc_function_next_above,
1875  "next_above(x) -> mpfr\n\n"
1876  "Return the next 'mpfr' from x toward +Infinity.");
1877  
1878  PyDoc_STRVAR(GMPy_doc_context_next_above,
1879  "context.next_above(x) -> mpfr\n\n"
1880  "Return the next 'mpfr' from x toward +Infinity.");
1881  
1882  static PyObject *
1883  GMPy_Context_NextAbove(PyObject *self, PyObject *other)
1884  {
1885      MPFR_Object *result, *tempx;
1886      CTXT_Object *context = NULL;
1887      mpfr_rnd_t temp_round;
1888  
1889      if (self && CTXT_Check(self)) {
1890          context = (CTXT_Object*)self;
1891      }
1892      else {
1893          CHECK_CONTEXT(context);
1894      }
1895  
1896      if (!(tempx = GMPy_MPFR_From_Real(other, 1, context))) {
1897          TYPE_ERROR("next_above() argument type not supported");
1898          return NULL;
1899      }
1900  
1901      if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) {
1902          Py_DECREF((PyObject*)tempx);
1903          return NULL;
1904      }
1905  
1906      mpfr_clear_flags();
1907  
1908      mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context));
1909      Py_DECREF((PyObject*)tempx);
1910      mpfr_nextabove(result->f);
1911      result->rc = 0;
1912      temp_round = GET_MPFR_ROUND(context);
1913      context->ctx.mpfr_round = MPFR_RNDU;
1914      _GMPy_MPFR_Cleanup(&result, context);
1915      context->ctx.mpfr_round = temp_round;
1916      return (PyObject*)result;
1917  }
1918  
1919  PyDoc_STRVAR(GMPy_doc_function_next_below,
1920  "next_below(x) -> mpfr\n\n"
1921  "Return the next 'mpfr' from x toward -Infinity.");
1922  
1923  PyDoc_STRVAR(GMPy_doc_context_next_below,
1924  "context.next_below(x) -> mpfr\n\n"
1925  "Return the next 'mpfr' from x toward -Infinity.");
1926  
1927  static PyObject *
1928  GMPy_Context_NextBelow(PyObject *self, PyObject *other)
1929  {
1930      MPFR_Object *result, *tempx;
1931      CTXT_Object *context = NULL;
1932      mpfr_rnd_t temp_round;
1933  
1934      if (self && CTXT_Check(self)) {
1935          context = (CTXT_Object*)self;
1936      }
1937      else {
1938          CHECK_CONTEXT(context);
1939      }
1940  
1941      if (!(tempx = GMPy_MPFR_From_Real(other, 1, context))) {
1942          TYPE_ERROR("next_below() argument type not supported");
1943          return NULL;
1944      }
1945  
1946      if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) {
1947          Py_DECREF((PyObject*)tempx);
1948          return NULL;
1949      }
1950  
1951      mpfr_clear_flags();
1952  
1953      mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context));
1954      Py_DECREF((PyObject*)tempx);
1955      mpfr_nextbelow(result->f);
1956      result->rc = 0;
1957      temp_round = GET_MPFR_ROUND(context);
1958      context->ctx.mpfr_round = MPFR_RNDD;
1959      _GMPy_MPFR_Cleanup(&result, context);
1960      context->ctx.mpfr_round = temp_round;
1961      return (PyObject*)result;
1962  }
1963  
1964  PyDoc_STRVAR(GMPy_doc_function_factorial,
1965  "factorial(n) -> mpfr\n\n"
1966  "Return the floating-point approximation to the factorial of n.\n\n"
1967  "See fac(n) to get the exact integer result.");
1968  
1969  PyDoc_STRVAR(GMPy_doc_context_factorial,
1970  "context.factorial(n) -> mpfr\n\n"
1971  "Return the floating-point approximation to the factorial of n.\n\n"
1972  "See fac(n) to get the exact integer result.");
1973  
1974  static PyObject *
1975  GMPy_Context_Factorial(PyObject *self, PyObject *other)
1976  {
1977      MPFR_Object *result;
1978      long n;
1979      int error;
1980      CTXT_Object *context = NULL;
1981  
1982      if (self && CTXT_Check(self)) {
1983          context = (CTXT_Object*)self;
1984      }
1985      else {
1986          CHECK_CONTEXT(context);
1987      }
1988  
1989      if (!(IS_INTEGER(other))) {
1990          TYPE_ERROR("factorial() requires non-negative integer argument");
1991          return NULL;
1992      }
1993  
1994      n = GMPy_Integer_AsLongAndError(other, &error);
1995      if ((error == -1) || (n < 0)) {
1996          VALUE_ERROR("factorial() of negative number");
1997          return NULL;
1998      }
1999  
2000      if (error == 1) {
2001          VALUE_ERROR("factorial() argument too large");
2002          return NULL;
2003      }
2004  
2005      if (error == 2) {
2006          TYPE_ERROR("factorial() requires non-negative integer argument");
2007          return NULL;
2008      }
2009  
2010      if (!(result = GMPy_MPFR_New(0, context))) {
2011          return NULL;
2012      }
2013  
2014      /* Force result to be 'inf' if n >= 44787928. The matches the behavior of
2015       * MPFR compiled on 64-bit Linux. MPFR compiled on 32-bit Linux and both 32
2016       * and 64-bit versions of Windows will enter an infinite loop. The constant
2017       * 44787929 occurs in the MPFR file gamma.c.
2018       */
2019  
2020      mpfr_clear_flags();
2021  
2022      if (n >= 44787928) {
2023          mpfr_set_inf(result->f, 1);
2024          mpfr_set_overflow();
2025      }
2026      else {
2027          mpfr_fac_ui(result->f, n, GET_MPFR_ROUND(context));
2028      }
2029  
2030      _GMPy_MPFR_Cleanup(&result, context);
2031      return (PyObject*)result;
2032  }
2033  
2034  PyDoc_STRVAR(GMPy_doc_function_fsum,
2035  "fsum(iterable) -> mpfr\n\n"
2036  "Return an accurate sum of the values in the iterable.");
2037  
2038  PyDoc_STRVAR(GMPy_doc_context_fsum,
2039  "fsum(iterable) -> mpfr\n\n"
2040  "Return an accurate sum of the values in the iterable.");
2041  
2042  static PyObject *
2043  GMPy_Context_Fsum(PyObject *self, PyObject *other)
2044  {
2045      MPFR_Object *temp, *result;
2046      mpfr_ptr *tab;
2047      int errcode;
2048      Py_ssize_t i, seq_length = 0;
2049      CTXT_Object *context = NULL;
2050  
2051      if (self && CTXT_Check(self)) {
2052          context = (CTXT_Object*)self;
2053      }
2054      else {
2055          CHECK_CONTEXT(context);
2056      }
2057  
2058      if (!(result = GMPy_MPFR_New(0, context))) {
2059          return NULL;
2060      }
2061  
2062      if (!(other = PySequence_List(other))) {
2063          Py_DECREF((PyObject*)result);
2064          TYPE_ERROR("argument must be an iterable");
2065          return NULL;
2066      }
2067  
2068      /* other contains a new list containing all the values from the
2069       * iterable. Now make sure each item in the list is an mpfr.
2070       */
2071  
2072      seq_length = PyList_GET_SIZE(other);
2073      if (seq_length > LONG_MAX) {
2074          OVERFLOW_ERROR("temporary array is too large");
2075  	Py_DECREF(other);
2076  	Py_DECREF((PyObject*)result);
2077  	return NULL;
2078      }
2079      for (i=0; i < seq_length; i++) {
2080          if (!(temp = GMPy_MPFR_From_Real(PyList_GET_ITEM(other, i), 1, context))) {
2081              Py_DECREF(other);
2082              Py_DECREF((PyObject*)result);
2083              TYPE_ERROR("all items in iterable must be real numbers");
2084              return NULL;
2085          }
2086  
2087          errcode = PyList_SetItem(other, i,(PyObject*)temp);
2088          if (errcode < 0) {
2089              Py_DECREF(other);
2090              Py_DECREF((PyObject*)result);
2091              TYPE_ERROR("all items in iterable must be real numbers");
2092              return NULL;
2093          }
2094      }
2095  
2096      /* create an array of pointers to the mpfr_t field of a Pympfr object */
2097  
2098      if (!(tab = (mpfr_ptr *)malloc((sizeof(mpfr_srcptr) * seq_length)))) {
2099          Py_DECREF(other);
2100          Py_DECREF((PyObject*)result);
2101          return PyErr_NoMemory();
2102      }
2103      for (i=0; i < seq_length; i++) {
2104          temp = (MPFR_Object*)PyList_GET_ITEM(other, i);
2105          tab[i] = temp->f;
2106      }
2107  
2108      mpfr_clear_flags();
2109  
2110      /* The cast is safe since we have compared seq_length to LONG_MAX. */
2111      result->rc = mpfr_sum(result->f, tab, (unsigned long)seq_length, GET_MPFR_ROUND(context));
2112      Py_DECREF(other);
2113      free(tab);
2114  
2115      _GMPy_MPFR_Cleanup(&result, context);
2116      return (PyObject*)result;
2117  }