/ src / gmpy2_plus.c
gmpy2_plus.c
  1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2   * gmpy2_plus.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  /* This file implements __pos__ and context.plus().
 28   *
 29   * Public API
 30   * ==========
 31   * The following function is available as part of GMPY2's C API. If the value
 32   * of context is NULL, then the function should use the currently active
 33   * context.
 34   *
 35   *   GMPy_Number_Plus(Number, context)
 36   *
 37   * Private API
 38   * ===========
 39   *   GMPy_MPZ_Plus_Slot
 40   *   GMPy_MPQ_Plus_Slot
 41   *   GMPy_MPFR_Plus_Slot
 42   *   GMPy_MPC_Plus_Slot
 43   *
 44   *   GMPy_Integer_Plus(Integer, context|NULL)
 45   *   GMPy_Rational_Plus(Rational, context|NULL)
 46   *   GMPy_Real_Plus(Real, context|NULL)
 47   *   GMPy_Complex_Plus(Complex, context|NULL)
 48   *
 49   *   GMPy_Context_Plus(context, args)
 50   */
 51  
 52  static PyObject *
 53  GMPy_Integer_Plus(PyObject *x, CTXT_Object *context)
 54  {
 55      return (PyObject*)GMPy_MPZ_From_Integer(x, context);
 56  }
 57  
 58  static PyObject *
 59  GMPy_MPZ_Plus_Slot(MPZ_Object *x)
 60  {
 61      Py_INCREF((PyObject*)x);
 62      return (PyObject*)x;
 63  }
 64  
 65  static PyObject *
 66  GMPy_Rational_Plus(PyObject *x, CTXT_Object *context)
 67  {
 68      return (PyObject*)GMPy_MPQ_From_Rational(x, context);
 69  }
 70  
 71  static PyObject *
 72  GMPy_MPQ_Plus_Slot(MPQ_Object *x)
 73  {
 74      Py_INCREF((PyObject*)x);
 75      return (PyObject*)x;
 76  }
 77  
 78  static PyObject *
 79  GMPy_Real_Plus(PyObject *x, CTXT_Object *context)
 80  {
 81      return (PyObject*)GMPy_MPFR_From_Real(x, 0, context);
 82  }
 83  
 84  static PyObject *
 85  GMPy_MPFR_Plus_Slot(MPFR_Object *x)
 86  {
 87      return (PyObject*)GMPy_MPFR_From_MPFR(x, 0, NULL);
 88  }
 89  
 90  static PyObject *
 91  GMPy_Complex_Plus(PyObject *x, CTXT_Object *context)
 92  {
 93      return (PyObject*)GMPy_MPC_From_Complex(x, 0, 0, context);
 94  }
 95  
 96  static PyObject *
 97  GMPy_MPC_Plus_Slot(MPC_Object *x)
 98  {
 99      return (PyObject*)GMPy_MPC_From_MPC(x, 0, 0, NULL);
100  }
101  
102  static PyObject *
103  GMPy_Number_Plus(PyObject *x, CTXT_Object *context)
104  {
105      if (IS_INTEGER(x))
106          return GMPy_Integer_Plus(x, context);
107  
108      if (IS_RATIONAL_ONLY(x))
109          return GMPy_Rational_Plus(x, context);
110  
111      if (IS_REAL_ONLY(x))
112          return GMPy_Real_Plus(x, context);
113  
114      if (IS_COMPLEX_ONLY(x))
115          return GMPy_Complex_Plus(x, context);
116  
117      TYPE_ERROR("plus() argument type not supported");
118      return NULL;
119  }
120  
121  /* Implement context.plus(). The following code assumes it used a as method of
122   * a context. */
123  
124  PyDoc_STRVAR(GMPy_doc_context_plus,
125  "context.plus(x) -> number\n\n"
126  "Return +x, the context is applied to the result.");
127  
128  static PyObject *
129  GMPy_Context_Plus(PyObject *self, PyObject *args)
130  {
131      CTXT_Object *context = NULL;
132  
133      if (PyTuple_GET_SIZE(args) != 1) {
134          TYPE_ERROR("plus() requires 1 argument.");
135          return NULL;
136      }
137  
138      if (self && CTXT_Check(self)) {
139          context = (CTXT_Object*)self;
140      }
141      else {
142          CHECK_CONTEXT(context);
143      }
144  
145      return GMPy_Number_Plus(PyTuple_GET_ITEM(args, 0), context);
146  }
147