c/c++ pow function question

Programming, for all ages and all languages.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

but
2*2*2 = 8
-2*-2*-2 = -8
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

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.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

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.
PyroMathic
Member
Member
Posts: 33
Joined: Wed Apr 26, 2006 11:00 pm

Post by PyroMathic »

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)

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
Post Reply