GaryF
05-06-2008, 09:18 AM
I'm getting output like this:
> a=function(x) x^(2)
> a(-1)
[1] 1
> a=function(x) x^(3)
> a(-1)
[1] -1
> a=function(x) x^(3.14)
> a(-1)
[1] NaN
> -1^(3.14)
[1] -1
All I want is a function that will let me take x to the pi, I want to use the real pi, but I'm not able to even use an estimate according to this.
I guess this must be something simple I'm doing wrong, but I sure can't figure out what it is.
Mean Joe
05-06-2008, 01:35 PM
I know nothing about R. But -1^(3.14) is read by the computer, using order of operations, as - [ 1^(3.14) ]. So first it evaluates the exponent, and 1^(any power) = 1, then applies the negative sign.
I think if you try (-1)^3.14 you'll get NaN again.
When you have a fraction exponent, e.g. 0.5, I'm not sure if you can put negative numbers into the function, e.g. (-1)^0.5 would mean the square root of -1.
TheEcologist
05-06-2008, 02:03 PM
I know nothing about R. But -1^(3.14) is read by the computer, using order of operations, as - [ 1^(3.14) ]. So first it evaluates the exponent, and 1^(any power) = 1, then applies the negative sign.
I think if you try (-1)^3.14 you'll get NaN again.
When you have a fraction exponent, e.g. 0.5, I'm not sure if you can put negative numbers into the function, e.g. (-1)^0.5 would mean the square root of -1.
Mean Joe is of course absolutely right but there is a way around this
The solution to (-x)^(1/a) has a real and imaginary part: a+bi
Therefore simply consistently use complex numbers
a(-1+0i)
Solution : -0.9048271-0.4257793i
which is the only answer when you have a fractional exponent
where a=function(x) {x^(pi) }
Edit:
or build it in your code
a=function(x,expon=pi) {x=x+0i ; x^(expon) }
a(-1)
-0.9048271-0.4257793i
when you have a number that only has a real part like
a(2,expon=2)
4+0i
you can remove the imaginary part by using Re(x) so
Re(a(2,expon=2))
4