c/c++ pow function question

Programming, for all ages and all languages.
PyroMathic
Member
Member
Posts: 33
Joined: Wed Apr 26, 2006 11:00 pm

c/c++ pow function question

Post 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
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

pow(0.25, 0.5) which is sqrt(0.25) = pow(0.25, 0.5), right?
PyroMathic
Member
Member
Posts: 33
Joined: Wed Apr 26, 2006 11:00 pm

Post 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).
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Is that what was not working, or as I am guessing it is something else?
PyroMathic
Member
Member
Posts: 33
Joined: Wed Apr 26, 2006 11:00 pm

Post 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
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

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

Post 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.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

ok, funny enough pow was the shortterm solution for me: pow(n, -1), anyhow, i get the same error.
PyroMathic
Member
Member
Posts: 33
Joined: Wed Apr 26, 2006 11:00 pm

Post 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
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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.
Ninjarider
Member
Member
Posts: 62
Joined: Fri Jun 29, 2007 8:36 pm

Post 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.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

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