[PATCH] manual: logb(x) is floor(log2(fabs(x)))

Vincent Lefevre vincent@vinc17.net
Tue Mar 5 15:01:31 GMT 2024


On 2024-03-05 08:16:09 -0500, Morten Welinder wrote:
> > 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.
> 
> Right you are.
> 
> However, the formula is meant as an explanation, not a C code
> substitute, so I think a bit of rewording can fix it.  Something like
> 
> ...works like "floor (log2 (fabs (x)))" if that formula is computed
> without rounding of intermediate values.

I don't think that giving an explanation as code is a good idea.
Giving a math formula is OK, though. Also note that a math formula
could be given for any radix, not just 2: floor(log_b |x|). I think
that this is important since there are 3 conventions to define an
exponent. So this must be explicit in any radix.

BTW, "These functions extract the exponent from the internal
floating-point representation of x and return it as a floating-point
value." should not use the word "representation", because this has
nothing to do with the representation (think of numbers that are not
normalized). And "extract" is misleading too, because for numbers
that are not normalized, the exponent is not extracted.

Something like

  These functions return the exponent of the floating-point number x
  (in radix FLT_RADIX) as a floating-point value.

> Same story as fma really.

No, not really. For fma, this is a math formula followed by rounding.

-- 
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