Bugzilla – Bug 15624
Don't use floor(x+0.5)
Last modified: 2014-01-10 16:15:26 UTC
For purposes of rounding a floating-point number to the nearest integer,
this does not work:
x = floor (x + 0.5);
The proper thing to do is rint, round, or nearbyint -- with some consideration
as to which one to use if x might be negative. nearbyint is likely the one
to use most of the time.
The problem affects two classes of numbers:
(a) nextafter(0.5,0) -- the number just below 0.5 gets rounded to 1.
(b) +- (2^52+odd) -- adding 0.5 really adds 1
This is visible from the R command line:
 -1.578226e+17 <-- good
 -Inf <-- bad
In dpois(2^52 + 1, 1, 1) : non-integer x = 4503599627370497.000000
You do not mention which of the many uses in the R sources you have in mind: helpful people do.
nearbyint and rint have only become available recently (I am not sure all R platforms have them both yet).
This particular case is fixed.
Created attachment 1553 [details]
Output of grep -Hrin floor.*\\.5
Here are some additional instances. This is checked against R-patched_2014-01-08_r64705.tar.gz. I ran the following command:
grep -Hrin floor.*\\.5 *
Attached please find is the output. Hope this helps.