This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow Fractions to return 1/6 for "0.17", "0.167", "0.1667", etc.
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Leengit, mark.dickinson, rhettinger, serhiy.storchaka, zach.ware
Priority: normal Keywords:

Created on 2022-02-17 17:27 by Leengit, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
best_fraction.py Leengit, 2022-02-17 22:02 Python code to find the best fraction in a specified interval
Messages (27)
msg413418 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 17:27
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.
msg413419 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2022-02-17 17:58
This sounds interesting, but also rather similar to what the `limit_denominator` method can get you.  Can you provide examples that can't be handled nicely by `limit_denominator` to strengthen your case?
msg413420 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-17 18:05
It would return 1/7 for "0.1" and 1/4 for "0.2". Is it what you expected?
msg413423 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-17 18:09
> the constructed Fraction first computes the range of the values that the input string could have been rounded from

There's too much magic and guesswork here for my liking; I don't really see this as feasible. Moreover, depending on which rounding mode was used (round-ties-to-even, round-ties-to-away), the interval may be half-open, open or closed.

For another problematic example, suppose the string supplied is "0.10". How are we to guess whether this was the result of rounding to two decimal places after the point (in which case the interval we need is [0.095, 0.105]), or whether it's the result of rounding to two significant figures (in which case the interval we need is [0.095, 0.15])?

> and then computes the fraction in that half-open interval with the lowest numerator and denominator

This part, however, is well-defined and can be done efficiently. You may be interested in the "simplefractions" module on PyPI, which solves the exact task "find the simplest fraction in a given interval".
msg413426 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-17 18:12
> in which case the interval we need is [0.095, 0.15]

Whoops, sorry; brain fail. If we're rounding to two sig figs, the next representable value up from 0.01 is 0.011, while the next one down is 0.099, so the interval we'd be interested in would be [0.0995, 0.105].
msg413427 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-17 18:13
Sigh:

> the next representable value up from 0.01 is 0.011

should say:

> the next representable value up from 0.10 is 0.11

I think I'll duck out and give my brain a rest before commenting further.
msg413430 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:40
>This sounds interesting, but also rather similar to what the `limit_denominator` method can get you. 

`Fractions("0.17").limit_denominator()` and `Fractions("0.17").limit_denominator(n)`
for n > 28 do not give 1/6.  So, I'd have to guess at n until I get 1/6.  Instead, I'd like to have the digits actually presented "0.17" determine the interval [0.165, 0.175) which then determines the fraction.

In particular, "0.170" would give a different answer ... a fraction within [0.1695, 0.1705).
msg413433 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:41
>It would return 1/7 for "0.1" and 1/4 for "0.2". Is it what you expected?

Yes.  Or putting it another way, if that's not the right answer then whoever rounded the number should have retained more digits.
msg413434 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:44
> depending on which rounding mode was used (round-ties-to-even, round-ties-to-away), the interval may be half-open, open or closed.

I think we will get the majority of the use cases if we pick one rounding strategy and stick with it.  In later version we could support more if we found the demand: in addition to assume_rounded=True we could allow assume_rounded="round_towards_zero", etc.
msg413435 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:46
> For another problematic example, suppose the string supplied is "0.10"

We would treat "0.1", "0.10", "0.100", etc. all differently.  In all cases we would assume rounding to compute the last digit.  Similarly for "3e-10", "3.0e-10" == "30e-11", "3.00e-11", etc.
msg413436 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-17 18:48
One more example: what interval is implied by an input string of "1600"? Is it (1550, 1650)? Or (1595, 1605)? Or even (1599.5, 1600.5).

Sorry, I just don't see this working - there are two many arbitrary choices involved in guessing what interval the user intended. Much better to require the user to give that interval directly.
msg413437 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:51
> You may be interested in the "simplefractions" module on PyPI, which solves the exact task "find the simplest fraction in a given interval".

I haven't seen that code and I am interested; I will take a look.  Perhaps code from there can be imported / incorporated in our implementation here.
msg413439 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 18:57
> One more example: what interval is implied by an input string of "1600"? Is it (1550, 1650)? Or (1595, 1605)? Or even (1599.5, 1600.5).

