New submission from Lee Newberg <git...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue46780>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to