On Thu, Oct 8, 2020, at 13:17, Tim Peters wrote:
> [Random832 <random...@fastmail.com>]
> > I was making a "convert Fraction to Decimal, exactly if possible" function 
> > and
> > ran into a wall: it's not possible to do some of the necessary operations 
> > with
> > exact precision in decimal:
> >
> > - multiplication
> > - division where the result can be represented exactly [the divisor is an
> > integer whose prime factors are only two and five, or a rational number
> > whose numerator qualifies]
> 
> Without you supplying careful concrete examples, I really have no idea
> what you're looking for.

I don't understand what's unclear - I was suggesting there should be an easy 
way to have the exact result for all operations on Decimal operands that have 
exact results representable as Decimal.

I did misunderstand part of the documentation, though... the documentation 
claims "Some operations like addition, subtraction, and multiplication by an 
integer will automatically preserve fixed point. ", and I assumed without 
checking that this applied even if the size of the operands was greater than 
the context precision, and therefore that addition and subtraction were always 
exact. This is why my post was so focused on multiplication and division.

Sine correcting that misunderstanding, I've looked over the implementation and 
I no longer think this is worth adding without a concrete use case, since 
supporting adding very large and very small numbers together would introduce 
more inefficiencies than it's worth.

Incidentally, I also noticed the procedure suggested by the documentation for 
doing fixed point arithmetic can result in incorrect double rounding in some 
situations:
>>> (D('1.01')*D('1.46')).quantize(TWOPLACES) # true result is 1.4746
Decimal('1.48')

> Integers have unbounded precision, so stick to those when part of a
> computation needs to be unbounded.  If by "Decimal" you mean Python's
> "decimal.Decimal" class, the constructor ignores the context
> precision, and retains all the info passed to it,  So there's no need
> at all to change Decimal precision.

This only applies to the constructor though, not the arithmetic operators.

> Here's a guess at what you want:

That works for the specific use case of converting Fraction to Decimal- I 
didn't know that Decimal supported E notation in the constructor, so I thought 
I would have to multiply or divide by a power of ten directly [which would 
therefore have to be rounded]... going through the string constructor seems 
extremely inefficient and inelegant, though, I'd like a way to losslessly 
multiply or divide a decimal by a power of ten at least... a sort of decimal 
equivalent to ldexp.

And I guess for the other operations it's possible to work around by doing the 
operations in Fraction and converting to Decimal in the end - that also seems 
inelegant, but oh well.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XF25A7K55FYRGZ2DWYD5MTTISD62XY56/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to