[Rd] Why no .Machine$sizeof.double?

Thomas Lumley tlumley at u.washington.edu
Tue Oct 18 22:45:28 CEST 2005


On Tue, 18 Oct 2005, Earl F. Glynn wrote:

> "Thomas Lumley" <tlumley at u.washington.edu> wrote in message
> news:Pine.LNX.4.63a.0510181039440.30016 at homer24.u.washington.edu...
>> On Tue, 18 Oct 2005, Earl F. Glynn wrote:
>>
>>> Whis is there a .Machine$sizeof.longdouble but no
> .Machine$sizeof.double?
>>>
>>
>> sizeof(double) is always 8 and sizeof(int) is always 4, because R requires
>> the IEEE/IEC standard arithmetic types.  R will not compile with any other
>> sizes.
>
> But it's a common, recommended software engineering practice to define
> mnemonic, named constants.
>
> If I were to see code like .Machine$sizeof.double * N.COL * N.ROW, I know
> that's the number of bytes in a matrix of doubles. If I see code that is 8 *
> N.COL * N.ROW, I can guess what "8" means, but I could guess wrong.  I wrote
> code that looks just like this today because I couldn't find the defined
> constant.  Will someone else reading my code automatically know what the "8"
> means?
>

But why would you ever want to write either .Machine$sizeof.double * N.COL 
* N.ROW or 8 * N.COL * N.ROW?

If you are doing memory allocation in R then numeric() automatically 
allocates things of the correct size. If you are doing memory allocation 
in C then you should use sizeof(double) or, even better, sizeof(*yourpointer).
In both cases, the language has the facility to let you work in the 
correct units without explicit constants, named or unnamed.

You would have a stronger case for arguing that there should be typedefs
in C so that you didn't need to remember that R's numeric type is double 
(rather than, er, float?)and its integer type is int (rather than long)
  and in fact we do provide
  typedef int Sint;
in R.h.

 	-thomas



More information about the R-devel mailing list