matlab双精度混淆
matlab double precision confusion
Matlab 默认将输入设置为双精度,所以如果我输入 a=1/3
那么变量将以双精度转换
>> a=1/3
a =0.3333
>> whos('a')
a 1x1 8 double
但是,当我之后输入 vpa(a,100)
时,我得到:
ans=0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
我的问题是
既然a
是双精度的,只有8个字节来存储数据,为什么vpa(a,100)
可以达到100位的精度呢?一句话,Matlab如何将a
的精度从'double'提高到100位?
这在 vpa
的 doc
页面中有解释,这里是 link.
vpa Restores Precision of Common Double-Precision Inputs
Unlike exact symbolic values, double-precision values inherently
contain round-off errors. When you call vpa on a double-precision
input, vpa cannot restore the lost precision, even though it returns
more digits than the double-precision value. However, vpa can
recognize and restore the precision of expressions of the form p/q,
pπ/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.
First, demonstrate that vpa cannot restore precision for a
double-precision input. Call vpa on a double-precision result and the
same symbolic result.
所以答案是,Matlab 足够聪明,可以恢复你的表达式,因为它的形式是 p/q
。
Matlab 默认将输入设置为双精度,所以如果我输入 a=1/3
那么变量将以双精度转换
>> a=1/3
a =0.3333
>> whos('a')
a 1x1 8 double
但是,当我之后输入 vpa(a,100)
时,我得到:
ans=0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
我的问题是
既然a
是双精度的,只有8个字节来存储数据,为什么vpa(a,100)
可以达到100位的精度呢?一句话,Matlab如何将a
的精度从'double'提高到100位?
这在 vpa
的 doc
页面中有解释,这里是 link.
vpa Restores Precision of Common Double-Precision Inputs
Unlike exact symbolic values, double-precision values inherently contain round-off errors. When you call vpa on a double-precision input, vpa cannot restore the lost precision, even though it returns more digits than the double-precision value. However, vpa can recognize and restore the precision of expressions of the form p/q, pπ/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.
First, demonstrate that vpa cannot restore precision for a double-precision input. Call vpa on a double-precision result and the same symbolic result.
所以答案是,Matlab 足够聪明,可以恢复你的表达式,因为它的形式是 p/q
。