[Python-ideas] Python Float Update

drekin at gmail.com drekin at gmail.com
Wed Jun 3 18:08:17 CEST 2015


Stefan Behnel wrote:

> random832 at fastmail.us schrieb am 01.06.2015 um 05:14:
>> On Sun, May 31, 2015, at 22:25, u8y7541 The Awesome Person wrote:
>>> First, I propose that a float's integer ratio should be accurate. For
>>> example, (1 / 3).as_integer_ratio() should return (1, 3). Instead, it
>>> returns(6004799503160661, 18014398509481984).
>> 
>> Even though he's mistaken about the core premise, I do think there's a
>> kernel of a good idea here - it would be nice to have a method (maybe
>> as_integer_ratio, maybe with some parameter added, maybe a different
>> method) to return with the smallest denominator that would result in
>> exactly the original float if divided out, rather than merely the
>> smallest power of two.
> 
> The fractions module seems the obvious place to put this. Consider opening
> a feature request. Target version would be Python 3.6.
> 
> Stefan

This makes sense for any floating point number, for example Decimal. It could be also a constructor of Fraction.

>>> Fraction.simple_from(0.1)
Fraction(1, 10)
>>> Fraction.simple_from(Decimal(1) / Decimal(3))
Fraction(1, 3)


Regards, Drekin



More information about the Python-ideas mailing list