Skip to content

Displaying numerics to full double precision

5 messages · Jeff Delmerico, Ben Bolker, Liviu Andronic

#
I'm working on a shared library of C functions for use with R, and I want to
create a matrix in R and pass it to the C routines.  I know R computes and
supposedly stores numerics in double precision, but when I create a matrix
of random numerics using rnorm(), the values are displayed in single
precision, and also exported in single precision when I pass them out to my
C routines.  An example is below:
[,1]      [,2]      [,3]      [,4]
[1,] 14.907606 17.572872 19.708977  9.809943
[2,]  9.322041 13.624452  7.745254  7.596176
[3,] 10.642408  6.151546  9.937434  6.913875
[4,] 14.617647  5.577073  8.217559 12.115465
[1] "double"

Does anyone know if there is a way to change the display or storage settings
so that the values will be displayed to their full precision?   Or does
rnorm only produce values to single precision? 

Any assistance would be greatly appreciated.

Thanks,
Jeff Delmerico
#
Jeff Delmerico wrote:
options("digits") # 7
options(digits=x)

  I may be mistaken, but I think the values are indeed exported
as double precision -- the issue here is just a display setting.

  Ben Bolker
#
Thanks Ben, that fixed the display within R.  However, even after changing
the display settings, the matrix elements still appear to be exported in
single precision.  The matrix object is being passed into my C routines as
an SEXP Numeric type, and somewhere along the way, some of the digits are
getting lost.  
Here's the relevant bit of my C code:

SEXP
divideMatrix(SEXP matrix_in, SEXP sub_height, SEXP sub_width, SEXP fileS)
...
if ( isMatrix(matrix_in) && isNumeric(matrix_in) )
{
	/* Use R macros to convert from SEXP to C types */
	matrix = REAL(matrix_in);
	height = INTEGER(GET_DIM(matrix_in))[0];
	width = INTEGER(GET_DIM(matrix_in))[1];
	subW = INTEGER_VALUE(sub_width);
	subH = INTEGER_VALUE(sub_height);
        ...
}

Am I using the wrong macro to convert into a double in C?  Any ideas?

Thanks,
Jeff Delmerico
Ben Bolker wrote:

  
    
#
Jeff Delmerico wrote:
I'm not sure.
  I do know  that Rinternals.h has

#define REAL(x)         ((double *) DATAPTR(x))

  so that doesn't seem to be the problem ...

  Ben
#
On 12/5/07, Jeff Delmerico <jeffdelmerico at yahoo.com> wrote:
As far as I know (beware, I'm a novice), internally R stores to and
uses full precision. The display of the data, however, is controlled
by "digits". You'd need to put, say, options(digits=7) in your
Rprofile.site (if it doesn't exist, creat it in the R etc/ folder).
You might also be interested by "scipen". Check ?options.

Regards,
Liviu