but
2*2*2 = 8
-2*-2*-2 = -8
c/c++ pow function question
That's mostly relevant for negative numbers. Taking the root from a positive number involves a normal root with at least one real solution. A negative number may have only complex solutions, in which case you wouldn't get a solution since you can't receive back the imaginary part of the solution - hence it throws a domain error.
The third root from -8 could be -2, it could also be cos(60)*i*2 + sin(60)*2 or -cos(60)*i*2 + sin(60)*2. The third root from 8 could also be the negative parts of these answers. You should also know that the root from 4 could be 2 or -2, but that normally only the 2 result is given.
If you want to properly handle these cases, you have to give all the results.
The third root from -8 could be -2, it could also be cos(60)*i*2 + sin(60)*2 or -cos(60)*i*2 + sin(60)*2. The third root from 8 could also be the negative parts of these answers. You should also know that the root from 4 could be 2 or -2, but that normally only the 2 result is given.
If you want to properly handle these cases, you have to give all the results.
According to the pow() man page, any attempt to calculate x^y where x is negative and y in non-integral will result in a domain error for the reasons Candy stated. If you only want to provide the real root, for a subset x^(1/n) where x is negative and n is integral you could do:
if n is even -> fail as roots are all complex
if n is odd, calculate -((-x)^(1/n)) i.e. -pow(-x, 1/n)
For futher info see http://en.wikipedia.org/wiki/Cube_root as an example.
Regards,
John.
if n is even -> fail as roots are all complex
if n is odd, calculate -((-x)^(1/n)) i.e. -pow(-x, 1/n)
For futher info see http://en.wikipedia.org/wiki/Cube_root as an example.
Regards,
John.
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
thx for the help.
I finaly got it working, the code seems to work properly on everything i tried it on . See below for the code, (for if you ever run into the same problem as i have).
sorry for the lack of comment. inside the code.
_int == the double "x" from x ^ y
rg2 == the double "y" from x ^ y
The function Return's false if it cant calculate something. (like: (-1)^(1/2)).
_int == also the var where the result is stored. (so: _int = x^y)
Regards.
PyroMathic
I finaly got it working, the code seems to work properly on everything i tried it on . See below for the code, (for if you ever run into the same problem as i have).
sorry for the lack of comment. inside the code.
_int == the double "x" from x ^ y
rg2 == the double "y" from x ^ y
The function Return's false if it cant calculate something. (like: (-1)^(1/2)).
_int == also the var where the result is stored. (so: _int = x^y)
Code: Select all
double lower,upper,tmp;
double rg2((* g2->Get()));
bool POW_1(false);
if(_int > 0) _int = pow(10,rg2 * log10(_int));
else if(_int == 0) _int = 0;
else if(_int < 0){
if(rg2 < 0){
rg2 = rg2 * -1;
POW_1 = true;
}
upper = floor( rg2 );
lower = rg2 - upper;
if(lower == 0.0) tmp = 0.0;
else tmp = (double) ((double) 1.0 / (double) lower);
if(tmp - floor(tmp) >= 0.5) tmp = ceil(tmp);
else tmp = floor(tmp);
int tmp2(tmp);
if( ((int) tmp2) & 1 == 1){
_int = pow( -1 * _int, rg2) * -1;
if( ( (int) upper) & 1 == 1) _int = _int * -1;
if(POW_1 == true){
if(_int == 0.0) return false;
_int = 1 / _int;
}
return true;
}else return false; // impossible...
return false;
}else return false;
return true;
Regards.
PyroMathic