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

Detlef Steuer Detlef.Steuer at unibw-hamburg.de
Mon Oct 14 14:51:39 CEST 2002

```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)) = 2

Just 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```