---------- Forwarded message ---------- From: Roger.Bergande at swisslife.ch Date: Mon, 29 Mar 2010 13:51:12 +0200 Subject: .Call and .C arguments To: bergarog at gmail.com Dear List My question is about .C and .Call I was experimenting with the .C and .Call interface as I came across the following behaviour. The passed values are not the same in C. I 'm calling a function in C with the argument as.double(1204.245) but in the debug mode in C the value has changed to 1204.2449999999999999. Is there a way to pass the arguments differently? I'm using Windows and Visual Studio C++ 2005 Express Edition and R-2.10.1. Please see the two simple examples to understand the issue. # C call from R .C("myroundC",as.double(1204.245)) // C Code void myroundC(double *Amount){ *Amount = Rf_fround(*Amount,2); } #Return value in R [[1]] [1] 1204.24 # C call from R .Call("myroundCall",as.double(1204.245)) // C Code SEXP myroundCall(SEXP a){ double *ap = REAL(a), *ansp; SEXP ans; PROTECT(ans = allocVector(REALSXP, 1)); ansp = REAL(ans); *ansp = Rf_fround(*ap,2); UNPROTECT(1); return(ans); } #Return value in R [1] 1204.24 # expected value 1204.25 Thanks a lot for your help. Best regards Roger Bergande
On 29/03/2010 7:56 AM, Roger Bergande wrote:> ---------- Forwarded message ---------- > From: Roger.Bergande at swisslife.ch > Date: Mon, 29 Mar 2010 13:51:12 +0200 > Subject: .Call and .C arguments > To: bergarog at gmail.com > > Dear List > > > > My question is about .C and .Call > > > > I was experimenting with the .C and .Call interface as I came across the > following behaviour. > > The passed values are not the same in C. I 'm calling a function in C > with the argument as.double(1204.245) but in the debug mode in C the > value has changed to 1204.2449999999999999. >What makes you think those two numbers are different? See FAQ 7.31. Duncan Murdoch> > > Is there a way to pass the arguments differently? > > > > I'm using Windows and Visual Studio C++ 2005 Express Edition and > R-2.10.1. > > > > > > Please see the two simple examples to understand the issue. > > > > # C call from R > > .C("myroundC",as.double(1204.245)) > > > > > > // C Code > > > > void myroundC(double *Amount){ > > > > *Amount = Rf_fround(*Amount,2); > > > > } > > > > #Return value in R > > [[1]] > > [1] 1204.24 > > > > > > > > # C call from R > > .Call("myroundCall",as.double(1204.245)) > > > > // C Code > > SEXP myroundCall(SEXP a){ > > double *ap = REAL(a), *ansp; > > SEXP ans; > > PROTECT(ans = allocVector(REALSXP, 1)); > > ansp = REAL(ans); > > *ansp = Rf_fround(*ap,2); > > UNPROTECT(1); > > return(ans); > > } > > > > #Return value in R > > [1] 1204.24 > > > > # expected value 1204.25 > > > > > > Thanks a lot for your help. > > Best regards > > Roger Bergande > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On Mon, Mar 29, 2010 at 01:56:14PM +0200, Roger Bergande wrote: ...> The passed values are not the same in C. I 'm calling a function in C > with the argument as.double(1204.245) but in the debug mode in C the > value has changed to 1204.2449999999999999.I do not see this value printed in the examples below. So, i think you assume that 1204.245 was changed to 1204.2449999999999999 since rounding it to 2 digits produces 1204.24. The problem is indeed a rounding problem, but not when the number is passed to C. The number 1204.245 cannot be represented in double precision exactly. So, already at the R level is, actually formatC(1204.245, digits=20) # [1] "1204.2449999999998909" See http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy or FAQ 7.31 for more examples. Petr Savicky.> > Is there a way to pass the arguments differently? > > > > I'm using Windows and Visual Studio C++ 2005 Express Edition and > R-2.10.1. > > > > > > Please see the two simple examples to understand the issue. > > > > # C call from R > > .C("myroundC",as.double(1204.245)) > > > > > > // C Code > > > > void myroundC(double *Amount){ > > > > *Amount = Rf_fround(*Amount,2); > > > > } > > > > #Return value in R > > [[1]] > > [1] 1204.24 > > > > > > > > # C call from R > > .Call("myroundCall",as.double(1204.245)) > > > > // C Code > > SEXP myroundCall(SEXP a){ > > double *ap = REAL(a), *ansp; > > SEXP ans; > > PROTECT(ans = allocVector(REALSXP, 1)); > > ansp = REAL(ans); > > *ansp = Rf_fround(*ap,2); > > UNPROTECT(1); > > return(ans); > > } > > > > #Return value in R > > [1] 1204.24 > > > > # expected value 1204.25 > > > > > > Thanks a lot for your help. > > Best regards > > Roger Bergande > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >