简体   繁体   English

Matlab双精度数字:变量编辑器与fprintf

[英]Matlab Double Precision Digits: Variable Editor vs. fprintf

Following this question , I was looking at the precision of double variables in Matlab. 这个问题之后 ,我正在研究Matlab中双变量的精度。 There, it is recommended to use fprintf to look at the variables more closely. 在那里,建议使用fprintf更密切地查看变量。

The strange thing is that the Variable Editor and fprintf show different results, fprintf shows one digit more. 奇怪的是,变量编辑器和fprintf显示不同的结果,fprintf显示更多的一位数。

% pi
Variable Editor:       3.141592653589793
fprintf('0.16f\n',pi): 3.1415926535897931
vpa('pi'):             3.1415926535897932384626433832795

% pi / 180
pi180 = pi/180
Variable Editor (pi180)      0.017453292519943
fprintf('%0.16f\n',pi180)    0.0174532925199433
vpa('pi/180')                0.017453292519943295769236907684886

Internally, Matlab seems to be working with the precision which is printed by fprintf 在内部,Matlab似乎正在使用fprintf打印的精度

>> fprintf('%0.16f\n',0.0174532925199433*10) % value from fprintf
0.1745329251994330
>> fprintf('%0.16f\n',0.017453292519943*10)  % value from Variable Editor
0.1745329251994300
>> fprintf('%0.16f\n',pi180*10)              % internal calculation
0.1745329251994330

Why is that so? 为什么会这样?

If I use the precalculated pi/180 in a function, should I use the value from fprintf or from the Variable Editor? 如果我在函数中使用预先计算的pi/180 ,我应该使用fprintf中的值还是变量编辑器中的值?

tl;dr In the Variable Editor, Matlab is truncating at 15 digits instead of 16. tl; dr在变量编辑器中,Matlab截断为15位而不是16位。

tl;dr Variables in MATLAB use double precision. tl; dr MATLAB中的变量使用双精度。 The same precision is used for variables in both the Command Window and the Variable Editor. 命令窗口和变量编辑器中的变量使用相同的精度。 The only difference is the display format and the number of decimal places of accuracy you print it to. 唯一的区别是显示格式和打印精度的小数位数。


MATLAB by default uses double precision for its variables. 默认情况下,MATLAB对其变量使用双精度。 These variables in MATLAB are stored in the Workspace. MATLAB中的这些变量存储在Workspace中。 Both the Command Window and the Variable Editor pull their variables from the same Workspace and hence underneath use the same precision. 命令窗口和变量编辑器都从同一个工作空间中提取变量,因此在下面使用相同的精度。

The default format for displaying variables in the Variable Editor and the Command Window is the short format. 在变量编辑器和命令窗口中显示变量的默认格式是short格式。 You can check this for the Command Window with 您可以使用命令窗口检查此项

>> get(0, 'format')
ans =
   short

and for the Variable Editor by going to Preferences -> Variables -> Format . 对于变量编辑器,转到Preferences -> Variables -> Format The short format by default displays variables to 4 decimal places of accuracy. 默认情况下, short格式将变量显示为4位小数的精度。

It appears to me that you have changed the default display format for variables in your Variable Editor to the long format. 在我看来,您已将变量编辑器中变量的默认显示格式更改为long格式。 This format displays double variables with 15 decimal places of accuracy. 此格式显示具有15位小数精度的双变量。 Both the long and short formats round variables, so pi which is 3.14159 gets rounded to 3.1416 because of the 9 in the 5th decimal place when displayed with the short format long格式和short格式都是圆形变量,所以pi3.14159会四舍五入为3.1416因为当以short格式显示时,小数点后5位为9

>> format short
>> pi
ans =
    3.1416

This is directly equivalent to the output produced by fprintf 这直接相当于fprintf产生的输出

>> fprintf(1, '%.4f\n', pi);
3.1416

However, the long format, which I'm guessing, you've set as the default for your Variable Editor rounds to 15 decimal places and so displays 但是,我猜的long格式,你已经设置为变量编辑器的默认值,舍入到15位小数,因此显示

>> format long
>> pi
ans =
   3.141592653589793

which is directly equivalent to 这直接相当于

>> fprintf(1, '%.15f\n', pi);
3.141592653589793

When you use fprintf(1, '%.16f\\n', pi); 当你使用fprintf(1, '%.16f\\n', pi); you are printing pi to 16 decimal places and not 15 as specified by the long format. 您将pi打印到16位小数,而不是 long格式指定的15。 This is why your output is 这就是你输出的原因

>> fprintf(1, '%.16f\n', pi);
3.1415926535897931

Note, the 1 at the end of this. 注意,这个结尾的1 This is why the 3 directly preceding it isn't rounded to 4 when displayed in your Variable Editor. 这就是为什么在变量编辑器中显示时,它前面的3不会四舍五入为4

Summary 摘要

  • Variables in MATLAB by default use double precision MATLAB中的变量默认使用双精度
  • MATLAB variables are stored in the Workspace MATLAB变量存储在Workspace中
  • Variables available in the Command Window and the Variable Editor both come from the Workspace and use the same precision 命令窗口和变量编辑器中可用的变量都来自工作区并使用相同的精度

