How to find the machine number nearest to an exact number -- N[] fails
- To: mathgroup at smc.vnet.net
- Subject: [mg103869] How to find the machine number nearest to an exact number -- N[] fails
- From: Alain Cochard <alain at geophysik.uni-muenchen.de>
- Date: Sat, 10 Oct 2009 07:07:47 -0400 (EDT)
Hello. 6.0 for Linux x86 (32-bit) (February 7, 2008) I had always thought that N[exact number] gives the machine number closest to that exact number. But consider the following exact number ex=14539079527638557142952619724749887685325578333570\ 9865657255467349279813823794598008431302162071157\ / \ 8000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000; find its machine number representation: num=N[ex]; and determine the corresponding rounding error: err=N[Abs[ex- SetPrecision[num,Infinity]] ] For me this gives 3.59393 10^-16, which is larger than Eps = $MachineEpsilon = 2^-52 ~ 2.22 10^-16. But, between 1 and 2, the spacing between machine numbers is Eps. So the rounding error should be smaller than or equal to Eps/2 ~ 1.11*10^-16 if the rounding is to the nearest machine number. To investigate further, consider the binary expansions of ex: binex=RealDigits[ex,2,55] which gives: Out[4]= {{1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, > 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, > 0, 1, 0, 1, 1, 0, 0, 1, 0}, 1} ^ ^-53rd digit Thus I would expect that the nearest binary number should end up either like (rounding below): > 0, 1, 0, 1, 1, 0, 0}, 1} or like (rounding above): > 0, 1, 0, 1, 1, 0, 1}, 1} whereas the actual binary expansion is given by: binum=RealDigits[num,2] and ends as > 0, 1, 0, 1, 0, 1, 1}, 1} In order to check, let us artificially construct those 2 other rounding numbers: binum2=binum; binum2[[1,51]]=1; binum2[[1,52]]=0; binum2[[1,53]]=0; binum3=binum2; binum3[[1,53]]=1; and compute again the rounding errors: err2= N[ Abs[ ex-FromDigits[binum2,2] ]] err3= N[ Abs[ ex-FromDigits[binum3,2] ]] This gives 1.37348 10^-16 for err2, which is this time less than Eps and 8.46964 10^-17 for err3, which is less then Eps/2, as expected. So, in this case, not only does N[] not give the nearest machine number, but it "jumps" a machine number. Is this a normal behavior? Is there a flaw in my reasoning above? Is there an easy way to automatically generate the nearest machine number? Many thanks in advance. Alain PS: all outputs are identical if num is evaluated as SetPrecision[ex,MachinePrecision] instead of by N[ex].
- Follow-Ups:
- Re: How to find the machine number nearest to an exact
- From: danl@wolfram.com
- Re: How to find the machine number nearest to an exact