Re: Understanding N and Precision

• To: mathgroup at smc.vnet.net
• Subject: [mg71199] Re: [mg71147] Understanding N and Precision
• From: Carl Woll <carlw at wolfram.com>
• Date: Fri, 10 Nov 2006 06:37:59 -0500 (EST)
• References: <200611090838.DAA15669@smc.vnet.net>

```Alain Cochard wrote:

>Hi.  I would like to understand the following behavior:
>
>   Mathematica 5.2 for Linux
>   Copyright 1988-2005 Wolfram Research, Inc.
>    -- Motif graphics initialized --
>
>   In[1]:= MatrixForm[{{exact1=Cos[3Pi/2+Pi/7], Precision[exact1]}, \
>		       {exact2=Cos[40139127975 Pi/14], Precision[exact2]}}]
>
>
>   Out[1]//MatrixForm=     23 Pi
>		       Cos[-----]
>			    14               Infinity
>
>			   40139127975 Pi
>		       Cos[--------------]
>				 14          Infinity
>
>just checking:
>
>   In[2]:= FullSimplify[exact1-exact2]
>
>   Out[2]= 0
>
>   In[3]:= MatrixForm[{{float1=N[exact1], Precision[float1]},\
>		       {float2=N[exact2], Precision[float2]}}]
>
>
>   Out[3]//MatrixForm= 0.433884           MachinePrecision
>
>		       0.433883           MachinePrecision
>
>So the N of supposedly(?) 2 identical numbers is different (although
>the precision is indeed the same). That's what I would like to
>understand most.
>
>
In this case N is applied to the argument of Cos, and then Cos of this
approximate number is evaluated. So the two approximate numbers are:

r1=N[23 Pi/14]
r2=N[40139127975 Pi/14]
5.16119
9.0072×10^9

As machine numbers we would expect r1 to be accurate to about +/-10^-15
and r2 to be accurate to about +/-10^-6. With machine number input I
think Cos does some sort of Mod operation of its argument, so the next
step is:

a1=Mod[r1,2Pi]//FullForm
a2=Mod[r2,2Pi]//FullForm
5.161187930897517`
5.161186218261719`

Here you can see that a1 and a2 differ by about 10^-6 which as I said
earlier is to be expected. Consequently the difference you are seeing
when applying N shouldn't be surprising.

>Also, if I specify the precision for N, it now gives the same.  E.g.:
>
>   In[4]:= MatrixForm[{{float1=N[exact1,3], Precision[float1]},\
>		       {float2=N[exact2,3], Precision[float2]}}]
>
>
>   Out[4]//MatrixForm= 0.434   3.
>
>		       0.434   3.
>
>
>
Here you are not using machine numbers anymore, and with extended
precision numbers Mathematica can use significance arithmetic to try to
ensure that the result has the requested precision.

>including the case where the precision asked is MachinePrecision:
>
>
>   In[5]:= MatrixForm[{{float1=N[exact1,\$MachinePrecision], Precision[float1]},\
>		       {float2=N[exact2,\$MachinePrecision], Precision[float2]}}]
>
>   Out[5]//MatrixForm= 0.4338837391175581   15.9546
>
>		       0.4338837391175581   15.9546
>
>Why in this case does it gives a precision of "15.9546" and not
>"MachinePrecision", as above, especially since
>
>   In[6]:= N[\$MachinePrecision,Infinity]
>
>   Out[6]:= 15.9546
>
>Isn't N[x] equivalent to N[x,\$MachinePrecision]?
>
>
No, N[x] is equivalent to N[x, MachinePrecision] (notice that there is
no \$ in front). \$MachinePrecision is the numerical precision of machine
numbers, and so N[x, \$MachinePrecision] gives an extended precision
number (not a machine number) with 15.9546 digits of precision. Notice
that MachinePrecision does not evaluate to anything, just like Pi:

MachinePrecision
MachinePrecision

However, it does have a numerical value:

N[MachinePrecision]
15.9546

This numerical value is the same thing as \$MachinePrecision:

\$MachinePrecision
15.9546

I hope this helps clear things up.

Carl Woll
Wolfram Research

>Thanks in advance for any tip.
>
>Alain
>
>

```

• Prev by Date: Re: comparing implicit 0 with machine floats
• Next by Date: condition placement
• Previous by thread: Re: Understanding N and Precision
• Next by thread: Matrix question