SetPrecision[expr,p]
yields a version of expr in which all numbers have been set to have precision p.
SetPrecision
SetPrecision[expr,p]
yields a version of expr in which all numbers have been set to have precision p.
Details
- When 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.
- SetPrecision returns an arbitrary‐precision number, even if the precision requested is less than $MachinePrecision.
- SetPrecision[expr,MachinePrecision] converts numbers in expr to machine precision.
- If there are numbers too large or small to represent machine-precision numbers, SetPrecision[expr,MachinePrecision] will convert them to arbitrary-precision numbers with precision $MachinePrecision.
- If expr contains machine‐precision numbers, SetPrecision[expr,p] can give results that differ from one computer system to another.
- 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.
- SetPrecision[expr,p] does not modify expr itself.
Examples
open all close allBasic 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]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}]]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]SetPrecision[x ^ 1.25, 20]Special rules may apply to data objects:
ifun = Interpolation[{0, 0, 1, 1}]For an InterpolatingFunction object, 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]}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 - emThis 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
:
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
, 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]Properties & Relations (5)
SetPrecision just sets the precision of numbers while N works adaptively:
N[Sin[1000], 20]Since N works adaptively, the result has the requested precision of 20:
Precision[%]Use SetPrecision:
SetPrecision[Sin[1000], 20]The precision is less than 20 because of the conditioning of the sine function at 1000:
Precision[%]SetPrecision effectively evaluates Sin with argument 1000 to precision 20:
Sin[1000`20]N will typically not raise the precision of an expression, while SetPrecision will:
e = f[1.] + Pi;ne = N[e, 20]spe = SetPrecision[e, 20]{Precision[e], Precision[ne], Precision[spe]}For nonzero numbers
, SetPrecision[x,p] is equivalent to SetAccuracy[x,p-e]:
xl = Table[RandomReal[10 ^ RandomInteger[{-9, 9}], WorkingPrecision -> RandomReal[{15, 30}]], {4}]Map[SetPrecision[#, 20]&, xl]
is given by RealExponent:
Map[SetAccuracy[#, 20 - RealExponent[#]]&, xl]SetPrecision[x,∞] and Rationalize[x,0] give rational approximations for real x:
x = N[π]Rationalize[x,0] gives a rational that is equivalent to x up to the precision of x:
r = Rationalize[x, 0]r - xSetPrecision[x,∞] gets a rational directly from the bitwise representation of x:
SetPrecision[x, ∞]% === FromDigits[RealDigits[x, 2], 2]SetPrecision[x,∞] and RootApproximant[x] give exact approximations for real x:
x = N[E]RootApproximant[x] gives an algebraic number equivalent to x up to the precision of x:
r = RootApproximant[x]r - xSetPrecision[x,∞] gets a rational directly from the bitwise representation of x:
SetPrecision[x, ∞]Possible Issues (1)
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 SetAccuracy:
SetAccuracy[z, 22]The only exception to this is that when prec is MachinePrecision, you get a machine zero:
SetPrecision[z, MachinePrecision]See Also
N Precision Chop SetAccuracy $MinPrecision $NumberMarks PrecisionGoal Rationalize
Function Repository: DecimalRound
Tech Notes
Related Guides
History
Introduced in 1991 (2.0) | Updated in 2003 (5.0)
Text
Wolfram Research (1991), SetPrecision, Wolfram Language function, https://reference.wolfram.com/language/ref/SetPrecision.html (updated 2003).
CMS
Wolfram Language. 1991. "SetPrecision." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/SetPrecision.html.
APA
Wolfram Language. (1991). SetPrecision. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetPrecision.html
BibTeX
@misc{reference.wolfram_2026_setprecision, author="Wolfram Research", title="{SetPrecision}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/SetPrecision.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_setprecision, organization={Wolfram Research}, title={SetPrecision}, year={2003}, url={https://reference.wolfram.com/language/ref/SetPrecision.html}, note=[Accessed: 13-June-2026]}