Re: Error in Subtraction with V4.0??
- To: mathgroup at smc.vnet.net
- Subject: [mg22537] Re: [mg22463] Error in Subtraction with V4.0??
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Thu, 9 Mar 2000 03:24:24 -0500 (EST)
- References: <200003080722.CAA13113@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
BEIntInc wrote: > > Can someone please explain why > > 1. - .999999999999 > > results in > 9.999778782798785*10^-13 > > The old way (by hand) gives me > > 1x10^-12 > > Is this an error or am I missing something? > > TIA, > > John Brooks The result is correct for machine arithmetic and the short explanation is that you are seeing massive cancellation at machine precision. To see in more detail what is happenning, we might do as follows. First we'll check the bit representation of (machine precision) 1 and 999999999999/10^12. To avoid introducing sources of error from binary-to-decimal conversion we'll numericize the exact entity for the latter. In[33]:= ??$NumberBits $NumberBits[x] gives a list containing the sign, a list of the bits thought to be correct, a list of the bits not thought to be correct, and the binary exponent. The number x must have a head of Real. Attributes[$NumberBits] = {Listable, Protected} In[34]:= e1 = 1.; e2 = N[999999999999/10^12]; In[36]:= InputForm[$NumberBits[e1]] Out[36]//InputForm= {1, {}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1} In[37]:= InputForm[$NumberBits[e2]] Out[37]//InputForm= {1, {}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1}, 0} When we subtract in binary arithmetic from a power of two we can get the same effect by complementing the bit-string for the second argument and multiplying by appropriate powers of two, except that the lowest '1' bit is uncomplemented. (If this is not clear, consider subtracting (binary) 1101 from 10000. You get 11.) d2 = $NumberBits[e2][[3]]; bitComplement[bit_] := Mod[1+bit,2] d3 = Map[bitComplement, d2]; d3 = MapAt[bitComplement, d3, -1] In[115]:= InputForm[num = d3 . 2^Range[-1,-53,-1]] Out[115]//InputForm= 9007/9007199254740992 In[116]:= InputForm[num2 = N[num]] Out[116]//InputForm= 9.999778782798785*^-13 Daniel Lichtblau Wolfram Research
- References:
- Error in Subtraction with V4.0??
- From: beintinc@aol.com (BEIntInc)
- Error in Subtraction with V4.0??