On Sat, 16 Jul 2005, Bernardo Rangel Tura wrote: > At 10:11 12/7/2005, [EMAIL PROTECTED] wrote: > >> hi all >> >> why does R do this: >> >> (-8)^(1/3)=NaN >> >> the answer should be : -2 >
Yes and no. The problem is that the reciprocal of 3 is not exactly representable as a floating point number (it has an infinite binary expansion .010101010101...) So the R expression 1/3 actually returns a number slightly different from one-third. It is a fraction with denominator a power of two (probably 2^53). Now, -8 to power that is a fraction with denominator a power of 2 is not a real number, so, NaN. It would be nice if R could realize that you meant the cube root of -8, but that requires either magical powers or complicated and unreliable heuristics. The real solution might be a function like root(x,a,b) to compute x^(a/b), where a and b could then be exactly representable integers. If someone wants to write one.... -thomas ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html