Skip to content
Prev 33471 / 63424 Next

Inaccurate complex arithmetic of R (Matlab is accurate)

On 04/08/2009 1:41 PM, Richardson, Ash wrote:
R uses the C runtime, not C++.  But the cpow function was not very good. 
  Blame Microsoft for that on Windows, some other scapegoat on whatever 
other platform you use.

Martin M. has now committed some changes to R-devel that give these 
results on Windows:

 > #-- Classical Rosenbrock function in n variables
 > rosen <- function(x) {
+ n <- length(x)
+ x1 <- x[2:n]
+ x2 <- x[1:(n-1)]
+ sum(100*(x1-x2^2)^2 + (1-x2)^2)
+ }
 >
 >
 > x0 <- c(0.0094, 0.7146, 0.2179, 0.6883, 0.5757, 0.9549, 0.7136, 
0.0849, 0.4147, 0.4540)
 > h <- c(1.e-15*1i, 0, 0, 0, 0, 0, 0, 0, 0, 0)
 > xh <- x0 + h
 >
 > rx <- rosen(xh)
 > Re(rx)
[1] 190.3080
 > Im (rx)
[1] -4.667764e-15

These seem to agree with the desired numbers.

Duncan Murdoch