# Math.log2 applied to powers of 2

Boris Zbarsky bzbarsky at mit.edu
Thu Sep 18 11:50:31 PDT 2014

```On 9/18/14, 2:12 PM, Kevin Reid wrote:
> for all x < 2^k, log2(x) < k
> for all x > 2^k, log2(x) > k

I'm not sure this is viable.

Consider, for example k == 960, x = 2^960 + 2^908.

The exact value of log2(x) would be

960 + log2(1 + 2^-52) = 960 + ln(1+2^-52)/ln(2)
< 960 + 1/ln(2) * 2^-52
< 960 + 2^-51

where the two inequalities follow from ln(1+x) < x for small x and ln(2)
> 0.5.

But 960 + 2^-51, represented as an IEEE double, is 960, because we only
have about 43-44 digits of mantissa precision left for the non-integral
part of this number...

In fact, the smallest representable double larger than 960 is 960 +
2^-43, as far as I can tell.  So to get the property you want, we'd need
to have log2(x) equal to that for 2^960 < x < 2^960 + 2^917 at the very
least, which is quite a bunch of doubles, for many of which the exact
log2 value is definitely closer to 960 than to 960+2^-43.

> (This probably conflicts with generally desirable rounding
> properties, however.)

If by that you mean the argument above, I clearly agree.  ;)

-Boris

```

More information about the es-discuss mailing list