The rule would be to look at the last digit supplied and assume that the rounding is there.  So "1600" gives [1599.5, 1600.5) and "16e2" gives [1550, 1650).
msg413440 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 19:03
The example of "16e2" would yield the interval [1550, 1650).  The smallest denominator possible is 1.  The smallest numerator that works with that denominator is 1550, but I don't like 1550/1 as the answer.

To cover these edge cases, I'd modify the optimization to be that we continue to seek the smallest denominator, but in the case that multiple numerators would give ratios within the computed interval then we choose the numerator among these that gives the ratio closest to the input value.
msg413441 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 19:07
>It would return 1/7 for "0.1" and 1/4 for "0.2". Is it what you expected?

I answered "yes" before, but I am now thinking "no".  In the case of "0.1", the smallest numerator achievable is 1, but there are multiple denominators that would give a value within [0.05, 0.15).  So, I'd choose the denominator that gives the ratio that is closet to the input value.

This is similar to the "16e2" example but with the roles of numerator and denominator reversed.
msg413442 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-17 19:08
> I'd modify the optimization to be that we continue to seek the smallest denominator, but in the case that multiple numerators would give ratios within the computed interval then we choose the numerator among these that gives the ratio closest to the input value.

Hmm. This is getting more DWIM-like (Do What I Mean) by the minute. :-)

What about for an input of "0.001"? Your current specification would give 1/667, but I'm betting that you'd actually prefer 1/1000.
msg413443 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 19:09
> What about for an input of "0.001"? Your current specification would give 1/667, but I'm betting that you'd actually prefer 1/1000.

You would win that bet.
msg413453 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-02-17 20:24
I don't think the standard library should go down this path.  

Mark's disinclinations all make sense to me, but I'm also concerned that the API would be almost unusable in practical situations.  Users would tend to know their input fraction and that they have a desire for "simplification", but it would be challenging for them to come with a surrounding interval that made sense.  We could attempt to infer an interval from the input, but a user is likely to have a poorly specified idea of what they actually want.  The operating principle here is, "refuse the temptation to guess".

Here's a little exercise for the API.  The 12 semitones in an octave are separated by a frequency ratio of 2**(1/12).  For example, if A is 440 Hz, then A# is 440*2**(1/12) = 466.16 Hz.  Note pairs sound the most harmonious when the ratio of their frequencies is a "near" a small fraction.  Determine the small fraction for each of the twelve notes.  For example, a perfect fifth has the frequency ratio of 2**(7/12) -> 1.498307 — it it "perfect" because it "close-enough" to the small integer ratio of 3 : 2.  Notice the vague requirements of "near", "close-enough" and "small integer ratio".
msg413455 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-02-17 20:31
Also note for the music example that the notion of "near enough" isn't equidistant about the "simple fraction".  The sense of nearness is logarithmic and is measured in "cents" which are hundredths of an equal-tempered semitone (i.e a one octave consists of 1200 cents).
msg413459 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-17 22:02
> The 12 semitones in an octave are separated ...

Right, this functionality would not solve the semitones / cents problem.  Nor will it achieve world peace.  But if it solves enough use cases then it is worth discussing, yes?

I haven't written the string parsing, but once we know that "0.001" means to look in [0.0005, 0.0015) then we can invoke the attached as best_fraction(Fraction("0.001"), Fraction("0.0005"), Fraction("0.0015")) to get the output Fraction(1, 1000).
msg413463 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-02-17 23:29
>  Nor will it achieve world peace.

Please watch the tone. It is borderline abusive and dismissive.

> we can invoke the attached as best_fraction(Fraction("0.001"),
> Fraction("0.0005"), Fraction("0.0015")) to get the output
> Fraction(1, 1000).

-1 I am solidly against this.  The other inputs to Fraction() are exact conversions.  The proposal works against a core concept behind the fraction module. 

Also, my expectation for Fraction("0.0015") would be to give Fraction(3, 2000), the same as would be given by Fraction(Decimal("0.0015")).

> But if it solves enough use cases then it is worth discussing, yes?

Perhaps this tracker issue should be closed and moved to python-ideas.  The notions are currently too immature to warrant more core developer time.

Also, it would be nice to have research into what other languages and libraries do for fractional approximations of decimal strings.
msg413471 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-18 09:52
Okay, let's close here; as Raymond says, that doesn't prevent further discussion on python-ideas.

