Today I puzzled to find out the value of log2((2^59)-1). Here, (2^59)-1 = 576460752303423487. We know that the value of log2(4)=2, log2(3)=1.58496 and log2(576460752303423488)=59 but **why log2(576460752303423487)=59** when **log2(x)=y and x=2^y**. This happens not only for (2^59)-1 but also for other values.

(I searched about it on google but couldn't find out the reason behind this.)

Precision error?

$$$log2$$$ is a

`double`

function, which means precision error could occur.But does the precision error occur in web calculators? I mean that I searched in some web calculators such as MiniWebtool, decode.fr etc. but same results found.

Try this link Casio Web Calculator

Maybe the difference between $$$log2(2^{59}-1)$$$ and $$$log2(2^{59})$$$ are so small that the answer is automatically adjusted to $$$59$$$

Is there any way to get the perfect floor result of this value in C++? I tried many times in a different way but failed.

convert number to binary and count length then subtract 1, and i'm pretty sure there's a c++ function too (that's just what i do in java)

if you use the calculator on Windows 10 you can get 58.999999999999999997497323043895

Yes , in a problem of previous contest I couldn't solve C because of this precision problem.

Because $$$log(x)$$$ in Competitive Programming is

usuallyequal to $$$y$$$ where $$$y$$$ is the smallestintegersuch that $$$2^y >= x$$$.This is because of precision error. The answer you are looking for is 58.9999999999999999975 A double variable cannot store these many decimal places. So it rounds it off to 59

Is there any valid way to get 58 in C++?

Write a simple loop and count how often you need to multiply by 2.

Simply implement it (for integer answers):

You can also use

`__builtin_clzll`

, which should be faster than a loop.Or, if you want to remember less characters,

`__lg`

.Interesting, I've never seen that even though it seems more direct (I don't actually use C++ so it's not that surprising, I guess).

In Java for the other five Java users:

`Long.numberOfTrailingZeros(Long.highestOneBit(x))`

.In Java,

`63 - Long.numberOfLeadingZeros(x)`

would be faster.Makes sense, thanks! I write it my way to avoid off by one mistakes (it's easiest for me, since it's pretty rare to write this anyway), but I will remember that in case I am reaching TLE.

What's the difference between log2 and __lg?

__lg(2^59-1)=58

Yes,it's amazing.

Now I know __lg != log2 and std::__lg(block_sz — pos_l + 1), but my curiosity made me want to know the reason(maybe difference).

I really want to know the reason why __lg is correct, so I can use it without doubt.

__lg is a function of integers

log2 is a function of float/double/long double

Thanks.

https://en.cppreference.com/w/cpp/numeric/countl_zero

Only since C++20 though?

We are almost there :)

Use log2l

https://ideone.com/HcgbhH More to think about

How is this possible? niyaznigmatul do you know the reason behind it?

Sure, floating point numbers store first several digits, depending on the number of bits provided for that (mantissa), and the other bunch of bits show the decimal point location (exponent).

For instance, for double it stores only 14-15 digits, and $$$2^{59}$$$ has more than that, the last digit isn't being stored.

P.S. Actually, it's more complex than what I said, because it doesn't store decimal digits, it stores in binary, that's why, for example, $$$2^{59}$$$ is printed correctly https://ideone.com/dPsCa6

I also tested for 2^59 vs 2^59 — 32 they both are showing the same result. As pointed by you, the 14-15 digits part is playing the role. :-)

Wikipedia has a pretty good description of the

`double`

format.