[R] Strange behaviour of as.integer()

David Winsemius dwinsemius at comcast.net
Thu Jan 7 14:14:18 CET 2010


On Jan 7, 2010, at 7:31 AM, Ulrich Keller wrote:

> I have encountered a strange behaviour of as.integer() which does not
> seem correct to me. Sorry if this is just an indication of me not
> understanding floating point arithmetic.
>
>> .57 * 100
> [1] 57
>> .29 * 100
> [1] 29
>
> So far, so good. But:
>
>> as.integer(.57 * 100)
> [1] 56
>> as.integer(.29 * 100)
> [1] 28
>

 From help page for as.integer:
"Non-integral numeric values are truncated towards zero (i.e.,  
as.integer(x) equals trunc(x) there), "

> Then again:
>
>> all.equal(.57 * 100, as.integer(57))
> [1] TRUE
>> all.equal(.29 * 100, as.integer(29))
> [1] TRUE
>
> This behaviour is the same in R 2.10.1 (Ubuntu and Windows) and 2.9.2
> (Windows),
> all 32 bit versions. Is this really intended?


Yes, it works as documented.

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list