All of lore.kernel.org
 help / color / mirror / Atom feed
From: Trent Piepho <tpiepho@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Daniel Latypov <dlatypov@google.com>,
	Yiyuan guo <yguoaz@gmail.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"andy@kernel.org" <andy@kernel.org>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"oskar@scara.com" <oskar@scara.com>
Subject: Re: A divide by zero bug in lib/math/rational.c (with triggering input)
Date: Sat, 22 May 2021 17:05:08 -0700	[thread overview]
Message-ID: <CA+7tXijWgDGzWPOYhzX+bWn0cjOZ9y1-gWRG+0uobQ128=8vCg@mail.gmail.com> (raw)
In-Reply-To: <CAHp75VdeSkQSHjwTFObj84TyOOW2dh9LW3Ci9L7=iDFTbEvRoA@mail.gmail.com>

On Fri, May 21, 2021 at 2:53 AM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> >
> > I think the error can only occur when the loop exits on the 1st
> > iteration, when d1 is still zero.  In this case the prior convergent,
> > n1/d1 = 1/0, does not really exist as this is the 1st iteration.  The
> > actual series of convergents generated will never have zero terms,
> > because we stop at zero, so there will never be zero from the prior
> > iteration as we would have stopped there.
>
> This is my conclusion as well, but you beat me to it.
> And below is exactly my understanding of what's going on.

I came up with some more test cases, and there is another possibility,
if the value is small. e.g.

rational_best_approximation(1,30, 1,10, ...)
rational_best_approximation(1,19, 1,10, ...)

The former should be 0/1 and the latter 1/10.  These will divide by
zero on the 2nd iteration.

But I have a patch now that works.  It gets the closest answer in all
cases, larger than max, less than min but closer to the min than to
zero, and closest to zero.

It ends up being zero additional arithmetic to do this.   All that is
needed is a few additional branches in the termination condition.

      parent reply	other threads:[~2021-05-23  0:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-21  5:09 A divide by zero bug in lib/math/rational.c (with triggering input) Yiyuan guo
     [not found] ` <CAHp75Vf8kQ73w0R9ieDNjDVkxM-V83QRN9mc6BjRZA8xHpPNAA@mail.gmail.com>
     [not found]   ` <CAHp75Vft8pnA+m0C=Ok7nRyjERAd2uJJ4q6HcN460j0Hir6Kaw@mail.gmail.com>
2021-05-21  7:55     ` Yiyuan guo
2021-05-21  9:20       ` Trent Piepho
2021-05-21  9:53         ` Andy Shevchenko
2021-05-22 19:07           ` Oskar Schirmer
2021-05-24  3:20             ` Trent Piepho
2021-05-23  0:05           ` Trent Piepho [this message]

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='CA+7tXijWgDGzWPOYhzX+bWn0cjOZ9y1-gWRG+0uobQ128=8vCg@mail.gmail.com' \
    --to=tpiepho@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=andy@kernel.org \
    --cc=dlatypov@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oskar@scara.com \
    --cc=yguoaz@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.