[R] (-8)^(1/3) == NaN?

jim holtman jholtman at gmail.com
Mon Jul 20 01:37:04 CEST 2009


It also works for raising a number to a negative integer:

> (-3)^(-3)
[1] -0.03703704
>


On Sun, Jul 19, 2009 at 6:23 PM, Rolf Turner<r.turner at auckland.ac.nz> wrote:
>
> 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 confidential. If you are not
> theintended recipient please delete the message and notify the sender.Any
> views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by
> MailMarshalwww.marshalsoftware.com
> ######################################################################
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list