Page 1 of 2
c/c++ pow function question
Posted: Sun Jul 01, 2007 8:52 am
by PyroMathic
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
Posted: Sun Jul 01, 2007 9:17 am
by Kevin McGuire
pow(0.25, 0.5) which is sqrt(0.25) = pow(0.25, 0.5), right?
Posted: Sun Jul 01, 2007 9:26 am
by PyroMathic
Kevin McGuire wrote:pow(0.25, 0.5) which is sqrt(0.25) = pow(0.25, 0.5), right?
yes.
0.25 ^ 0.5 = 0.25 ^ (1/2)
so same as square root of (0.25).
Posted: Sun Jul 01, 2007 10:07 am
by Kevin McGuire
Is that what was not working, or as I am guessing it is something else?
Posted: Sun Jul 01, 2007 10:34 am
by PyroMathic
Kevin McGuire wrote:Is that what was not working, or as I am guessing it is something else?
The square root would work, just the problem is what if i need:
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
Posted: Sun Jul 01, 2007 11:21 am
by Zacariaz
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?
Posted: Sun Jul 01, 2007 11:28 am
by PyroMathic
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?
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.
Posted: Sun Jul 01, 2007 11:30 am
by Zacariaz
i just did a test with my compiler, i dont get an error, how ever if i do x^(1/n)
it doesnt matter which value x and n has, it returns 1.
Posted: Sun Jul 01, 2007 11:40 am
by Zacariaz
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
Posted: Sun Jul 01, 2007 11:46 am
by Zacariaz
ok, funny enough pow was the shortterm solution for me: pow(n, -1), anyhow, i get the same error.
Posted: Sun Jul 01, 2007 11:56 am
by PyroMathic
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
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..
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
Posted: Sun Jul 01, 2007 12:47 pm
by Zacariaz
Ya, though it was something like that, just didnt think straight, tryed (among other things) e = double(1/2) which ofcourse didnt work.
Posted: Sun Jul 01, 2007 4:30 pm
by Ninjarider
for some reason when working with a double you need to have the periods. not sure if its anything like the fpu registers. but it needs the decimal to properly setup the registers to handle a decimal point.
Posted: Sun Jul 01, 2007 7:03 pm
by Zacariaz
im really more interested in a solution/explanation to the pow problem as i most likely will run into the same problem sooner or later.
Posted: Sun Jul 01, 2007 7:48 pm
by Kevin McGuire
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.