Strange behaviour of as.integer()
Use round(), floor(), or ceiling() to convert numbers with possible fractional parts to numbers without fraction parts. as.integer()'s main use is to convert from one internal representation (i.e., bit pattern) of a number to another so you can interface to C or Fortran code. Note that as.integer(x) also doesn't "work" when abs(x)>2^31, while round(), floor(), and ceiling() do work up to c. 2^52. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ulrich Keller Sent: Thursday, January 07, 2010 4:32 AM To: r-help at r-project.org Subject: [R] Strange behaviour of as.integer() 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 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?
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.