From: Andrew Morton <akpm@linux-foundation.org>
To: Trent Piepho <tpiepho@gmail.com>
Cc: linux-kernel@vger.kernel.org, Oskar Schirmer <oskar@scara.com>
Subject: Re: [PATCH] lib: Fix possible incorrect result from rational fractions helper
Date: Mon, 1 Apr 2019 22:22:28 -0700 [thread overview]
Message-ID: <20190401222228.5692d7e8d5b979a8c574e76c@linux-foundation.org> (raw)
In-Reply-To: <20190330205855.19396-1-tpiepho@gmail.com>
On Sat, 30 Mar 2019 13:58:55 -0700 Trent Piepho <tpiepho@gmail.com> wrote:
> In some cases the previous algorithm would not return the closest
> approximation. This would happen when a semi-convergent was the
> closest, as the previous algorithm would only consider convergents.
>
> As an example, consider an initial value of 5/4, and trying to find the
> closest approximation with a maximum of 4 for numerator and denominator.
> The previous algorithm would return 1/1 as the closest approximation,
> while this version will return the correct answer of 4/3.
>
> To do this, the main loop performs effectively the same operations as it
> did before. It must now keep track of the last three approximations,
> n2/d2 .. n0/d0, while before it only needed the last two.
>
> If an exact answer is not found, the algorithm will now calculate the
> best semi-convergent term, t, which is a single expression with two
> divisions:
> min((max_numerator - n0) / n1, (max_denominator - d0) / d1)
>
> This will be used if it is better than previous convergent. The test
> for this is generally a simple comparison, 2*t > a. But in an edge
> case, where the convergent's final term is even and the best allowable
> semi-convergent has a final term of exactly half the convergent's final
> term, the more complex comparison (d0*dp > d1*d) is used.
>
> I also wrote some comments explaining the code. While one still needs
> to look up the math elsewhere, they should help a lot to follow how the
> code relates to that math.
What are the userspace-visible runtime effects of this change?
next prev parent reply other threads:[~2019-04-02 5:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-30 20:58 Trent Piepho
2019-04-02 5:22 ` Andrew Morton [this message]
2019-04-04 3:41 ` tpiepho
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190401222228.5692d7e8d5b979a8c574e76c@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=oskar@scara.com \
--cc=tpiepho@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®