kalenbhaiya's blog

By kalenbhaiya, history, 4 years ago, In English

I would like to ask that what is the problem with code forces compiler that it returns int x=log10(8)/log10(2); cout<<x; as 2 rather than 3 because in every compiler it returns 3 except code forces[problem:C. Powers Of Two]

  • Vote: I like it
  • -13
  • Vote: I do not like it

»
4 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

You should see https://blog.codinghorror.com/the-first-rule-of-programming-its-always-your-fault/ and https://blog.plover.com/prog/compiler-error.html. Codeforces uses GCC and MSVC which are well known and widely used compilers. It's highly unlikely that a compiler developed by tons of very intelligent people over the course of decades would have a trivial bug like this.

»
4 years ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

log10, pow, and other similar functions all work with floating point numbers. This means that the calculations might not be perfectly precise. Use bit shifts to get powers of two and code your own exponentiation-by-squaring function to get powers of other numbers. Use bit intrinsics such as __builtin_clz for floored base two logarithms. If you want a floating point base two logarithm there's log2.

Also, this outputs 3 on CF:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    cout << static_cast<int>(log10(8)/log10(2)) << '\n';
}