[New-bugs-announce] [issue46780] Allow Fractions to return 1/6 for "0.17", "0.167", "0.1667", etc.

Lee Newberg report at bugs.python.org
Thu Feb 17 12:27:34 EST 2022


New submission from Lee Newberg <github at quantconsulting.com>:

For example, a string such as "0.167" could be rounded from anything in [0.1665, 0.1675).  Within that interval, the fraction with the lowest numerator and denominator is 1/6.

Here it is proposed that we add a new flag to the Fractions constructor, perhaps called `_assume_rounded`, which defaults to False and then yields no change from current behavior.  However, when it is True, the constructed Fraction first computes the range of the values that the input string could have been rounded from, and then computes the fraction in that half-open interval with the lowest numerator and denominator.  This is described at https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_within_an_interval, which uses continued fractions to arrive at the answer.

For extra bells and whistles, we'd support strings like "0x0.2AAB" which is hexadecimal for 1/6 rounded to that many places.  In this case, we'd find 1/6 as the fraction with lowest numerator and denominator in the interval [0x0.2AAA8, 0x0.2AAB8).  Likewise for binary, octal, and any other formats supported by Python.

----------
components: Library (Lib)
messages: 413418
nosy: Leengit
priority: normal
severity: normal
status: open
title: Allow Fractions to return 1/6 for "0.17", "0.167", "0.1667", etc.
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46780>
_______________________________________


More information about the New-bugs-announce mailing list