Dear everybody, How can I transform numbers to a positional system with the base of, e.g., nine, and do further operations with them? Thank you in advance Yours, sincerely Mag. Ferri Leberl
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. It would be useful if you could at least provide some examples of what you want to do. There are various ways of converting numbers back and forth. Are these integers or floating point? What type of operations do you want to do on them? On Sun, Feb 7, 2010 at 4:25 PM, Mag. Ferri Leberl <ferri.leberl at gmx.at> wrote:> Dear everybody, > How can I transform numbers to a positional system with the base of, e.g., nine, and do further operations with them? > Thank you in advance > Yours, sincerely > Mag. Ferri Leberl > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On 07/02/2010 4:25 PM, Mag. Ferri Leberl wrote:> Dear everybody, > How can I transform numbers to a positional system with the base of, e.g., nine, and do further operations with them?I don't understand what you want. Decimal, noval or binary are just ways to represent numbers as strings of characters. It doesn't make sense to me to say you are "transforming them" to a particular representation. You can represent them in a variety of ways: 10 (decimal), 11 (noval), 1010 (binary), ten (English), but it's still the same number. It does make sense to ask if you can convert numbers to one of these representations, or convert the representation back to the number; is that what you meant? Erich Neuwirth posted a function to do one way conversions: http://finzi.psych.upenn.edu/Rhelp10/2008-September/175003.html With his functions you can do > makeDigitSeq(numberInBase(10, 9)) [1] "11" Duncan Murdoch> Thank you in advance > Yours, sincerely > Mag. Ferri Leberl > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
The attached file gives functions to go both directions. I have used
it in class for many years.
This is very useful when studying machine representations of numbers,
for understanding mixed-radix number systems, for example time (days, hours,
minutes, seconds) or British money (pounds, shillings, pence), and for unique
indexing of cells in designed experiments.
Rich
-------------- next part --------------
## base
## Richard M. Heiberger
## See Section 12.1.4.2 of
## Richard M. Heiberger
## Computation for the Analysis of Designed Experiments
## Wiley, 1989
## defaults to 8 bit binary
base <- function(x, basis=c(2,2,2,2,2,2,2,2)) {
cb <- rev(cumprod(c(1,basis)))
xx <- x
y <- rep(0, length(cb))
for (i in 1:length(cb)) {
yy <- xx %/% cb[i]
if (yy > 0) {
y[i] <- yy
xx <- xx %% cb[i]
}
}
names(y) <- cb
y
}
baseinv <- function(y, basis=c(2,2,2,2,2,2,2,2)) {
sum(y * rev(cumprod(c(1,basis))))
}
base(200)
baseinv(.Last.value)
## British money
basis <- c(12,20) ## 12 pence per shilling, 20 shillings per pound sterling
base(498, basis)
baseinv(.Last.value, basis)
## American weight
base(100, 16) ## 16 ounces per pound avoirdupois
baseinv(.Last.value, 16)
## time
basis <- c(60,60,24) ## 60 seconds per minute, 60 minutes per hour, 24 hours
per day
x <- c(1, 2, 3, 40)
y <- baseinv(x, basis)
y
base(y, basis)
## binary arithmetic with 8 bits
basis <- c(2,2,2,2,2,2,2,2)
x <- 100
y <- base(x, basis)
y
baseinv(y, basis)
base(1)
baseinv(.Last.value)
base(200)
baseinv(.Last.value)
base(1000)
baseinv(.Last.value)
## IEEE with 53 base 2 digits
x <- c( 100000000000001, 100000000000002, 100000000000003,
1000000000000001, 1000000000000002, 1000000000000003,
10000000000000001, 10000000000000002, 10000000000000003 ## the last three
values illustrate
) ## the effects of
.Machine$double.eps
x
sprintf("%17.0f", x)
y <- sapply(x, base, basis=rep(2,54))
y
print(digits=17,
apply(y, 2, baseinv, basis=rep(2,54))
)
## base 9
a <- base(132, c(9,9,9))
b <- base(125, c(9,9,9))
a
b
a+b
baseinv(a+b, c(9,9,9))
base(baseinv(a+b, c(9,9,9)), c(9,9,9))