Skip to content

Invalid mpq.__float__? #498

@skirpichev

Description

@skirpichev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions