Re: Machine arithmetics Q?

*To*: mathgroup at smc.vnet.net*Subject*: [mg3678] Re: Machine arithmetics Q?*From*: Robert Knapp <rknapp>*Date*: Fri, 5 Apr 1996 02:52:24 -0500*Organization*: Wolfram Research, Inc.*Sender*: owner-wri-mathgroup at wolfram.com

Arturas Acus wrote: > > Hello, > > It is known, that following two formulas > give the same result. > > sinusas[i_]:=sinusas[i]=N[Sin[2^i*ArcSin[Sqrt[x0]]]^2,prec]; > iter[i_]:=iter[i]=4*iter[i-1]*(1-iter[i-1]); > > I want to demonstrate, that we must calculate correct. > That is, if we use $MachinePrecision numbers after some > steps we will get random numbers: > > prec=5;maximum=100; > iter[0]=x0=N[1/3,prec]; > deltalist=ListPlot[Table[ > N[(sinusas[i]-iter[i]),prec],{i,maximum}]] > > This result is ok., because we > have positive and negative differences in calculations. > > Now I want to show, that if we will use 20 digits, and > machine arithmetic (Not Big Numbers Arithmetic !) > we will get chaos later. Therefore I remove Big Numbers Arithmetic: > > $MinPrecision=$MaxPrecision=20; > prec=20; > > and perform the same calculations. What I get is quite different! > After some iterations I get only negative deviations! > Can anybody explain why this happens? > I use Mathematica 2.2.1 for Windows. > This raises some good questions. I hope some of the examples and explanation below will be informative. In Mathematica, machine numbers and bignums (arbitrary precision numers) are used together. Generally, when machine numbers and bignums are mixed together, the result will be a machine number. For example: In[1]:= MachineNumberQ[N[Pi,17]*1.1] Out[1]= True However, this will not happen when the result (or sometimes intermediate results) is not representable by machine numbers: In[2]:= MachineNumberQ[N[10^1000,17]*1.1] Out[2]= False Generally, when one uses N without a second argument, machine numbers are produced, for example, In[3]:= MachineNumberQ[a = N[Sin[1]]] Out[3]= True however, there is no guarantee that this will happen--for eaxmple, N[10^1000] will not be a machine number. Now we get into the part that is more subtle. Bignums can also be called arbitrary precision numbers because they can (within system limits) any precision you want. In particular they can have precision equal to $MachinePrecision. In contrast to N, the command SetPrecision generally produces bignums. For example, on the result from the previous command: In[4]:= MachineNumberQ[SetPrecision[a,$MachinePrecision]] Out[4]= False We get a bignum with Precision equal to $MachinePrecision. Now suppose we set: In[5]:= $MinPrecision = $MaxPrecision = $MachinePrecision Out[5]= 16 This does not preclude the possibility of bignums because they can have this precision. In fact, sometimes bignums are produced when they would otherwise not have been. FOr example, with this setting, the same command I ran before gives In[6]:= MachineNumberQ[a = N[Sin[1]]] Out[6]= False a bit of a surprise. The reason is that the quantity you see as $MachinePrecision is rounded. On this machine (a SPARC 5), $MachinePrecision is 16. In actuality, the machine precision is 53 bits, which is really like 15.95 digits. This discrepancy is what causes the above result to be a bignum with the settings for $MinPrecison and $MaxPrecision. In some places, the rounding is taken into account, and so this does not preclude the possiblity of using a machine number: In[7]:= MachineNumberQ[b = 1.23] Out[7]= True and they will still appear in arithmetic results: In[8]:= MachineNumberQ[a*b] Out[8]= True The real effect of setting $MinPrecsion = $MaxPrecision = const is to have the system do fixed precision arithmetic. When const is equal to $MachinePrecision, and your calculation gives bignums, this will be different than machine arithmetic because bignums employ guard bits to ensure accuracy which machine numbers do not. My guess is that what is occurring in your calculation is that the N is producing bignums, so you arenot using machine arithmetic. If you want to ensure that you are using machine numebrs at each stage, I suggest you check with MachineNumberQ in your calculations to be sure. -- Rob Knapp Wolfram Research, Inc. http://www.wri.com/~rknapp ==== [MESSAGE SEPARATOR] ====