[R] log10(), floor() combo issue?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Oct 14 12:31:02 CEST 2002


"E.L. Willighagen" <egonw at sci.kun.nl> writes:

> The reason is the combination of floor() and log10():
> 
> log10(1000) = 3 
> 
> ok, but:
> 
> floor(log10(1000)) = 2
> 
> ? Ok, I can live with some floating point inaccuracy, but this seems
> at least a bit strange, and to me troublesome as it makes the bindec()
> method fail...

Well, the reason is of course that

> print(log10(1000),18)
[1] 2.99999999999999956

Having log10 exact for integer powers of 10 is apparently too much to
ask from a binary computer, although it might in principle be possible
(but I believe log10(x) is computed as log(x)/log(10)). For negative
powers it really is impossible.
 
> I've also tried to force the output of log10(1000) into integer format
> with
> 
> floor(as.integer(log10(b)))
> 
> However, with the same disappointing results...

(as.integer() truncates, so floor() is superfluous)

The only way out is to add "fuzz" as in

floor(log10(b)+1e-10)

-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list