Arbitrary Precision Arithmetic
- To: mathgroup at smc.vnet.net
- Subject: [mg18834] Arbitrary Precision Arithmetic
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Thu, 22 Jul 1999 08:19:43 -0400
- Sender: owner-wri-mathgroup at wolfram.com
I agree with the result below. If we multiply a number with 50 digits of precision by a machine precision number, all the extra digits are garbage. I get back a number with 16 digits of precision and this is good. In[1]:= pie=SetPrecision[Pi,50]; area=pie (1.25)^2; {Precision[pie], Precision[area]} Out[3]= {50, 16} ------------------------------ Next I multiply a number with only 4 digits of precision by a machine precision number and Mathematica doesn't know that the result should have 4 digits of precision. I don't like this. In[4]:= mass=SetPrecision[4.2,4]; density=mass / 1.25; {Precision[mass], Precision[density]} Out[6]= {4, 16} To do the calculation above correctly we can do something like the line below. In[7]:= density=mass / SetPrecision[1.25,6]; {Precision[mass], Precision[density]} Out[8]= {4, 4} -------------------------------------------- At first you might guess that if a numeric function gets a machine precision number it returns a machine precision number whenever possible. This isn't always true, and it can be hard to predict what you will get. Consider the line below, and look at the ByteCount of the results. If memory is a concern you might want to use N[Erf[x2]] to make sure the result is a machine number. In[9]:= x1=Erf[26.0]; x2=Erf[27.0]; {Precision[x1], Precision[x2]} Out[11]= {16, 331} In[12]:= {ByteCount[x1], ByteCount[x2]} Out[12]= {16, 192} ---------------------------------- In the line below (b1 = a1 + Pi - 1) and the kernel thinks (b1) has better precision than (a1). Wrong! In[12]:= a1 = Exp[1000.0] / Exp[1000]; b1 = a1 + Pi - 1; {Precision[a1], Precision[b1]} Out[14]= {13, 24} --------------------------------------- I learned from WRI tech support that the calculation above can be done correctly by using ($MinPrecision = -Infinity). In[15]:= $MinPrecision=-Infinity; In[16]:= b1 = a1 + Pi - 1; {Precision[a1], Precision[b1]} Out[17]= {13, 13} -------------------------------------- Sometimes the kernel leaves machine precision arithmetic unfinished. See the example below. In[18]:= x =15.0 / 0.125 +Log[1-Erf[7]] Out[18]= 120. +Log[1-Erf[7]] Below we see simply using N on the result above returns Indeterminate. That's why it was returned unfinished. We have to use arbitrary precision to get an ordinary number. However, we get back a machine number because of the machine number in the original calculation. In this case I am impressed with how careful the kernel is. In[19:= { N[x], N[x, 17] } Out[19]= {Indeterminate, 68.4718} In[20]:= Precision[ N[x, 17] ] Out[20]= 16 ------------------------------------- Regards, Ted Ersek ersektr at navair.navy.mil