[Python-Dev] Mixing float and Decimal -- thread reboot

Guido van Rossum guido at python.org
Mon Mar 22 21:48:09 CET 2010

On Mon, Mar 22, 2010 at 12:45 PM, Mark Dickinson <dickinsm at gmail.com> wrote:
> On Mon, Mar 22, 2010 at 8:33 PM, Raymond Hettinger
> <raymond.hettinger at gmail.com> wrote:
>> While we're on the topic, I think you should consider allowing the Fraction()
>> constructor to accept a decimal input.
>> This corresponds to common schoolbook problems and simple client requests:
>>   "Express 3.5 as a fraction".
>>    >>> Fraction(Decimal('3.5'))
>>   Fraction(7, 2)
>> Unlike typical binary floats which use full precision, it is not uncommon
>> to have decimal floats with only a few digits of precision where the
>> expression as a fraction is both useful and unsurprising.
> Sounds fine to me.  Fraction already accepts decimal floating-point
> strings, so the implementation of this would be trivial (convert to
> string, then call the Fraction constructor).

Better, Fraction.from_decimal() already exists. ;-)

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list