Skip to content
Prev 269986 / 398502 Next

Fractional component of a number

On 28-Aug-11 03:59:51, edward.m at psu.ac.th wrote:
It depends on what you mean by "wrong"! Perhaps "unexpected"
might be better!

The key to the matter is described in '?as.integer' under "Value":

  Non-integral numeric values are truncated towards zero
  (i.e., ?as.integer(x)? equals ?trunc(x)? there) ...

so that

  as.integer(2)
  # [1] 2
  as.integer(2 + 2e-14)
  # [1] 2
  as.integer(2 - 2e-14)
  # [1] 1
  as.integer(-2 + 2e-14)
  # [1] -1
  as.integer(-2 - 2e-14)
  # [1] -2


so it is doing "what it says on the box". Yes, you are better
using round()!

Hoping this helps to clear it up,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 28-Aug-11                                       Time: 09:24:14
------------------------------ XFMail ------------------------------