gmpy icon indicating copy to clipboard operation
gmpy copied to clipboard

Invalid mpq.__float__?

Open skirpichev opened this issue 7 months ago • 0 comments

Consider following example:

>>> from gmpy2 import mpq
>>> from fractions import Fraction
>>> float(Fraction(9, 5))
1.8
>>> float(mpq(9, 5))
1.7999999999999998

It seems, gmpy2 uses mpq_get_d(), which "Convert op to a double, truncating if necessary (i.e. rounding towards zero)". While Python does round to nearest.

Indeed:

>>> mpz(9)/mpz(5)
mpfr('1.8')
>>> ctx = gmpy2.get_context()
>>> ctx.round=gmpy2.RoundToZero
>>> mpz(9)/mpz(5)
mpfr('1.7999999999999998')

I'll provide a patch.

skirpichev avatar Jul 22 '24 14:07 skirpichev