# adjust.r J F Monahan, June 2001 # revised August 2007 # write positive number in d*2**i format adjust <- function(x) { if( x <= 0 ) c( 0, -2**31 ) else { # flag if not positive i <- 0 while (x > 16 ) { x <- x/16 i <- i + 4 } while (x < 1 ) { x <- x*16 i <- i - 4 } c(x,i) } } x <- c(13, -3, 128, .35, 257) # vector of examples di <-sapply(x,adjust) # two rows of results # # write it out pretty xdi <- paste( 'x', format(x), '= d*2**i ',format(di[1,]), format(di[2,])) cat(xdi,sep="\n") rm(list=ls()) # clean up