gmpy/decimal interoperation

Raymond L. Buvel levub137 at
Mon Nov 14 13:55:53 CET 2005

Alex Martelli wrote:
> As things stand now (gmpy 1.01), an instance d of decimal.Decimal cannot
> transparently become an instance of any of gmpy.{mpz, mpq, mpf}, nor
> vice versa (the conversions are all possible, but a bit laborious, e.g.
> by explicitly going through string-forms).
> I'm thinking about possible ways to fix this, in whole or in part, but,
> before I spend more time on this, I was wondering if there's anybody
> else who'd be interested -- if so, maybe we can discuss which
> conversions should happen implicitly (e.g., if you try to sum a Decimal
> and an mpz, maybe the latter should implicitly become a Decimal, just
> like an int would, so that the result is a Decimal) and which ones
> should only happen on explicit request (e.g., gmpy.mpf(d) should produce
> an mpf instance, just as calling gmpy.mpf on an int instance would).
> Alex

This is a bit off topic but I would like to know how you would go about
doing an implicit operation with an mpz and Decimal becoming a Decimal.
 I would like to use something like this to make the float and complex
types more interoperable with mpq and cmpq in my clnum module
(  In those cases, I
would like the mpq to degrade to float and the cmpq to degrade to
complex (right now they just raise exceptions).  Seems like you need to
either modify the target type to recognize the new one or the code has
to get very complex to handle all the combinations.


More information about the Python-list mailing list