[R] (-8)^(1/3) == NaN?
Rolf Turner
r.turner at auckland.ac.nz
Mon Jul 20 00:23:30 CEST 2009
On 20/07/2009, at 9:13 AM, jim holtman wrote:
> If the power that a number is being raised to is integer, then is does
> evaluate honoring the unary minus.
>
>> (-2) ^ 5 #integer power
> [1] -32
>> (-2) ^ 5.1
> [1] NaN
<snip>
I was vaguely aware of this ... but it now triggers in my mind the
question of how the ^ function decides when the exponent is an integer.
A bit of experimentation seems to indicate that, e.g., (-2)^x ``works''
if (and only if?) round(x)==x returns TRUE.
Note that (-2)^x may NOT ``work'' in some cases were all.equal(x,round
(x))
returns TRUE.
Young players should also be aware of the following trap. It
can happen that n + epsilon ``is an integer'' according to my
rule, but m + epsilon is NOT an integer according to this rule.
Where m and n are both integers.
E.g.:
> eps <- 0.4e-15
> x <- 5+eps
> x==round(x)
[1] TRUE
> y <- 3+eps
> y==round(y)
[1] FALSE
This is of course due to the exigencies of how n and m are represented
in floating point arithmetic. Not too deep once you're aware of the
problem, but it can still be a ``gotcha'' if one is not alert.
(Be alert. The world needs more lerts!)
cheers,
Rolf Turner
P. S. Perhaps young players should be reminded at this point that
is.integer() is
no help here. This function tells you about the ***storage mode***
of its argument.
Only.
R. T.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list