[R] Why is 1 a double?

Mulholland, Tom Tom.Mulholland at dpi.wa.gov.au
Wed Apr 13 06:21:54 CEST 2005


I'm not sure that this answers your questions but maybe they partly help.

p. 7 in An introduction to R notes

"For most purposes the user will not be concerned if the "numbers" in a numeric vector
are integers, reals or even complex. Internally calculations are done as double precision real
numbers, or double precision complex numbers if the input data are complex."

p. 13 of the R Language Definition notes

"Numeric calculations whose result is undefined, such as '0/0' produce (on most, if not all,
platforms) the value NaN. This exists only in the double type."

p. 164 of the Reference manual notes


"Integer vectors exist so that data can be passed to C or Fortran code which expects them, and so that
small integer data can be represented exactly and compactly.
Note that on almost all implementations of R the range of representable integers is restricted to
about ±2 × 109: doubles can hold much larger integers exactly."

Tom

> -----Original Message-----
> From: Vivek Rao [mailto:rvivekrao at yahoo.com]
> Sent: Wednesday, 13 April 2005 10:12 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Why is 1 a double?
> 
> 
> Based on examples in R books and the syntax of other
> programming languages, I expected that
> 
> n <- 10
> 
> assigns the integer 10 to n, but typeof(n) is actually
> a double. The subscripting expression x[1] is valid,
> but sprintf("\n %d",1) is not, giving the error
> 
> Error in sprintf("\n %d", 1) : use format %f, %e or %g
> for numeric objects
> 
> One must use instead sprintf("\n %d",as.integer(1)). 
> 
> Two questions:
> 
> (1) When one intends to define an integer variable,
> should a construction such as
> 
> n <- as.integer(10)
> 
> be used? Most examples I have seen don't do this. 
> 
> (2) Why wasn't the S language defined so that 
> typeof(1) is "integer" rather than "double"?
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list