SetPrecision[expr,p]
将 expr 中所有数设置为具有精确度 p.
SetPrecision
SetPrecision[expr,p]
将 expr 中所有数设置为具有精确度 p.
更多信息
- 当使用 SetPrecision 来提高一个数的精确度时,该数将用 0 填充. 0 采用以 2 为基. 在以 10 为基时,其它数位通常不是 0.
- 即便所得到的有效数位比 $MachinePrecision 小,SetPrecision 仍返回一个任意精度的数.
- SetPrecision[expr,MachinePrecision] 把所有在 expr 的数转变为机器精度.
- 如果有太大或太小的数来表示机器精度数,SetPrecision[expr,MachinePrecision] 将把他们转变为精度为 $MachinePrecision 的任意精度数.
- 当 expr 包含机器精度的数时,在不同计算机系统上 SetPrecision[expr,p] 可以给出不同的结果.
- SetPrecision 首先打开一个数的内部二进制表示中所有隐藏的特殊数位,且只有用完这些之后才添加尾数 0. »
- 在任何计算机系统上 0.004``25 生成一个所有末尾数字为 0,精度为 25 的数.
- SetPrecision[expr,p] 不修改 expr 本身.
范例
打开所有单元 关闭所有单元基本范例 (3)
范围 (5)
SetPrecision[Pi + I, 32]SetPrecision[f[.1, N[1 / 10, 30]], Infinity]TableForm[Table[{p, SetPrecision[.1, p]}, {p, 15, 65, 5}]]SetPrecision 不影响明确的幂:
p = (x + Pi) ^ (9 / 2)SetPrecision[p, 18]poly = 1. + 2. x + 3. x ^ 2 + 4. x ^ 3;SetPrecision[poly, 20]SetPrecision[x ^ 1.25, 20]ifun = Interpolation[{0, 0, 1, 1}]对于一个 InterpolatingFunction 对象,SetPrecision 仅改变适当的数据:
spifun = SetPrecision[ifun, 20]InputForm[spifun]{ifun[5 / 3], spifun[5 / 3]}应用 (4)
e = Hold[(Sin[1.00000000001] - Sin[1.0]) / (1.00000000001 - 1.0)]em = ReleaseHold[e]eb = ReleaseHold[SetPrecision[e, 32]]eb - emeb - Cos[1]e = N[1 / 10];ee = SetPrecision[e, Infinity]{error = ee - 1 / 10, N[error]}ep = SetPrecision[e(1 + $MachineEpsilon), Infinity];
em = SetPrecision[e(1 - $MachineEpsilon / 2), Infinity];N[{ep - 1 / 10, 1 / 10 - em}]Log[2, {ep - ee, ee - em}]duffing = {x''[t] + .15x'[t] - x[t] + x[t] ^ 3 == .3Cos[t], x[0] == 0, x'[0] == 0};dbig = SetPrecision[duffing, 32]mpsol = First[NDSolve[duffing, x, {t, 100}]]bigsol = First[NDSolve[dbig, x, {t, 100}, WorkingPrecision -> 32, MaxSteps -> Infinity]]Plot[Evaluate[x[t] /. {mpsol, bigsol}], {t, 0, 100}, PlotStyle -> {{Red}, {Green}}]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]ll = NestList[4#(1 - #)&, x0, 20]fl - NestList[4#(1 - #)&, 1 / 3, 20]RSolve[x[i + 1] == 4x[i](1 - x[i]), x, i]属性和关系 (5)
当选择相应的 N,SetPrecision 仅设置数的精度:
N[Sin[1000], 20]因为选择相应的 N,结果有所需要的 20 位准确度:
Precision[%]用 SetPrecision:
SetPrecision[Sin[1000], 20]Precision[%]SetPrecision 计算 Sin,其中参数 1000 精度为 20:
Sin[1000`20]N 一般不提高表达式的精度,但 SetPrecision 相反:
e = f[1.] + Pi;ne = N[e, 20]spe = SetPrecision[e, 20]{Precision[e], Precision[ne], Precision[spe]}对于非零数
,SetPrecision[x,p] 等价于 SetAccuracy[x,p-e]:
xl = Table[RandomReal[10 ^ RandomInteger[{-9, 9}], WorkingPrecision -> RandomReal[{15, 30}]], {4}]Map[SetPrecision[#, 20]&, xl]
由 RealExponent 给出:
Map[SetAccuracy[#, 20 - RealExponent[#]]&, xl]SetPrecision[x,∞] 和 Rationalize[x,0] 给出实数 x 的有理近似值:
x = N[π]Rationalize[x,0] 给出一个等价于 x 有理数,它有 x 的精度:
r = Rationalize[x, 0]r - xSetPrecision[x,∞] 从 x 的按位表示直接获得一个有理式:
SetPrecision[x, ∞]% === FromDigits[RealDigits[x, 2], 2]SetPrecision[x,∞] 和 RootApproximant[x] 给出关于实数 x 的明确近似值:
x = N[E]RootApproximant[x] 给出等价于 x 的代数数,它有 x 的精度:
r = RootApproximant[x]r - xSetPrecision[x,∞] 从 x 的按位表示直接获得一个有理式:
SetPrecision[x, ∞]可能存在的问题 (1)
SetPrecision[z,prec] 对于关于近似零 z 给出明确的零:
z = N[Pi, 20] - (21053343141/6701487259)SetPrecision[z, 20]如果您需要一个近似零,用 SetAccuracy:
SetAccuracy[z, 22]这对于 prec 是 MachinePrecision 例外,您获得一个机器零:
SetPrecision[z, MachinePrecision]技术笔记
-
▪
- 任意精度的数
相关指南
-
▪
- 精度和准确度控制
历史
1991年引入 (2.0) | 在以下年份被更新:2003 (5.0)
文本
Wolfram Research (1991),SetPrecision,Wolfram 语言函数,https://reference.wolfram.com/language/ref/SetPrecision.html (更新于 2003 年).
CMS
Wolfram 语言. 1991. "SetPrecision." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2003. https://reference.wolfram.com/language/ref/SetPrecision.html.
APA
Wolfram 语言. (1991). SetPrecision. Wolfram 语言与系统参考资料中心. 追溯自 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: 03-November-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: 03-November-2026]}