Peculiar behaviour of log2() function in C++.

Revision en1, by Fearless_cod, 2021-12-16 06:39:46

Hi reader. Coming straight to the point. The value of (1ll<<59) or 2^59 is 576460752303423488(say x).So if you do log2(x),the answer should come out to be 59.That's indeed the case. But here is the catch... .If you do log2(x-1) in C++ 14(not sure for other versions),it also gives 59 as output..(but actual answer is 58), which is wrong and that caused me a fst in recent Div 3 problem F.

So, can anyone explain why is this happening and how to avoid these types of things. Thanks for spending your time in reading this blog.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Fearless_cod 2021-12-16 06:39:46 580 Initial revision (published)