Re: specifying arbitrary precision thruout calculations
- To: mathgroup at christensen.cybernetics.net, rich at earth.nwu.edu
- Subject: [mg442] Re: [mg440] specifying arbitrary precision thruout calculations
- From: withoff (David Withoff)
- Date: Thu, 2 Feb 1995 20:02:20 -0600
> Is there any way to specify, at the start of a session, or at the start of > a Mathematica script or program, that one would like MMA to use a given > precision (i.e. something other than $MachinePrecision) thruout the > calculations? It doesn't appear to me that "PrecisionGoal" is what I want > as that is an option for specific numerical functions. In other words, > what I want is something like "SetPrecision[]", but not for a specific > expression but rather a global default value which tells MMA to assume such a > precision for all expressions and numerical calculations, unless specified > otherwise. Then when > one typed "x=3.", x would automatically have whatever precision one had > globally set at the beginning of the session. I guess this would > correspond to use of C or Fortran compiler flags like "-r8" that > instruct the compiler that all variables are to be double precision > (for example). Is there a way of doing such a thing in mathematica? > > Thanks for your help and advice. > > Jonathan Rich Although it is theoretically possible to do this, I'm not sure you would be happy with the result. An essential idea in Mathematica arithmetic is that precision is variable. For example, in the operation In[4]:= 1000000000000000000000000000000000000000007.91 - 1000000000000000000000000000000000000000002.47 Out[4]= 5.44 In[5]:= Precision[%] Out[5]= 3 Mathematica correctly reports that the result has very low precision. If the precision were fixed, as it is in conventional Fortran, the precision of the result would be artifically raised to get a result with the required precision. Mathematically, this is a dubious thing to do, and obligates the user or programmer to do a separate calculation if they want to know how many digits to believe. Many of the algorithms in Mathematica assume that arithmetic is more reliable than this. These algorithms will fail if precision is raised in some arbitrary way. This reliability has a price -- fixed precision is simpler than variable precision -- but getting a wrong answer by a simple method is not usually an advantage. If you want Mathematica to treat inexact numbers as having, say, at least 25 significant digits, you can use $MinPrecision = 25. In[6]:= $MinPrecision = 25 Out[6]= 25 In[7]:= 1000000000000000000000000000000000000000007.91 - 1000000000000000000000000000000000000000002.47 Out[7]= 5.440000000000001278976924 There is also a variable $MaxPrecision that can be used to set an upper limit to precision. As can be seen from this example, the underlying arithmetic has not changed. Several arbitrary digits are appended to the number to give it the required precision. Numbers entered with fewer digits than machine precision will still be treated as machine numbers. Since the resulting machine arithmetic can be a problem for algorithms that are now expecting higher precision, it is best not to enter machine numbers at all. As a convenience, you could do something like In[32]:= $Pre = ((# /. p_?MachineNumberQ :> SetPrecision[p, $MinPrecision]) &) Out[32]= #1 /. (p_)?MachineNumberQ :> SetPrecision[p, $MinPrecision] & In[33]:= 2.7 Out[33]= 2.7000000000000001776356839 to automatically convert machine numbers in interactive input into numbers with a precision of $MinPrecision. These changes are sufficient for arithmetic and for certain other simple operations. For more complicated operations (like NIntegrate), which usually make implicit assumptions about the reliability of basic arithmetic, it is preferable to work directly with WorkingPrecision and other options. Dave Withoff Research and Development Wolfram Research