[PATCH] manual: logb(x) is floor(log2(fabs(x)))
Vincent Lefevre
vincent@vinc17.net
Tue Mar 5 11:37:38 GMT 2024
On 2024-03-05 12:10:23 +0100, Alejandro Colomar wrote:
> Hi Vincent!
>
> On Tue, Mar 05, 2024 at 11:02:51AM +0100, Vincent Lefevre wrote:
> > On 2024-03-04 23:40:59 +0100, Alejandro Colomar wrote:
> > > diff --git a/manual/math.texi b/manual/math.texi
> > > index 2f6ee253b9..bf4027c4ee 100644
> > > --- a/manual/math.texi
> > > +++ b/manual/math.texi
> > > @@ -561,7 +561,7 @@ These functions return the base-2 logarithm of @var{x}.
> > > @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
> > > These functions extract the exponent of @var{x} and return it as a
> > > floating-point value. If @code{FLT_RADIX} is two, @code{logb} is equal
> > > -to @code{floor (log2 (x))}, except it's probably faster.
> > > +to @code{floor (log2 (fabs ((x)))}, except it's probably faster.
BTW, note that there is a missing closing parenthesis, or actually,
an opening parenthesis that could be removed, i.e.
@code{floor (log2 (fabs (x)))}
> > No, it isn't necessarily equal. The code floor (log2 (fabs ((x)))
> > can give an incorrect result due to rounding if x is just before
> > a power of 2, in particular if x is large.
>
> Hmmm, the bug is different, so how about a second patch for fixing that?
Yes.
> For that, I'd do s/equal/similar/ and s/faster/more accurate/. Does it
> sound good to you?
I think that "more accurate" is misleading because this is actually
much more than "more accurate": logb is guaranteed to give the exact
result (the result is a small integer, thus exactly representable),
while floor (log2 (fabs (x))) may give an incorrect integer (there is
a small inaccuracy up to log2, but this becomes a much more important
problem due to the fact that floor is mathematically a discontinuous
function).
I would suggest:
@code{logb} is similar to @code{floor (log2 (fabs (x)))}, except that
the latter code may give an incorrect integer.
If needed, one can add an explanation: "due to intermediate rounding";
or an example: ", e.g. on DBL_MAX".
Regards,
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
More information about the Libc-alpha
mailing list