# Re: [Rd] 0 ^ NaN == Inf, why?

From: John Chambers <jmc_at_r-project.org>
Date: Sat, 25 Oct 2008 15:43:26 -0400

Stephen Milborrow wrote:
> In R, 0 ^ NaN yields Inf. I would have expected NaN or perhaps 0. Is this
> behaviour intended?
>
Well, it certainly follows from the implementation. In the R_pow C routine,

double R_pow(double x, double y) /* = x ^ y */ {

if(x == 1. || y == 0.)
return(1.);
if(x == 0.) {

```      if(y > 0.) return(0.);
/* y < 0 */ return(R_PosInf);
```

}

It does seem, however, that NaN is the logical result here, which I think results from changing the implementation to:

if(x == 0.) {

```      if(y > 0.) return(0.);
else if(y < 0) return(R_PosInf);
else return(y); /* NA or NaN, we assert */
```
}

Other things being equal, `^` should follow the C pow() function for numeric arguments. After writing pow() as an R function that calls this C function:

> pow(0,NaN)
[1] NaN
> pow(0,NA)
[1] NA
> pow(0,0)
[1] 1

The second example presumably falls out because the C function returns its second argument if that is a NaN (which a numeric NA is, in C but not in R). The modified code in R_pow mimics that behavior.

Other opinions?

John

>

```>> sessionInfo()
>>
```

> R version 2.8.0 (2008-10-20)
> i386-pc-mingw32
```>
```

> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
```>

> attached base packages:

> [1] stats     graphics  grDevices utils     datasets  methods   base
>
```

> Steve Milborrow
> www.milbo.users.sonic.net
```>

> ______________________________________________

```
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

[[alternative HTML version deleted]]

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 25 Oct 2008 - 19:45:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 25 Oct 2008 - 20:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.