The case
I have compiled shared library for dyn.loading into R. Then when I call the wrapper function in R I get result rounded with truncated part after decimal point. I am probably missing something well known as it is my first try to use .C function, but I can’t find what is the cause of rounding. There is my code below.
My C code
#include <freesteam/steam_pT.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <R.h>
// gcc -Wall -fPIC -c Rsteam_h_pT_2.c -o Rsteam_h_pT_2.o -I/usr/local/include -I/opt/R-2.14.0/lib/R/include -L/usr/local/lib -lfreesteam
// gcc -Wall -shared -o Rsteam_h_pT_2.so Rsteam_h_pT_2.o -I/usr/local/include -I/opt/R-2.14.0/lib/R/include -L/usr/local/lib -lfreesteam
// function
double steam_h_pT(double p, double T){
SteamState S = freesteam_set_pT(p, T);
double s = freesteam_h(S);
return s;
}
// wrapper
double steam_h_pT(double, double);
void steam_h_pT_R_wrapper(double *p, double *T, double *result) { *result = steam_h_pT(*p, *T); }
My R function
steam_h_pT <- function(p_Pa,T_K)
{
dyn.load('Rsteam_h_pT.so')
# Call the C function
returned_data = .C('steam_h_pT_R_wrapper', p=as.numeric(p_Pa), T= as.numeric(T_K), result=numeric(1))
# Return the value of the result parameter
return(returned_data$result)
}
My R session
> steam_h_pT(100000,400)
[1] 2730398
> str(steam_h_pT(100000,400))
num 2730398
>
Expected result from original code
bash>./steam_h_pT 1e5 400
2730397.845968
Now, why it gets rounded in R?
It the representation due to the
digitsargument to theprintfunction. The number is still stored correctly.You can set the global default with
options(digits=15).