c/c++ pow function question
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
c/c++ pow function question
lo,
I am currently trying to build a simple calculator in borland c++, which can do things like: F(x) = x^x, and can display a nice graph of it.
The problem is if i use pow(x,x) to calculate the result of ( (-1)^x), when ever x = 1/3 or or x = 1+1/5 , the pow function gives a domain error.
So it basicly can not calc a thing like: pow(x , 0.2). (or pow(x, 1.2) ) If x < 0. Are there any (standaard) function availeble which can do this ?
Does any of you might know a function which can calculate (x^x), whit whatever x (off course things like: (- 0.5) ^ (- 0.5) cannot be calculated)..
Regards
PyroMathic
I am currently trying to build a simple calculator in borland c++, which can do things like: F(x) = x^x, and can display a nice graph of it.
The problem is if i use pow(x,x) to calculate the result of ( (-1)^x), when ever x = 1/3 or or x = 1+1/5 , the pow function gives a domain error.
So it basicly can not calc a thing like: pow(x , 0.2). (or pow(x, 1.2) ) If x < 0. Are there any (standaard) function availeble which can do this ?
Does any of you might know a function which can calculate (x^x), whit whatever x (off course things like: (- 0.5) ^ (- 0.5) cannot be calculated)..
Regards
PyroMathic
- Kevin McGuire
- Member
- Posts: 843
- Joined: Tue Nov 09, 2004 12:00 am
- Location: United States
- Contact:
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
- Kevin McGuire
- Member
- Posts: 843
- Joined: Tue Nov 09, 2004 12:00 am
- Location: United States
- Contact:
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
The square root would work, just the problem is what if i need:Kevin McGuire wrote:Is that what was not working, or as I am guessing it is something else?
x^(1/3), then i cant use the sqare root function..
i could use the pow function, but if x would be -1, it would have a domain error..
if x = -8
(-8)^(1/3) = -2
just if i where to use pow(-8 , 1/3) then it would give a domain error. if i would use square root function then i would have " (-8) ^ (1/2) ".
i simply need a "pow" like function which is able to calcuta all:
x^y, except for the one's which are impossible (like: ((-1) ^ (1/2)).)
Regards.
PyroMathic
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
yeah think so, just i think more people must have run into this problem.. and i know that my pocket calculator (Texas Instruments TI-83) can easely calculate such things, so it must be possible.Zacariaz wrote:ok, the windows calculator might not be the best in the world but still, it allso reports an error, could it be one of those thing that the fpu just have a hard time handling?
-
- Member
- Posts: 33
- Joined: Wed Apr 26, 2006 11:00 pm
think that has some thing to do whit: 1 and 2 being "int's" so 1 / 2 = 0, after that he make's a double from the result..Zacariaz wrote:opps my bad, the trouble is not with the pow for my hand, its some else that i dont understand either.
when
double e = 1/2;
e == 0
for some reason
see:
double e = 1/2; e = 0
double e = (int) 1/ (int) 2; e = 0
double e = (double) 1/ (double) 2; e = 0.5
double e = 1.0 / 2.0; e = 0.5
-
- Member
- Posts: 62
- Joined: Fri Jun 29, 2007 8:36 pm
- Kevin McGuire
- Member
- Posts: 843
- Joined: Tue Nov 09, 2004 12:00 am
- Location: United States
- Contact:
I was about to post this, earlier today: pow(fabs(x), y).
Then I found this:
http://mathforum.org/library/drmath/view/52613.html
Which makes me very sure about:
pow(fabs(-8.0), 0.3333) being correct.
The reason being:
4 * 4 = 16
-4 * -4 = 16
That being rooted further in the way pow works to represent sqrt, and to keep from breaking any rules I think.
Then I found this:
http://mathforum.org/library/drmath/view/52613.html
Which makes me very sure about:
pow(fabs(-8.0), 0.3333) being correct.
The reason being:
4 * 4 = 16
-4 * -4 = 16
That being rooted further in the way pow works to represent sqrt, and to keep from breaking any rules I think.