R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > # 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") x 13.00 = d*2**i 13.000000 0 x -3.00 = d*2**i 0.000000 -2147483648 x 128.00 = d*2**i 8.000000 4 x 0.35 = d*2**i 5.600000 -4 x 257.00 = d*2**i 1.003906 8 > > rm(list=ls()) # clean up >