R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing 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 an HTML browser interface to help. Type 'q()' to quit R. > # chtab21.r September 2009, March 2010 > # > # floating point representations using sprintf and "%a" > # > # list of interesting numbers > onep <- 1 + 2**(-16) # a little more than one > full <- 2 - 2^(-52) # biggest with zero exponent > # internal representation > cbind(c(onep,full),sprintf("%a",c(onep,full))) [,1] [,2] [1,] "1.00001525878906" "0x1.0001p+0" [2,] "2" "0x1.fffffffffffffp+0" > nums <- c(2,1,1/16,0,-15,2**53-1, # exactly rep integer + 2**53+1, # no longer exact + 2**(-1022)*onep, # still normalized + 2**(-1023)*onep, # just denormalized + 2**(-1058)*onep, # very small positive + 2**(-1074), # smallest positive + 2**(-1075)*onep, # too small + (2**1023)*onep, # almost biggest + (2**1023)*full, # biggest + (2**1024)*onep) # too big > nums [1] 2.000000e+00 1.000000e+00 6.250000e-02 0.000000e+00 -1.500000e+01 [6] 9.007199e+15 9.007199e+15 2.225108e-308 1.112554e-308 3.237958e-319 [11] 4.940656e-324 0.000000e+00 8.988603e+307 1.797693e+308 Inf > # names for those numbers > names <- c("two","one","sixteenth","zero","neg fifteen", + "exactly rep integer","no longer exact","still normalized", + "just denormalized","very small positive","smallest positive", + "too small", "almost biggest","biggest","too big") > # internal representation > internal <- sprintf("%a",nums) > rbind(internal,nums,names) [,1] [,2] [,3] [,4] [,5] internal "0x1p+1" "0x1p+0" "0x1p-4" "0x0p+0" "-0x1.ep+3" nums "2" "1" "0.0625" "0" "-15" names "two" "one" "sixteenth" "zero" "neg fifteen" [,6] [,7] [,8] internal "0x1.fffffffffffffp+52" "0x1p+53" "0x1.0001p-1022" nums "9007199254740991" "9007199254740992" "2.22510781043986e-308" names "exactly rep integer" "no longer exact" "still normalized" [,9] [,10] internal "0x0.80008p-1022" "0x0.0000000010001p-1022" nums "1.11255390521993e-308" "3.23795802314978e-319" names "just denormalized" "very small positive" [,11] [,12] [,13] internal "0x0.0000000000001p-1022" "0x0p+0" "0x1.0001p+1023" nums "4.94065645841247e-324" "0" "8.9886028274133e+307" names "smallest positive" "too small" "almost biggest" [,14] [,15] internal "0x1.fffffffffffffp+1023" "Inf" nums "1.79769313486232e+308" "Inf" names "biggest" "too big" > rm(list=ls()) > q()