[R] round() seems inconsistent when rounding 5s

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Mar 16 11:36:43 CET 2003


ripley at stats.ox.ac.uk writes:

> As people have already suggested, inexact representation.
> 
> 2.45 is being represented as 2.4500000000000002 on my system when printed
> out from the C code in fround.  It is not a binary fraction and so cannot
> be represented exactly (in standard hardware).  The calculation does (2.45
> - 2)*10 and rounds it, and that is just greater than 4.5 (which could be
> represented exactly).
> 
> Try this:
> 
> > x <- 2.45
> > (100*x - 245)
> [1] 2.842171e-14
> 
> which is fairly firm evidence that the representation error is positive, 
> as multiplication by 100 should be exact.  Note though that the print 
> routine will multiply by powers of 10 so the only sure way is to read the 
> bit pattern and compute exactly from that.

Ah, I see now. What I saw before was 

        ltmp = x + 0.5;
        /* implement round to even */
        if(fabs(x + 0.5 - ltmp) < 10*DBL_EPSILON
           && (ltmp % 2 == 1)) ltmp--;
        tmp = ltmp;

but that code sits inside

#ifdef USE_BUILTIN_RINT

and if that's not on we get the system rint() which very likely
will not care about using fuzz.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-help mailing list