On Fri, Mar 07, 2014 at 06:02:02PM -0800, Guido van Rossum wrote:
On Fri, Mar 7, 2014 at 5:05 PM, Steven D'Aprano
wrote: [...] The way Python converts between the two is the right way to do the conversion.
It's *exact*. I don't know we all agree it is the *right* way.
Fair point. I can't argue with that :-)
Given a decimal d and a float f constructed from d, f is the closest possible float to d. And the same applies for conversions the other way around.
It's actually stronger the other way around: when d is constricted from f, d is *equal* to the mathematical value of f.
Ah, so it is.
The issue (as I see it) is that there are many different decimals d that all convert to the same float f (because of rounding). The d that is constructed by taking the exact value of f is gross overkill.
Decimal -> float is many-to-one: more than one Decimal will round to a single float. But float -> Decimal is always one-to-one, I think, regardless of whether you use the current exact conversion or repr first. The crux of the matter is whether or not it is overkill for Decimal to use the exact value. Correct me if I'm wrong, but I think we agree that there ought to be a way to convert floats exactly to Decimal, we just disagree on whether that ought to be spelled Decimal(x) or Decimal.from_float(x). Likewise I think we agree that there ought to be some way to convert floats to the nearest "simple" Decimal, with the question being whether that ought to be spelled Decimal(repr(x)) or Decimal(x). If we're all in agreement that this is the only serious area of disagreement for a change which has any hope of appearing in 3.5, then we can put aside the more speculative proposals -- Steven