Matlab双精度数字:变量编辑器与fprintf [英] Matlab Double Precision Digits: Variable Editor vs. fprintf

查看:142
本文介绍了Matlab双精度数字:变量编辑器与fprintf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题之后,我一直在寻找在Matlab中使用双变量的精度.在那里,建议使用fprintf更仔细地查看变量.

Following this question, I was looking at the precision of double variables in Matlab. There, it is recommended to use fprintf to look at the variables more closely.

奇怪的是,变量编辑器和fprintf显示的结果不同,fprintf显示的数字多一位.

The strange thing is that the Variable Editor and fprintf show different results, fprintf shows one digit more.

% 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

在内部,Matlab似乎在以fprintf打印的精度工作

Internally, Matlab seems to be working with the precision which is printed by 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

为什么会这样?

如果我在函数中使用预先计算的pi/180,应该使用fprintf还是变量编辑器中的值?

If I use the precalculated pi/180 in a function, should I use the value from fprintf or from the Variable Editor?

tl; dr 在变量编辑器中,Matlab截断了15位数字而不是16位数字.

tl;dr In the Variable Editor, Matlab is truncating at 15 digits instead of 16.

推荐答案

tl; dr MATLAB中的变量使用双精度.命令窗口和变量编辑器中的变量使用相同的精度.唯一的区别是显示格式和打印精度的小数位数.

tl;dr Variables in MATLAB use double precision. 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的变量默认使用双精度. MATLAB中的这些变量存储在工作区中. 命令窗口"和变量编辑器"都从相同的工作区中提取变量,因此在下面使用相同的精度.

MATLAB by default uses double precision for its variables. These variables in MATLAB are stored in the Workspace. Both the Command Window and the Variable Editor pull their variables from the same Workspace and hence underneath use the same precision.

在变量编辑器"和命令窗口"中显示变量的默认格式为short格式.您可以使用

The default format for displaying variables in the Variable Editor and the Command Window is the short format. You can check this for the Command Window with

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

,然后转到Preferences -> Variables -> Format进入变量编辑器.默认情况下,short格式将变量显示为小数点后4位.

and for the Variable Editor by going to Preferences -> Variables -> Format. The short format by default displays variables to 4 decimal places of accuracy.

在我看来,您已将变量编辑器中变量的默认显示格式更改为long格式.此格式显示精度为小数点后15位的双精度变量. longshort格式都对变量进行舍入,因此pi(3.14159)被舍入为3.1416,因为当9short格式显示时,它位于小数点后第五位.

It appears to me that you have changed the default display format for variables in your Variable Editor to the long format. This format displays double variables with 15 decimal places of accuracy. 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

>> format short
>> pi
ans =
    3.1416

这直接等同于fprintf

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

但是,我猜是long格式,您已将变量编辑器的默认格式设置为四舍五入到15位小数,因此显示

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

>> format long
>> pi
ans =
   3.141592653589793

直接等同于

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

使用fprintf(1, '%.16f\n', pi);时,将pi打印到小数点后16位,而不打印不是 15位(按long格式指定).这就是为什么您的输出是

When you use fprintf(1, '%.16f\n', pi); you are printing pi to 16 decimal places and not 15 as specified by the long format. This is why your output is

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

注意,此末尾的1.这就是为什么在变量编辑器中显示时,紧靠其前的3不能四舍五入到4的原因.

Note, the 1 at the end of this. This is why the 3 directly preceding it isn't rounded to 4 when displayed in your Variable Editor.

  • 默认情况下,MATLAB中的变量使用双精度
  • MATLAB变量存储在工作区中
  • 命令窗口和变量编辑器中可用的变量都来自工作区,并且使用相同的精度

在MATLAB中,应在函数调用中或在处理其他数字数据时使用变量名称pi180.这将使用双精度,并消除使用fprintf或在变量编辑器中输出的值可能引起的任何复制和粘贴错误.

In MATLAB you should use the variable name pi180 in function calls or when manipulating other numeric data. 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.

tl; dr MATLAB的shortlong格式根据最合适的输入方法在%d%.f%.g%.e说明符之间切换

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.

@ horchler 指出,%.f仅直接等同于shortlong格式,具体的输入,这是事实. fprintf与MATLAB的shortlong格式之间的所有输入没有没有直接等效.

@horchler pointed out that %.f is only directly equivalent to the short and long formats for specific inputs and this is true. There is no direct equivalent for all inputs between fprintf and MATLAB's short and long formats.

例如,让我们看一下eps100.5,并尝试打印与MATLAB的shortlong格式完全相同的数字.

For instance let's look at eps and 100.5 and try to print the numbers exactly like MATLAB's short and long formats.

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

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

现在我们从上方知道pifprintf(1, '%.4f\n', pi);fprintf(1, '%.15f\n', pi);分别直接与shortlong等效,但是对于eps100.5

Now we know from above that fprintf(1, '%.4f\n', pi); and fprintf(1, '%.15f\n', pi); are directly equivalent to short and long respectively for pi but are they for eps and 100.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

,它们不是唯一的直接等效项是fprintf(1, '%.4f\n', 100.5);.如果我们尝试使用%.g怎么办?

No they aren't the only direct equivalent is fprintf(1, '%.4f\n', 100.5);. What if we try with %.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

现在,所有fprintf语句都没有直接等价的内容.但是,我们可以将long格式与一起直接生成eps的等价物.

Now none of the fprintf statements are directly equivalent. However, we can produce a direct equivalent for eps using the long format with

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

因为%g格式说明符在.之后的数字指定了有效位数(包括小数点.之前的位数),我们需要使用 16 和不是 15 .

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.

要为short格式的所有这些输入类型产生直接等效的内容,我们需要混合%.f%.g %.e 说明符并调整字段宽度.

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.

>> 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

一点都不琐碎.对于long格式也可以这样做.

Not trivial at all. The same can be done for the long format.

>> 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

short格式还要糟糕.

因此,简而言之,MATLAB的shortlong格式会根据最合适的输入方法在%d%.f%.g%.e说明符之间切换.

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.

您可以通过 文档.在数字显示格式中也有一个有用的文档值.最后,还有关于变量编辑器和其首选项.

You can find information on the different display formats available in MATLAB through the format documentation. There is also a helpful document on Display Format for Numeric Values. And lastly, there is information about the Variable Editor and its preferences.

这篇关于Matlab双精度数字:变量编辑器与fprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