Skip to content

Commit

Permalink
Merge pull request #541 from skirpichev/pass-ctx-to-div/540
Browse files Browse the repository at this point in the history
Pass context arguments to division functions
  • Loading branch information
casevh authored Dec 26, 2024
2 parents dc08582 + fec56ea commit 6e7a351
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/gmpy2_divmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,16 @@ GMPy_Number_DivMod(PyObject *x, PyObject *y, CTXT_Object *context)
int ytype = GMPy_ObjectType(y);

if (IS_TYPE_INTEGER(xtype) && IS_TYPE_INTEGER(ytype))
return GMPy_Integer_DivModWithType(x, xtype, y, ytype, NULL);
return GMPy_Integer_DivModWithType(x, xtype, y, ytype, context);

if (IS_TYPE_RATIONAL(xtype) && IS_TYPE_RATIONAL(ytype))
return GMPy_Rational_DivModWithType(x, xtype, y, ytype, NULL);
return GMPy_Rational_DivModWithType(x, xtype, y, ytype, context);

if (IS_TYPE_REAL(xtype) && IS_TYPE_REAL(ytype))
return GMPy_Real_DivModWithType(x, xtype, y, ytype, NULL);
return GMPy_Real_DivModWithType(x, xtype, y, ytype, context);

if (IS_TYPE_COMPLEX(xtype) && IS_TYPE_COMPLEX(ytype))
return GMPy_Complex_DivModWithType(x, xtype, y, ytype, NULL);
return GMPy_Complex_DivModWithType(x, xtype, y, ytype, context);

TYPE_ERROR("divmod() argument type not supported");
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/gmpy2_floordiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,16 @@ GMPy_Number_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context)
int ytype = GMPy_ObjectType(y);

if (IS_TYPE_INTEGER(xtype) && IS_TYPE_INTEGER(ytype))
return GMPy_Integer_FloorDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Integer_FloorDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_RATIONAL(xtype) && IS_TYPE_RATIONAL(ytype))
return GMPy_Rational_FloorDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Rational_FloorDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_REAL(xtype) && IS_TYPE_REAL(ytype))
return GMPy_Real_FloorDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Real_FloorDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_COMPLEX(xtype) && IS_TYPE_COMPLEX(ytype))
return GMPy_Complex_FloorDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Complex_FloorDivWithType(x, xtype, y, ytype, context);

TYPE_ERROR("floor_div() argument type not supported");
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/gmpy2_truediv.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,16 @@ GMPy_Number_TrueDiv(PyObject *x, PyObject *y, CTXT_Object *context)
int ytype = GMPy_ObjectType(y);

if (IS_TYPE_INTEGER(xtype) && IS_TYPE_INTEGER(ytype))
return GMPy_Integer_TrueDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Integer_TrueDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_RATIONAL(xtype) && IS_TYPE_RATIONAL(ytype))
return GMPy_Rational_TrueDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Rational_TrueDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_REAL(xtype) && IS_TYPE_REAL(ytype))
return GMPy_Real_TrueDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Real_TrueDivWithType(x, xtype, y, ytype, context);

if (IS_TYPE_COMPLEX(xtype) && IS_TYPE_COMPLEX(ytype))
return GMPy_Complex_TrueDivWithType(x, xtype, y, ytype, NULL);
return GMPy_Complex_TrueDivWithType(x, xtype, y, ytype, context);

TYPE_ERROR("div() argument type not supported");
return NULL;
Expand Down
8 changes: 8 additions & 0 deletions test/test_mpfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,11 @@ def test_mpfr_round_roundtrip_bulk(x, n):
q = mpq(*x.as_integer_ratio())
assert float(round(q, n)) == round(x, n)
assert mpfr(round(q, n)) == round(mpfr(x), n)


def test_issue_540():
a = mpfr('1')
b = mpfr('10')
ctxD = gmpy2.context(round=gmpy2.RoundDown)

assert ctxD.div(a, b) == mpfr('0.099999999999999992')

0 comments on commit 6e7a351

Please sign in to comment.