MATLAB为什么删除我的小数? [英] Why did MATLAB delete my decimals?
问题描述
比方说,我创建了一个编号为10^4
的数字A
:
A = 81472.368639;
disp(A)
8.1472e+04
那不是我想要的.我的小数点在哪里?应该再多六个小数.检查变量编辑器显示以下信息:
再次,我输了我的小数.如何保存这些数据以进行进一步的计算?
科学计数法,或者为什么您不丢失任何小数位
您没有丢失任何小数,这只是MATLAB显示大数的方式. MATLAB使用科学计数法将命令窗口和变量编辑器中的数字显示四舍五入到点之前的一位数字,再到点之后的四位数字.科学表示法是Xe+y
表示法,其中X
是某个数字,而y
是整数.这意味着X
是y
的幂的10
倍,可以将其可视化为将点向右移动y
个位置"(如果y
为负,则向左移动).>
强制MATLAB显示所有小数位
现在我们知道MATLAB的作用,是否可以强制其向我们显示编号?当然,有多种选择,最简单的方法是设置更长的 format
.最常用于显示长号的是format long
和format longG
,当我们使用它们时,它们的区别显而易见:
format long
A
A =
8.1472368639e+04
format longG
A
A =
81472.368639
format long
使用科学计数法显示所有小数(最多16个),format longG
再次尝试显示不具有科学计数法但具有最多可用小数的数字:最多等于16位总共是点号之后.
更高级的解决方案是使用 disp(sprintf())
或 fprintf
,如果您想在小数点前,小数点后输入精确的小数位数,或两者兼有:
fprintf('A = %5.3f\n',A) % \n is just to force a line break
A = 81472.369
disp(sprintf('A = %5.2f\n',A))
A = 81472.37
最后,还记得变量编辑器吗?我们如何得到它来完全显示我们的变量?简单:点击包含数字的单元格:
之后的变量编辑器
因此,简而言之:在此过程中,我们没有丢失任何小数,MATLAB仍在内部存储它们,默认情况下,它只显示较少的小数.
format
的其他用途
format
还有一个不错的属性,您可以设置format compact
,它可以消除所有其他空行:
format compact
format long
A
A =
8.147236863931789e+04
format longG
A
A =
81472.3686393179
当您不想使命令窗口很大而又不想滚动太多时,我认为这非常方便.
format shortG
和format longG
在您的数组中具有非常不同的数字时很有用:
b = 10.^(-3:3);
A.*b
ans =
1.0e+07 *
0.0000 0.0001 0.0008 0.0081 0.0815 0.8147 8.1472
format longG
A.*b
ans =
Columns 1 through 3
81.472368639 814.72368639 8147.2368639
Columns 4 through 6
81472.368639 814723.68639 8147236.8639
Column 7
81472368.639
format shortG
A.*b
ans =
81.472 814.72 8147.2 81472 8.1472e+05 8.1472e+06 8.1472e+07
即它们在单个数字上的作用类似于long
和short
,但是为每个数字选择最方便的显示格式.
还有一些特殊的选项,例如shortE
,shortEng
,hex
等,但是您可以在 解决方案
Scientific notation, or why you didn't lose any decimals
You didn't lose any decimals, this is just MATLAB's way of displaying large numbers. MATLAB rounds the display of numbers, both in the command window and in the variable editor, to one digit before the dot and four after that, using scientific notation. Scientific notation is the Xe+y
notation, where X
is some number, and y
an integer. This means X
times 10
to the power of y
, which can be visualised as "shift the dot to the right for y
places" (or to the left if y
is negative).
Force MATLAB to show you all your decimals
Now that we know what MATLAB does, can we force it to show us our number? Of course, there're several options for that, the easiest is setting a longer format
. The most used for displaying long numbers are format long
and format longG
, whose difference is apparent when we use them:
format long
A
A =
8.1472368639e+04
format longG
A
A =
81472.368639
format long
displays all decimals (up to 16 total) using scientific notation, format longG
tries to display numbers without scientific notation but with most available decimals, again: as many as there are or up to 16 digits, both before and after the dot, in total.
A more fancy solution is using disp(sprintf())
or fprintf
if you want an exact number of decimals before the dot, after the dot, or both:
fprintf('A = %5.3f\n',A) % \n is just to force a line break
A = 81472.369
disp(sprintf('A = %5.2f\n',A))
A = 81472.37
Finally, remember the variable editor? How do we get that to show our variable completely? Simple: click on the cell containing the number:
So, in short: we didn't lose any decimals along the way, MATLAB still stores them internally, it just displays less decimals by default.
Other uses of format
format
has another nice property in that you can set format compact
, which gets rid of all the additional empty lines:
format compact
format long
A
A =
8.147236863931789e+04
format longG
A
A =
81472.3686393179
which in my opinion is very handy when you don't want to make your command window very big, but don't want to scroll a lot either.
format shortG
and format longG
are useful when your array has very different numbers in them:
b = 10.^(-3:3);
A.*b
ans =
1.0e+07 *
0.0000 0.0001 0.0008 0.0081 0.0815 0.8147 8.1472
format longG
A.*b
ans =
Columns 1 through 3
81.472368639 814.72368639 8147.2368639
Columns 4 through 6
81472.368639 814723.68639 8147236.8639
Column 7
81472368.639
format shortG
A.*b
ans =
81.472 814.72 8147.2 81472 8.1472e+05 8.1472e+06 8.1472e+07
i.e. they work like long
and short
on single numbers, but chooses the most convenient display format for each of the numbers.
There's a few more exotic options, like shortE
, shortEng
, hex
etc, but those you can find well documented in The MathWork's own documentation on format
.
这篇关于MATLAB为什么删除我的小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!