From fec56ea8d744840ce723296e7278b4ab87f523e3 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 24 Dec 2024 14:00:26 +0300 Subject: [PATCH] Pass context arguments to division functions Closes #540 --- src/gmpy2_divmod.c | 8 ++++---- src/gmpy2_floordiv.c | 8 ++++---- src/gmpy2_truediv.c | 8 ++++---- test/test_mpfr.py | 8 ++++++++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/gmpy2_divmod.c b/src/gmpy2_divmod.c index 289963c7..1cd97b4c 100644 --- a/src/gmpy2_divmod.c +++ b/src/gmpy2_divmod.c @@ -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; diff --git a/src/gmpy2_floordiv.c b/src/gmpy2_floordiv.c index 95fe0606..4b130e98 100644 --- a/src/gmpy2_floordiv.c +++ b/src/gmpy2_floordiv.c @@ -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; diff --git a/src/gmpy2_truediv.c b/src/gmpy2_truediv.c index e0dadbaf..b6976dc8 100644 --- a/src/gmpy2_truediv.c +++ b/src/gmpy2_truediv.c @@ -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; diff --git a/test/test_mpfr.py b/test/test_mpfr.py index e72f6c6c..22599ef4 100644 --- a/test/test_mpfr.py +++ b/test/test_mpfr.py @@ -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')