> The notions are currently too immature to warrant more core developer time.

Agreed. It seems that what Lee wants is some kind of blend between the simplest fraction and the closest fraction, and it's not clear exactly how that blend would work.
msg413482 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-18 13:49
> Please watch the tone. It is borderline abusive and dismissive.

I apologize for the adverse impact there.  I will be more careful.

> Also, my expectation for Fraction("0.0015") would be to give Fraction(3, 2000), the same as would be given by Fraction(Decimal("0.0015")).

Yes, 0.0015 would be 3/2000.  However, in my example "0.0015" is the right endpoint of [0.0005, 0.0015), not the value "0.001" that we are seeking the fraction for.

> Perhaps this tracker issue should be closed and moved to python-ideas.  The notions are currently too immature to warrant more core developer time.

You have more experience with this than I.  I defer to you.

> Agreed. It seems that what Lee wants is some kind of blend between the simplest fraction and the closest fraction, and it's not clear exactly how that blend would work.

Drat.  I gave the code example in order to make that clear.  I guess we can continue to discuss this at "python-ideas".  My quick web search for that turns up a lot of things.  If you could give me a lead -- is there a web URL for that, is it an e-mail list, etc.?

Thank you all for your valuable comments.
msg413483 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-18 14:05
In case there are others who are unsure about "python-ideas" ... I believe the discussion page https://discuss.python.org/c/ideas is what was meant.
msg413487 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-18 15:24
Re python-ideas: there's a mailing list: 

https://mail.python.org/archives/list/python-ideas@python.org/

But the https://discuss.python.org/c/ideas/ category also works for this.
msg413488 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-18 15:26
> I gave the code example in order to make that clear.

Yep, that didn't help: reverse engineering the intended behaviour from a complicated piece of code isn't easy. An up-front description of the intended behaviour would be better.
msg413492 - (view) Author: Lee Newberg (Leengit) Date: 2022-02-18 16:19
I have started a discussion at https://discuss.python.org/t/allow-fractions-fraction-to-return-1-6-for-0-17-0-167-0-1667-etc .  Your feedback there would be much appreciated.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90936
2022-02-18 16:19:59Leengitsetmessages: + msg413492
2022-02-18 15:26:00mark.dickinsonsetmessages: + msg413488
2022-02-18 15:24:28mark.dickinsonsetmessages: + msg413487
2022-02-18 14:05:08Leengitsetmessages: + msg413483
2022-02-18 13:49:35Leengitsetmessages: + msg413482
2022-02-18 09:52:04mark.dickinsonsetstatus: open -> closed
resolution: rejected
messages: + msg413471

stage: resolved
2022-02-17 23:29:30rhettingersetmessages: + msg413463
2022-02-17 22:02:56Leengitsetfiles: + best_fraction.py

messages: + msg413459
2022-02-17 20:31:11rhettingersetmessages: + msg413455
2022-02-17 20:24:20rhettingersetmessages: + msg413453
2022-02-17 19:09:32Leengitsetmessages: + msg413443
2022-02-17 19:08:20mark.dickinsonsetmessages: + msg413442
2022-02-17 19:07:21Leengitsetmessages: + msg413441
2022-02-17 19:03:32Leengitsetmessages: + msg413440
2022-02-17 18:57:22Leengitsetmessages: + msg413439
2022-02-17 18:51:12Leengitsetmessages: + msg413437
2022-02-17 18:48:53mark.dickinsonsetmessages: + msg413436
2022-02-17 18:46:30Leengitsetmessages: + msg413435
2022-02-17 18:44:20Leengitsetmessages: + msg413434
2022-02-17 18:41:43Leengitsetmessages: + msg413433
2022-02-17 18:40:04Leengitsetmessages: + msg413430
2022-02-17 18:13:33mark.dickinsonsetmessages: + msg413427
2022-02-17 18:12:36mark.dickinsonsetmessages: + msg413426
2022-02-17 18:09:50mark.dickinsonsetmessages: + msg413423
2022-02-17 18:05:25serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg413420
2022-02-17 17:58:11zach.waresetnosy: + rhettinger, mark.dickinson, zach.ware

messages: + msg413419
versions: + Python 3.11
2022-02-17 17:27:34Leengitcreate