[Rd] as.integer(x) versus as.integer(trunc(x))

From: Petr Savicky <savicky_at_cs.cas.cz>
Date: Fri, 12 Oct 2007 09:19:07 +0200


as.integer(x) rounds floating point numbers towards zero, so it behaves approximately as as.integer(trunc(x)).

If x > INT_MAX, then as.integer(x) is NA. This is nothing bad, but it is slightly more restrictive than necessary. An alternative approach could be that
  as.integer(x) is NA, if trunc(x) > INT_MAX and   as.integer(x) == INT_MAX for all x in [INT_MAX,INT_MAX+1).

Let me suggest the following patch to IntegerFromReal for consideration:

This patch changes the behavior as suggested above. Its effect may be seen using the script
  options(digits=12)
  x <- seq(2^31 - 3, 2^31, length=7)
  cbind(x,as.integer(trunc(x)),as.integer(x))

Original behavior:

  [1,] 2147483645.0 2147483645 2147483645
  [2,] 2147483645.5 2147483645 2147483645
  [3,] 2147483646.0 2147483646 2147483646
  [4,] 2147483646.5 2147483646 2147483646
  [5,] 2147483647.0 2147483647 2147483647
  [6,] 2147483647.5 2147483647         NA
  [7,] 2147483648.0         NA         NA

Using the patch:

  [1,] 2147483645.0 2147483645 2147483645
  [2,] 2147483645.5 2147483645 2147483645
  [3,] 2147483646.0 2147483646 2147483646
  [4,] 2147483646.5 2147483646 2147483646
  [5,] 2147483647.0 2147483647 2147483647
  [6,] 2147483647.5 2147483647 2147483647
  [7,] 2147483648.0         NA         NA

Petr Savicky.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 12 Oct 2007 - 07:22:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 25 Oct 2007 - 11:37:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.