Precalculated Values 预先计算的值

In MATLAB you should use the variable name pi180 in function calls or when manipulating other numeric data. 在MATLAB中,您应该在函数调用或操作其他数字数据时使用变量名pi180 This will use double precision and eliminate any copy and paste errors that may arise by using values output by fprintf or in the Variable Editor. 这将使用双精度并消除使用fprintf输出的值或在变量编辑器中可能出现的任何复制和粘贴错误。

fprintf Quirks fprintf怪癖

tl;dr MATLAB's short and long formats switch between the %d , %.f , %.g and %.e specifiers depending on the most appropriate method for the input. tl; dr MATLAB的short格式和long格式在%d%d%.f%.g%.e说明符之间切换,具体取决于最合适的输入方法。


@ horchler pointed out that %.f is only directly equivalent to the short and long formats for specific inputs and this is true. @ horchler指出%.f仅直接等同于特定输入的short格式和long格式,这是事实。 There is no direct equivalent for all inputs between fprintf and MATLAB's short and long formats. fprintf和MATLAB的short格式和long格式之间的所有输入都没有直接的等价物。

For instance let's look at eps and 100.5 and try to print the numbers exactly like MATLAB's short and long formats. 例如,让我们看看eps100.5并尝试打印数字,就像MATLAB的short格式和long格式一样。

>> format short
>> eps
ans =
   2.2204e-16
>> 100.5
ans =
  100.5000

and

>> format long
>> eps
ans =
     2.220446049250313e-16
>> 100.5
ans =
     1.005000000000000e+02

Now we know from above that fprintf(1, '%.4f\\n', pi); 现在我们从上面知道fprintf(1, '%.4f\\n', pi); and fprintf(1, '%.15f\\n', pi); fprintf(1, '%.15f\\n', pi); are directly equivalent to short and long respectively for pi but are they for eps and 100.5 对于pi ,它们分别直接相当于shortlong ,但它们对于eps100.5

>> fprintf(1, '%.4f\n', eps);
0.0000
>> fprintf(1, '%.15f\n', eps);
0.000000000000000
>> fprintf(1, '%.4f\n', 100.5);
100.5000
>> fprintf(1, '%.15f\n', 100.5);
100.500000000000000

No they aren't the only direct equivalent is fprintf(1, '%.4f\\n', 100.5); 不,它们不是唯一的直接等价物是fprintf(1, '%.4f\\n', 100.5); . What if we try with %.g ? 如果我们尝试%.g怎么办?

>> fprintf(1, '%.4g\n', eps);
2.22e-16
>> fprintf(1, '%.15g\n', eps);
2.22044604925031e-16
>> fprintf(1, '%.4g\n', 100.5);
100.5
>> fprintf(1, '%.15g\n', 100.5);
100.5

Now none of the fprintf statements are directly equivalent. 现在没有fprintf语句直接等效。 However, we can produce a direct equivalent for eps using the long format with 但是,我们可以使用long格式生成eps的直接等效项

>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16

because the number directly following the . 因为数字直接跟着. for the %g format specifier specifies the number of significant digits (including those preceding the decimal point, . ) we need to use 16 and not 15 . 对于%g格式说明符指定有效位数(包括小数点前面的位数, . ),我们需要使用16而不是15

To produce a direct equivalent for all of these input types for the short format we need to mix the %.f , %.g and %.e specifiers as well as adjusting the field width . 要为short格式生成所有这些输入类型的直接等效项,我们需要混合%.f%.g%.e说明符以及调整字段宽度

>> format short
>> pi
ans =
    3.1416
>> eps
ans =
   2.2204e-16
>> 100.5
ans =
  100.5000
>> fprintf(1, '%.4f\n', pi);
3.1416
>> fprintf(1, '%.5g\n', eps);
2.2204e-16
>> fprintf(1, '%.4f\n', 100.5);
100.5000

Not trivial at all. 根本不是微不足道的。 The same can be done for the long format. 对于long格式也可以这样做。

>> format long
>> pi
ans =
   3.141592653589793
>> eps
ans =
     2.220446049250313e-16
>> 100.5
ans =
     1.005000000000000e+02
>> fprintf(1, '%.15f\n', pi);
3.141592653589793
>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16
>> fprintf(1, '%.15e\n', 100.5);
1.005000000000000e+02

Even worse than for the short format. short格式更糟糕。

So in short, MATLAB's short and long formats switch between the %d , %.f , %.g and %.e specifiers depending on the most appropriate method for the input. 简而言之,MATLAB的short格式和long格式在%d%d%.f%.g%.e说明符之间切换,具体取决于最合适的输入方法。

Additional Reading 补充阅读

You can find information on the different display formats available in MATLAB through the format documentation . 您可以通过format文档找到有关MATLAB中可用的不同显示格式的信息。 There is also a helpful document on Display Format for Numeric Values . 还有一个关于数值显示格式的有用文档。 And lastly, there is information about the Variable Editor and its preferences . 最后,有关于变量编辑器及其首选项的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM