MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: SetPrecision vs N

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71699] Re: SetPrecision vs N
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 27 Nov 2006 04:04:04 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <ekbmmt$f9g$1@smc.vnet.net>

Andrew Moylan wrote:
> Hi all,
> 
> Suppose I want to evaluate an expression at a given precision. What is
> the difference between using N[expr, precision] and using
> SetPrecision[expr, precision]?

. N[expr, n] attempts to give a result with nâ??-digit precision.

==> N[] does *NOT* change the precision of an expression. That is, a 
machine-precision number is still a machine-precision number.

. SetPrecision[expr, p] yields a version of expr in which all numbers 
have been set to have precision p.

==> SetPrecision[] *CHANGES* the precision of the expression, paddding 
numbers with additional zeros if needed (in base 2).  Therefore, having 
used SetPrecision[], you have change the computational model from 
machine arithmetic to arbitrary-precision arithmetic.

So, for less than 16-digit precision (typical 32-bit machine) both 
function might appear equivalent, although they are not because the 
computational model is different.  And to work with higher precision, 
you must use arbitrary precision, that is SetPrecision[] (N[] is 
hopeless in this case.)

In[1]:=
val = 1./3

Out[1]=
0.333333

In[2]:=
Precision[N[val]]

Out[2]=
MachinePrecision

In[3]:=
Precision[N[val, 15]]

Out[3]=
MachinePrecision

In[4]:=
Precision[N[val, 20]]

Out[4]=
MachinePrecision

In[5]:=
Precision[SetPrecision[val, 15]]

Out[5]=
15.

In[6]:=
Precision[SetPrecision[val, 20]]

Out[6]=
20.

Regards,
Jean-Marc


  • Prev by Date: Re: Limit of Infinitely Nested Expression (4.0 succeeds, 5.2 fails...)
  • Next by Date: Re: Strange use of FrontEnd`FileName
  • Previous by thread: Re: SetPrecision vs N
  • Next by thread: RE: Re: SetPrecision vs N