[R] Integer vs numeric
(Ted Harding)
Ted.Harding at manchester.ac.uk
Mon Jan 28 22:46:32 CET 2008
On 28-Jan-08 21:23:12, Roland Rau wrote:
> Christophe Genolini wrote:
>> Hi the list.
>>
>> I do not understand the philosophy behind numeric and integer.
>> - 1 is numeric (which I find surprising)
>> - 2 is numeric.
>> - 1:2 is integer.
>> Why is that ?
>>
> I hope I can answer your question at least partly:
> Numeric means "double", i.e. internally stored as a double precision
> floating point number. As far as I know this is the default.
> You can, however, force an object to be, e.g. an integer, a single
> prevision float or a double precision float.
> as.integer(12)
> as.single(12)
> as.double(12)
>
> So far I only needed this if I wanted to call some C or Fortran code.
> Maybe there are other applications when you need to force the type?
>
> I hope this helps at least a bit further?
>
> Roland
Further to the above: The help
?":"
says:
Value:
For numeric arguments [as opposed to factors],
a numeric vector. This will be of type 'integer'
if 'from' and 'to' are both integers and
representable in the integer type, otherwise of
type 'numeric'.
By "if 'from' and 'to' are both integers" I understand
it to mean "if the values of 'from' and 'to' are
integers (in the mathematical realm)", i.e. this
does not refer to the R type.
Thus:
a<-1; b<-2
str(a)
# num 1
str(b)
# num 2
str((a:b))
# int [1:2] 1 2
so a and b were numeric when created, but since their
values are integers (etc.) (a:b) has integer type.
Presumably this is for computational efficiency.
Integer arithmetic on a computer is faster than
floating-point arithmetic; and if the computation
can be done in the CPU registers ("register arithmetic")
then it is faster still (as I presume is the case here).
Mind you. I'm guessing here. I have had nothing to do
with the implementation of arithmetic in R, so cannot
answer authoritatively for the motivations of those
who did!
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 28-Jan-08 Time: 21:46:28
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list