Hi all, in my search for a nice binary2decimal method, I received this nice code (thanx to Uwe Ligges): bindec <- function(b) sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0)) It fails, however, with:> bindec(1000)[1] 4 Warning message: longer object length is not a multiple of shorter object length in: as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0) 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... 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... Ideas? kind regards, Egon -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"E.L. Willighagen" wrote:> > Hi all, > > in my search for a nice binary2decimal method, I received this nice > code (thanx to Uwe Ligges): > > bindec <- function(b) > sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0)) > > It fails, however, with: > > > bindec(1000) > [1] 4 > Warning message: > longer object length > is not a multiple of shorter object length in: > as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0) > > 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... > > 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...Sorry, I haven't thought about that. Another, numerical more stable solution is: bindec <- function(b){ temp <- as.integer(unlist(strsplit(b, ""))) sum(temp * 2^((length(temp) - 1):0)) } bindec(1000) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
You will find that a `fuzz', typically 1.0e-7, is added to quantities before floor() or as.integer() all over R. seq.default() is one example. For log10(1000) we use log(1000)/log(10), and that is not a ratio of integers. On Mon, 14 Oct 2002, E.L. Willighagen wrote:> > Hi all, > > in my search for a nice binary2decimal method, I received this nice > code (thanx to Uwe Ligges): > > bindec <- function(b) > sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0)) > > It fails, however, with: > > > bindec(1000) > [1] 4 > Warning message: > longer object length > is not a multiple of shorter object length in: > as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0) > > 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... > > 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... > > Ideas? > > kind regards, > > Egon > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 14-Oct-2002 E.L. Willighagen wrote:> > Hi all, > > in my search for a nice binary2decimal method, I received this nice > code (thanx to Uwe Ligges): > > bindec <- function(b) > sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0))Nice function!> > It fails, however, with: > >> bindec(1000) > [1] 4 > Warning message: > longer object length > is not a multiple of shorter object length in: > as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0) > > The reason is the combination of floor() and log10(): > > log10(1000) = 3 > > ok, but: > > floor(log10(1000)) = 2Just throw in a round of rounding (say to 10 digits): floor(round(log10(b),10)). This repairs the numerical inaccuracies of taking logarithms, if those are not too large :-).> floor(round(log10(1000),10))[1] 3 detlef> > ? 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... > > 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... > > Ideas? > > kind regards, > > Egon > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > - > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _"There is no way to peace, peace is the way." -- Ghandi Detlef Steuer --- http://fawn.unibw-hamburg.de/steuer.html ***** Encrypted mail preferred ***** -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._