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. > # macheps.r J F Monahan, June 2001 > # revised August 2007, 2009 > # compute machine epsilon > # *** these results on R version 2.9.0 on Linux *** > # *** Vista version 2.9.1 gives old 2**(-53)+2**(-63) *** > # > # first try some values to get in ballpark > eps <- 2**(-56) > "2**(-56)" [1] "2**(-56)" > eps [1] 1.387779e-17 > "Is 1+eps bigger than 1 for eps = j*2**(-56), j = 1 to 16" [1] "Is 1+eps bigger than 1 for eps = j*2**(-56), j = 1 to 16" > (1!=(1+eps*(1:16))) # F if different, T if not [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE [13] TRUE TRUE TRUE TRUE > # > # look at representations with sprintf and "%a" > sprintf("%a",(1+eps*(1:16))) [1] "0x1p+0" "0x1p+0" "0x1p+0" [4] "0x1p+0" "0x1p+0" "0x1p+0" [7] "0x1p+0" "0x1p+0" "0x1.0000000000001p+0" [10] "0x1.0000000000001p+0" "0x1.0000000000001p+0" "0x1.0000000000001p+0" [13] "0x1.0000000000001p+0" "0x1.0000000000001p+0" "0x1.0000000000001p+0" [16] "0x1.0000000000001p+0" > # > # at this point it's around 8*2**(-56) or 2**(-53) > # or ... 64*2**(-59), so try 60 to 70 > "Is 1+eps bigger than 1 for eps = j*2**(-59), j = 60 to 70" [1] "Is 1+eps bigger than 1 for eps = j*2**(-59), j = 60 to 70" > eps <- 2**(-59) > (1!=(1+eps*(60:70))) [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE > # > # look at representations > sprintf("%a",(1+eps*(60:70))) [1] "0x1p+0" "0x1p+0" "0x1p+0" [4] "0x1p+0" "0x1p+0" "0x1.0000000000001p+0" [7] "0x1.0000000000001p+0" "0x1.0000000000001p+0" "0x1.0000000000001p+0" [10] "0x1.0000000000001p+0" "0x1.0000000000001p+0" > # > # another try > eps <- 2**(-53) # first guess + smaller > (1!=(1+eps*(1 + 2**(-51:-55)))) [1] TRUE TRUE FALSE FALSE FALSE > # > # look at representations > sprintf("%a",(1+eps*(1 + 2**(-51:-55)))) [1] "0x1.0000000000001p+0" "0x1.0000000000001p+0" "0x1p+0" [4] "0x1p+0" "0x1p+0" > # > # this is different -- the real machine epsilon > "Is 1+eps bigger than 1 for eps = 2**(-53) + 2**(-105)" [1] "Is 1+eps bigger than 1 for eps = 2**(-53) + 2**(-105)" > eps <- 2**(-53) + 2**(-105) > eps [1] 1.110223e-16 > # this should do it > sprintf("%a",eps) [1] "0x1.0000000000001p-53" > (1!=(1+eps)) [1] TRUE > # > # this is the same -- just a hair too small > "Is 1+eps bigger than 1 for eps = 2**(-53) + 2**(-106)" [1] "Is 1+eps bigger than 1 for eps = 2**(-53) + 2**(-106)" > eps <- 2**(-53) + 2**(-106) > eps [1] 1.110223e-16 > # this should be too much > sprintf("%a",eps) [1] "0x1p-53" > # test it > (1!=(1+eps)) [1] FALSE > # > rm(list=ls()) > q()