({{, (RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,p]
yields a version of expr in which all numbers have been set to have precision p. }})

OpenersNotesSection(Spacer1)Details

When (RefLink[SetPrecision, paclet:ref/SetPrecision]) is used to increase the precision of a number, the number is padded with zeros. The zeros are taken to be in base 2. In base 10, the additional digits are usually not zeros.

(RefLink[SetPrecision, paclet:ref/SetPrecision]) returns an arbitrary-precision number, even if the precision requested is less than (RefLink[$MachinePrecision, paclet:ref/$MachinePrecision]).

(RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,(RefLink[MachinePrecision, paclet:ref/MachinePrecision])] converts numbers in expr to machine precision.

If there are numbers too large or small to represent machine-precision numbers, (RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,(RefLink[MachinePrecision, paclet:ref/MachinePrecision])] will convert them to arbitrary-precision numbers with precision (RefLink[$MachinePrecision, paclet:ref/$MachinePrecision]).

If expr contains machine-precision numbers, (RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,p] can give results that differ from one computer system to another.

(RefLink[SetPrecision, paclet:ref/SetPrecision]) will first expose any hidden extra digits in the internal binary representation of a number, and, only after these are exhausted, add trailing zeros. »

0.004`25 generates a number with all trailing digits zero and precision 25 on any computer system.

(RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,p] does not modify expr itself.

OpenersPrimaryExamplesSection(Spacer1)Examples  (18)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Convert all numbers in an expression to 20-digit precision:

SetPrecision[a+2 b+3 c,20]

Convert all numbers to machine precision:

SetPrecision[a+2 b+3c,MachinePrecision]

Convert from a machine number to an arbitrary-precision number:

SetPrecision[2.1,20]

OpenersExampleSection1(Spacer1)Scope  (5)

Set precision of a complex number:

SetPrecision[Pi+I,32]

Convert approximate numbers to exact rational numbers:

SetPrecision[f[.1,N[1/10,30]],Infinity]

The result has trailing zeros once any hidden digits are exposed:

TableForm[Table[{p,SetPrecision[.1,p]},{p,15,65,5}]]

(RefLink[SetPrecision, paclet:ref/SetPrecision]) does not affect exact powers:

p = (x+Pi)^(9/2)

SetPrecision[p,18]

This allows you to, for example, change the precision of polynomial coefficients:

poly = 1.+2. x+3. x^2 + 4. x^3;

SetPrecision[poly,20]

Inexact powers are modified:

SetPrecision[x^1.25,20]

Special rules may apply to data objects:

ifun = Interpolation[{0,0,1,1}]

For an (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction]) object, (RefLink[SetPrecision, paclet:ref/SetPrecision]) changes the appropriate data only:

spifun=SetPrecision[ifun, 20]

InputForm[spifun]

It still works as an approximate function, but with the new precision:

{ifun[5/3],spifun[5/3]}

OpenersExampleSection2(Spacer1)Applications  (4)

Find the roundoff error in evaluating an expression with machine numbers:

e=Hold[(Sin[1.00000000001]-Sin[1.0])/(1.00000000001-1.0)]

em=ReleaseHold[e]

eb=ReleaseHold[SetPrecision[e,32]]

eb-em

This dominates the approximation error since the increment is so small:

eb-Cos[1]

Find the representation error of a machine number:

e=N[1/10];

ee=SetPrecision[e,Infinity]

{error=ee-1/10,N[error]}

The error is small because this is the closest machine number to 1/10:

ep=SetPrecision[e(1+$MachineEpsilon),Infinity];
em=SetPrecision[e(1 - $MachineEpsilon/2),Infinity];

N[{ep-1/10,1/10-em}]

The distance between nearest machine numbers is a power of two:

Log[2,{ep-ee,ee-em}]

Raise the precision of coefficients in a differential equation to check error:

duffing={x''[t]+.15x'[t]-x[t]+x[t]^3==.3Cos[t],x[0]==0,x'[0]==0};

dbig=SetPrecision[duffing,32]

Find the solutions computed at machine precision and precision 32:

mpsol=First[NDSolve[duffing,x,{t,100}]]

bigsol=First[NDSolve[dbig,x,{t,100},WorkingPrecision->32,MaxSteps->Infinity]]

Plot the two solutions. They have deviated by t=100, indicating significant error:

Plot[Evaluate[x[t]/.{mpsol,bigsol}],{t,0,100},PlotStyle->{{Red},{Green}}]

Override the default accuracy and precision model:

bit=Log[10.,2.];
f[x_]:=Module[{p=Precision[x],lx},
lx=Block[{$MaxPrecision=p,$MinPrecision=p},
4*x*(1-x)];
SetPrecision[lx,p-bit]]

x0=N[1/3,20];

fl=NestList[f,x0,20]

This loses precision more slowly than the default model that treats operations as independent:

ll=NestList[4#(1-#)&,x0,20]

Nonetheless, all digits given are correct:

fl-NestList[4#(1-#)&,1/3,20]

The bit loss per iteration is justified because the map is effectively a shift map:

RSolve[x[i+1]==4x[i](1-x[i]),x,i]

OpenersExampleSection3(Spacer1)Properties & Relations  (5)

(RefLink[SetPrecision, paclet:ref/SetPrecision]) just sets the precision of numbers while (RefLink[N, paclet:ref/N]) works adaptively:

N[Sin[1000],20]

Since (RefLink[N, paclet:ref/N]) works adaptively, the result has the requested precision of 20:

Precision[%]

Use (RefLink[SetPrecision, paclet:ref/SetPrecision]):

SetPrecision[Sin[1000],20]

The precision is less than 20 because of the conditioning of the sine function at 1000:

Precision[%]

(RefLink[SetPrecision, paclet:ref/SetPrecision]) effectively evaluates (RefLink[Sin, paclet:ref/Sin]) with argument 1000 to precision 20:

Sin[1000`20]

