15
$\begingroup$

There are multiple ways to convert an expression to machine precision, for example:

In[1]:= a = Sqrt[2]
Out[1]= Sqrt[2]

In[2]:= {1.a, 1`a, N@a, SetPrecision[a,MachinePrecision]}
Out[2]= {1.41421,1.41421,1.41421,1.41421}

In[3]:= Precision /@ %
Out[3]= {MachinePrecision,MachinePrecision,MachinePrecision,MachinePrecision}

My question is whether or not these methods are absolutely equivalent. Is it just a matter of personal taste which one to use, or are there examples where they behave differently?

$\endgroup$
1
  • 3
    $\begingroup$ You can also use Developer`ToPackedArray[{1, Sqrt[2], 3, 4}, Real] to create machine precision numbers. $\endgroup$
    – user21
    Commented Jun 28, 2012 at 17:35

1 Answer 1

14
$\begingroup$

In terms of speed N and SetPrecision can be expected to be faster as they do not involve an unnecessary multiplication. (Conversely 2` * a would be better than N[2 * a] because the latter does exact multiplication before the conversion.)


1. a and 1` a can be considered identical because they represent the same input. Personally I have taken to using the latter form for entering machine-precision integers because the syntax better reminds me of the purpose.


One can see that N and SetPrecision[#, MachinePrecision] & are, if not equivalent, closely related. Observe:

N[thing] := 17.5

NValues[thing]
{HoldPattern[N[thing, {MachinePrecision, MachinePrecision}]] :> 17.5}

Now:

N[thing]

SetPrecision[thing, MachinePrecision]
17.5

17.5

The fact that NValues output is given from SetPrecision indicates to me that it is using a common mechanism.

On-the-fly conversion does not use NValues:

1. thing

2` + thing
1. thing

2. + thing

Here is another demonstrable difference between N/SetPrecision and multiplication by 1.:

N[ Exp[1000] ]                            // Precision

SetPrecision[Exp[1000], MachinePrecision] // Precision

1. Exp[1000]                              // Precision
12.9546

12.9546

15.9546
$\endgroup$
5
  • $\begingroup$ N also does some caching or am I mistaken? $\endgroup$
    – Ajasja
    Commented Jun 28, 2012 at 11:10
  • $\begingroup$ @Ajasja I don't know, but I'll see what I can find. $\endgroup$
    – Mr.Wizard
    Commented Jun 28, 2012 at 11:18
  • $\begingroup$ Here is the relevant example reference.wolfram.com/mathematica/ref/ClearSystemCache.html $\endgroup$
    – Ajasja
    Commented Jun 28, 2012 at 12:34
  • $\begingroup$ @Ajasja that kind of caching is not specific to N. Try: 1`1000000 Pi; // Timing twice. $\endgroup$
    – Mr.Wizard
    Commented Jun 28, 2012 at 12:52
  • $\begingroup$ @Mr.Wizard, thanks. I like the idea of using the backtick form as a reminder of the purpose. I think it looks a bit neater than 1. too (I always want to add a zero after the decimal point even though I know it isn't necessary) $\endgroup$ Commented Jul 1, 2012 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.