As I mentioned in a comment, checking a rational representation of a number in a computer is pretty much impossible because of round-off errors. You may be able to work around it, but maybe you can just ignore the special case of negative numbers?
One definition of exponentiation says that negative numbers cannot be raised to any power at all (according to this definition, (-1)^2 doesn't exist). To express this in C++:
bool can_has_power(double a, double b)
{
if (a < 0)
return false;
else if (a == 0)
return b != 0;
else
return true;
}
Maybe you want to allow the special case of integer powers? Here:
bool can_has_power(double a, double b)
{
if (a < 0)
return fmod(b, 1) == 0;
else if (a == 0)
return b != 0;
else
return true;
}
BTW here is some additonal consideration, which you may find enlightening. Non-integer numbers in computers are usually represented using floating-point or fixed-point encoding. Both of these systems represent the numbers as a ratio whose demoninator is a power of 2; for example:
b = m / n; n = 2^52
In this representation, all non-integer numbers have an odd numerator and even denominator! If you use b = 1/7
in your hypothetical program, the exact value 1/7
will be rounded to something like 643371375338642/2^52
, which, according to the rules of rational numbers must be adjusted by the GCD of the numerator and denominator: 321685687669321/2^51
. If you use your definition of exponentiation, then no negative number can be raised to any non-integer power!