(RefLink[N, paclet:ref/N]) will typically not raise the precision of an expression, while (RefLink[SetPrecision, paclet:ref/SetPrecision]) will:

e=f[1.]+Pi;

ne=N[e,20]

spe=SetPrecision[e,20]

{Precision[e],Precision[ne],Precision[spe]}

For nonzero numbers x, (RefLink[SetPrecision, paclet:ref/SetPrecision])[x,p] is equivalent to (RefLink[SetAccuracy, paclet:ref/SetAccuracy])[x,p-e]:

xl=Table[RandomReal[10^RandomInteger[{-9,9}],WorkingPrecision->RandomReal[{15,30}]],{4}]

Map[SetPrecision[#,20]&,xl]

e is given by (RefLink[RealExponent, paclet:ref/RealExponent]):

Map[SetAccuracy[#,20-RealExponent[#]]&,xl]

(RefLink[SetPrecision, paclet:ref/SetPrecision])[x,∞] and (RefLink[Rationalize, paclet:ref/Rationalize])[x,0] give rational approximations for real x:

x = N[π]

(RefLink[Rationalize, paclet:ref/Rationalize])[x,0] gives a rational that is equivalent to x up to the precision of x:

r=Rationalize[x,0]

r-x

(RefLink[SetPrecision, paclet:ref/SetPrecision])[x,∞] gets a rational directly from the bitwise representation of x:

SetPrecision[x,∞]

{d,e} = RealDigits[x,2]

d.(2^Range[e-1,e-1-52,-1])

(RefLink[SetPrecision, paclet:ref/SetPrecision])[x,∞] and (RefLink[RootApproximant, paclet:ref/RootApproximant])[x] give exact approximations for real x:

x=N[E]

(RefLink[RootApproximant, paclet:ref/RootApproximant])[x] gives an algebraic number equivalent to x up to the precision of x:

r=RootApproximant[x]

r-x

(RefLink[SetPrecision, paclet:ref/SetPrecision])[x,∞] gets a rational directly from the bitwise representation of x:

SetPrecision[x,∞]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

(RefLink[SetPrecision, paclet:ref/SetPrecision])[z,prec] will make an exact zero for approximate zeros z:

z = N[Pi,20] - (21053343141/6701487259)

SetPrecision[z,20]

If you want an approximate zero, use (RefLink[SetAccuracy, paclet:ref/SetAccuracy]):

SetAccuracy[z,22]

The only exception to this is that when prec is (RefLink[MachinePrecision, paclet:ref/MachinePrecision]), you get a machine zero:

SetPrecision[z,MachinePrecision]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[N, paclet:ref/N])  ▪  (RefLink[Precision, paclet:ref/Precision])  ▪  (RefLink[Chop, paclet:ref/Chop])  ▪  (RefLink[SetAccuracy, paclet:ref/SetAccuracy])  ▪  (RefLink[$MinPrecision, paclet:ref/$MinPrecision])  ▪  (RefLink[$NumberMarks, paclet:ref/$NumberMarks])  ▪  (RefLink[PrecisionGoal, paclet:ref/PrecisionGoal])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Arbitrary-Precision Numbers, paclet:tutorial/ArbitraryPrecisionNumbers])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Precision & Accuracy Control, paclet:guide/PrecisionAndAccuracyControl])}})}})