RE: [R] Why is 1 a double?

From: Mulholland, Tom <Tom.Mulholland_at_dpi.wa.gov.au>
Date: Wed 13 Apr 2005 - 14:21:54 EST


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@yahoo.com]
> Sent: Wednesday, 13 April 2005 10:12 AM
> To: r-help@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@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
>



R-help@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 Received on Wed Apr 13 14:27:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST