[R] log10(), floor() combo issue?
Remigijus Lapinskas
remigijus.lapinskas at maf.vu.lt
Mon Oct 14 20:17:45 CEST 2002
bindec (at least, on my computer) works only for moderate values of b:
bindec <- function(b){
as.i <- as.integer(unlist(strsplit(b,"")))
print(as.i)
fl <- 2^(floor(round(log10(b),10)):0)
print(fl)
dec <- sum(as.i * fl)
dec
}
> bindec(10000)
[1] 1 0 0 0 0
[1] 16 8 4 2 1
[1] 16
> bindec(100000)
[1] 1 NA NA 0 5
[1] 32 16 8 4 2 1
[1] NA
Warning messages:
1: NAs introduced by coercion
2: longer object length
is not a multiple of shorter object length in: as.i * fl
The reason is that b soon jumps to scientific notation:
> b <- 100000
> b
[1] 1e+05
Here is a slightly improved version of bindec:
> bindec.n <- function(b) {
as.i <- as.integer(unlist((strsplit(b, ""))))
print(as.i)
fl <- 2^(floor(log10(as.numeric(b)+1.0e-7):0))
print(fl)
dec <- sum(as.i * fl)
dec}
> bindec.n("100000")
[1] 1 0 0 0 0 0
[1] 32 16 8 4 2 1
[1] 32
> bindec.n("1000000000000000000")
[1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[1] 131072.0 65536.0 32768.0 16384.0 8192.0 4096.0 2048.0 1024.0
[9] 512.0 256.0 128.0 64.0 32.0 16.0 8.0 4.0
[17] 2.0 1.0 0.5
[1] 131072
Best,
Remigijus
Monday, October 14, 2002, 2:51:39 PM, you wrote:
DS> 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))
DS> 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
DS> Just throw in a round of rounding (say to 10 digits):
DS> floor(round(log10(b),10)).
DS> This repairs the numerical inaccuracies of taking logarithms,
DS> if those are not too large :-).
>> floor(round(log10(1000),10))
DS> [1] 3
DS> 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
>> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
>> _
DS> "There is no way to peace, peace is the way." -- Ghandi
DS> Detlef Steuer --- http://fawn.unibw-hamburg.de/steuer.html
DS> ***** Encrypted mail preferred *****
DS> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
DS> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
DS> Send "info", "help", or "[un]subscribe"
DS> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
DS> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Linkejimai,
Remigijus mailto:remigijus.lapinskas at maf.vu.lt
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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