[R] R: to the power
Duncan Murdoch
murdoch at stats.uwo.ca
Tue Jul 12 16:07:22 CEST 2005
On 7/12/2005 9:53 AM, allan_sta_staff_sci_main_uct at mail.uct.ac.za wrote:
> hi all
>
> i simply wanted to work with real numbers and thought that (-8)^(1/3) should
> work.
It might work in an ideal world, but not in the R floating point world.
There's no way to express (1/3) exactly. Since (-8)^(1/3 + epsilon)
(restricted to the reals) is not defined for epsilon near zero but not
exactly zero, it's really hopeless to expect R to give you what you wanted.
*You* know that you're taking an odd root of a negative number, but R
doesn't. You need to use your knowledge to rewrite that mathematical
expression as the mathematically equivalent -(8^(1/3)) and then things
will be fine.
Duncan Murdoch
>
> sorry for not making the question clearer.
>
> /
> allan
>
> Quoting Duncan Murdoch <murdoch at stats.uwo.ca>:
>
>> On 7/12/2005 9:29 AM, Robin Hankin wrote:
>> > Hi
>> >
>> > I find that one often needs to keep reals real and complexes complex.
>> >
>> > Try this:
>> >
>> > "cuberooti" <-
>> > function (x)
>> > {
>> > if (is.complex(x)) {
>> > return(sqrt(x + (0+0i)))
>> > }
>> > sign(x)* abs(x)^(1/3)
>> > }
>> >
>> >
>> > best wishes
>> >
>> > [see that (0+0i) sitting there!]
>>
>> I don't understand this.
>>
>> 1. I don't think you meant to use sqrt() there, did you??
>>
>> 2. What effect does the 0+0i have? x has already been determined to be
>> complex.
>>
>> Duncan Murdoch
>>
>
>
>
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
More information about the R-help
mailing list