Skip to content

SetPrecision

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

SetPrecision[x, n]
    Returns an expression equivalent to x with numeric values
    re-rounded or promoted to n decimal digits of precision.
    Requires a USE_MPFR build for n > MachinePrecision.

Examples

No verified examples yet for this function.

Implementation notes

builtin_set_precision is a two-argument wrapper: it parses the precision argument into a NumericSpec via parse_prec_arg (accepting an integer/real digit count or MachinePrecision) and drives numericalize(value, spec) — the same engine N uses — to re-represent the value to the requested number of significant digits (an EXPR_MPFR at the corresponding bit width when MPFR is built, otherwise machine double). Returns NULL if the precision argument is not a valid spec.

Attributes: Listable, Protected.

Implementation status

Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

References

Notes & additional examples

Worked examples

In[1]:= SetPrecision[1.5, 30]
Out[1]= 1.5

In[2]:= Precision[SetPrecision[1.5, 30]]
Out[2]= 30.103

Applied to an exact constant, SetPrecision produces a high-precision numeric value — here 50 correct digits of Pi:

In[1]:= SetPrecision[Pi, 50]
Out[1]= 3.1415926535897932384626433832795028841971693993751

It also re-rounds an exact rational to a chosen precision; note the trailing digit shows where the finite expansion is cut off:

In[1]:= SetPrecision[1/3, 40]
Out[1]= 0.33333333333333333333333333333333333333332

Notes

SetPrecision[x, n] returns a value equal to x but carrying n digits of precision; the printed form may look unchanged while the internal precision is raised (confirm with Precision). Padding extra digits onto a machine-precision number introduces meaningless trailing bits, so only widen precision when the original value genuinely has them.