Question: please help me for my coding. I want to make a program like this. sorry bad english. 

Given input: 

```
N  
where N is an integer. 

```

return: 

```
True  if N = 2^x, where x is an integer.

```

I've tried to do this but it doesn't work as i want.

```
using namespace std;
int main()
{
    float a,b,c;
    cin>>a;
    c=log10(a)/log10(2.0);
    b=pow(2,c);
    if(b==a)
    {
        cout<<"TRUE"<<endl;}
    else
        cout<<"FALSE"<<endl;{
    }
}

```

Help me please. Thank you.

Answer: 
As [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) explains, floating point numbers in computer programs pretend they can represent any real number, but actually have only 32 or 64 bits, so you will get rounded off to the nearest representation. Even numbers that look simple, like 0.1, have an endless representation in binary and so will get rounded off. Functions that operate on floating point numbers like `cos` and `pow` will, by their nature, sometimes give you the 'wrong' result simply because the 'right' result is not a representible floating point.

Sometimes the solution is complex. However in this case the solution is quite simple - check that the two numbers' difference is less than epsilon, where epsilon is a small enough number to give you the accuracy you need. e.g.

```
float epsilon = 0.0001;
if(abs(b-a) < epsilon)

```

Also, whenever you need accuracy more than speed and size, use `double` in preference to `float`. It's twice as big and thus many significant places more precise.
I think the code should read (given the problem description.) You want to know if N is a power of 2?

**Edit to code**

```
#include <iostream>

int main()
{
    unsigned int N;
    std::cout << "Input a number ";
    std::cin >> N;
    unsigned int two_to_the_power_of_bit = 0;
    do {
        std::cout << "Working on " <<
          two_to_the_power_of_bit << std::endl;
        if (two_to_the_power_of_bit == N) {
            std::cout << "TRUE" << std::endl;
            return 0;
        }

        if (two_to_the_power_of_bit > N) {
            std::cout << "FALSE" << std::endl;
            return 1;
        }

        if (two_to_the_power_of_bit == 0) {
          two_to_the_power_of_bit = 1;
        } else {
          two_to_the_power_of_bit <<= 1;
        }
    } while(two_to_the_power_of_bit);
}

```

If I got your problem wrong can you please clarify?

```
Output:
Input a number 3
Working on 0
Working on 1
Working on 2
Working on 4
FALSE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$ ./a.out 
Input a number 4
Working on 0
Working on 1
Working on 2
Working on 4
TRUE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$ ./a.out 5
Input a number 5
Working on 0
Working on 1
Working on 2
Working on 4
Working on 8
FALSE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$ 

```