Hello
I am very confused at my problem. I have a piece of code that sometimes offers the correct result, and sometimes not.
The code is similar to this
for a = 7 this returns 2 instead of 3.Code:#include "cmath" #include "iostream" using namespace std; int log(int a){ return (int)log2(a+1); } int main(){ int a =7; cout << log(a) << endl; }
for a = 15 it returns the correct value of 4
it seems to alternate for other powers of two.
I programmed using g++ on ubuntu which gave me some form of converting double to int warning which I managed to suppress by typecasting (int). However when I went to test my program on windows using cygwin the (int) seems to be the cause of error.
What is the cause of this?Code:in windows: int a = 7; cout << log2(a+1) << endl; gives 3 cout << (int)log2(a+1) << endl; gives 2
thanks