Issue using log2(x)

I'm having a strange issue using the log2 function

this section of code

Doffset= log2(Db);
cout<<"Db is "<<Db<<endl;
cout<<"Doffset is "<<Doffset<<endl;

is producing this output

Db is 8
Doffset is 2

How can this be when log base 2 of 8 is 3??

Note Db is an int. I am able to input log2(8) and get the correct result of 3 so I'm guessing some typecasting is required here??
Last edited on
log2 accepts a double and returns a double.
You don't show the declaration of Doffset, but I'm assuming you've also declared it as an int. Doubles are aproximations, so if log2 returns 2.999999999 storing it in an int will result in it being truncated to 2.
Thanks for that, I'm now using


which has solved the problem.
Topic archived. No new replies allowed.