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 arbitraryprecision 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 machineprecision 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 allclose all

Basic Examples  (3)

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

Convert all numbers to machine precision:

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

Scope  (5)

Set precision of a complex number:

Convert approximate numbers to exact rational numbers:

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

SetPrecision does not affect exact powers:

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

Inexact powers are modified:

Special rules may apply to data objects:

For an InterpolatingFunction object, SetPrecision changes the appropriate data only:

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

Applications  (4)

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

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

Find the representation error of a machine number:

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

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

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

Find the solutions computed at machine precision and precision 32:

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

Override the default accuracy and precision model:

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

Nonetheless, all digits given are correct:

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

Properties & Relations  (5)

SetPrecision just sets the precision of numbers while N works adaptively:

Since N works adaptively, the result has the requested precision of 20:

Use SetPrecision:

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

SetPrecision effectively evaluates Sin with argument 1000 to precision 20:

N will typically not raise the precision of an expression, while SetPrecision will:

For nonzero numbers , SetPrecision[x,p] is equivalent to SetAccuracy[x,p-e]:

is given by RealExponent:

SetPrecision[x,] and Rationalize[x,0] give rational approximations for real x:

Rationalize[x,0] gives a rational that is equivalent to x up to the precision of x:

SetPrecision[x,] gets a rational directly from the bitwise representation of x:

SetPrecision[x,] and RootApproximant[x] give exact approximations for real x:

RootApproximant[x] gives an algebraic number equivalent to x up to the precision of x:

SetPrecision[x,] gets a rational directly from the bitwise representation of x:

Possible Issues  (1)

SetPrecision[z,prec] will make an exact zero for approximate zeros z:

If you want an approximate zero, use SetAccuracy:

The only exception to this is that when prec is MachinePrecision, you get a machine zero:

Introduced in 1991
 (2.0)
 |
Updated in 2003
 (5.0)