0

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.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    Try printing both `b` and `a`, as well as the results of intermediate calculations (in this case, print `c`). Are they the same value? Why or why not? – Patashu May 27 '13 at 04:55
  • Float arithmetics, especially logarithmic operations, introduce some imprecisions due to finite precision – fatihk May 27 '13 at 04:56
  • I think I'm doing it 10th time... Your variables are float and they are not represented in an EXACT way in memory of a computer. Google it, you'll find a lot of info :) – sashkello May 27 '13 at 04:56
  • what data type should i use? using another than float and double will do an error:more than one instance of overloaded function.... – Prajogo Atmaja May 27 '13 at 04:59
  • 4
    Note that if you want to check if a native integer type is a power of 2 you just need to check that only one bit is set. See http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2 for C# examples that should also work in C++. – John Carter May 27 '13 at 05:13
  • 1
    That the problem says that both the input and the result is an integer is a strong hint that you should *not* use floats. (This problem is about the principles of binary representation of integers, not about maths.) – molbdnilo May 27 '13 at 07:52
  • An other method would be to use a bitcount function(http://stackoverflow.com/questions/15370250/how-does-this-line-works-n-nn-1) and check that result is 1. – Jan Herrmann May 27 '13 at 07:58

3 Answers3

5

As What Every Computer Scientist Should Know About Floating-Point Arithmetic 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.

Patashu
  • 21,443
  • 3
  • 45
  • 53
0

Declare the values a,b,c as double if you want to use this c=log10(a)/log10(2.0);

Declare the values a,b,c as float if u want to use this c=log10(a)/log10(2.0f);

I executed the program with both these changes ,one by one .Its working for both check the syntax and example here

0

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$ 
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140