# Typecasting and math function

This is a discussion on Typecasting and math function within the C++ Programming forums, part of the General Programming Boards category; Hello I am very confused at my problem. I have a piece of code that sometimes offers the correct result, ...

1. ## Typecasting and math function

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
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 = 7 this returns 2 instead of 3.
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.

Code:
```in windows:
int a = 7;
cout << log2(a+1) << endl; gives 3
cout << (int)log2(a+1) << endl; gives 2```
What is the cause of this?

thanks

2. 2.99...99
printed as double will probalbly shown 3
(int)2.99...99 will give you 2

3. vart is right. The solution would be to do:
Code:
`return (int)(log2(a+1) + epsilon);`
Where epsilon is a small value, eg. 1e-12.

4. Thanks you are right adding epsilon works.

Why would the math function return 2.999 instead of 3 Can I blame the compiler?

5. Originally Posted by wozaa
Thanks you are right adding epsilon works.

Why would the math function return 2.999 instead of 3 Can I blame the compiler?
float arithmetic is not precise. to calculate log2 probably some long sum is used, which is stopped as soon as some predefined precision is achieved. so any float point operaton result could be a little less or bigger than presize result. you should take this into account.

This is one of the resons not to use == on float types

6. Originally Posted by vart
float arithmetic is not precise. to calculate log2 probably some long sum is used, which is stopped as soon as some predefined precision is achieved. so any float point operaton result could be a little less or bigger than presize result. you should take this into account.

This is one of the resons not to use == on float types
Thanks, makes sense.

7. Originally Posted by vart
float arithmetic is not precise. to calculate log2 probably some long sum is used, which is stopped as soon as some predefined precision is achieved. so any float point operaton result could be a little less or bigger than presize result. you should take this into account.

This is one of the resons not to use == on float types
Actually, on a processor with floating point, it's likely that log2 is simply log(x) / log(2) - but either way, the result is not EXACT.

If you really just want log2 to the nearest integer value, you could use a method of figuring out the highest bit - there are several ways to do that, but this works:
Code:
```int log(int x)
{
int cnt = -1;
x ++;   // Make it x + 1 first.
do
{
cnt++;
x >>= 1;
} while(x);
return cnt;
}```
Note that the above code doesn't work for negative numbers (neither does log).
--
Mats