为什么MATLAB删除了我的小数?

时间:2018-03-09 10:23:19

标签: matlab floating-point numbers number-formatting

我们假设我创建了一个A的订单号10^4

A = 81472.368639;
disp(A)
   8.1472e+04

那不是我想要的。我的小数位在哪里?应该有六位小数。检查变量编辑器会向我显示:

Variable editor

再次,我丢失了小数。如何保留这些以供进一步计算?

1 个答案:

答案 0 :(得分:12)

科学记数法,或者为什么你没有丢失任何小数

你没有丢失任何小数,这只是MATLAB显示大数字的方式。 MATLAB使用科学计数法将命令窗口和变量编辑器中数字的显示四舍五入到点之前的一位数和之后的四位数。科学记数法是Xe+y符号,其中X是某个数字,y是整数。这意味着X10 y y的力量,可以将其显示为"将点向右移动y个地方" (如果format long为负数,则向左)。

强制MATLAB显示所有小数

既然我们知道MATLAB的作用,我们可以强制它向我们展示我们的号码吗?当然,有几种选择,最简单的是设置更长的format。最常用于显示长数字的是format longGformat long A A = 8.1472368639e+04 format longG A A = 81472.368639 ,当我们使用它们时,它们的区别很明显:

format long

format longG使用科学记数法显示所有小数(总共最多16个),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 尝试显示没有科学记数法的数字但显示大多数可用的小数,再次:多达16个或多达16个总数之前和之后的数字。

如果您想在点之前,点之后或两者之间使用精确的小数位数,则使用disp(sprintf())fprintf这是一个更奇特的解决方案:

format

最后,还记得变量编辑器吗?我们如何才能完全显示变量?简单:单击包含数字的单元格:

Variable editor after clicking

因此,简而言之:我们在此过程中没有丢失任何小数,MATLAB仍在内部存储它们,默认情况下它只显示较少的小数。

format

的其他用途

format compact有另一个不错的属性,你可以设置format compact format long A A = 8.147236863931789e+04 format longG A A = 81472.3686393179 ,它可以摆脱所有额外的空行:

format shortG

在我看来,当你不想让你的命令窗口变得非常大,但不想滚动很多时,这是非常方便的。

当您的数组中包含非常不同的数字时,

format longGb = 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

即。它们在单个数字上的作用类似于shortshortE,但为每个数字选择最方便的显示格式。

还有一些更具异国情调的选项,例如shortEnghex import re def replace_entities(example): # dd mm yyyy example = re.sub("(\d{1,31}(:? |\-|\/)\d{1,12}(:? |\-|\/)\d{4})", "DATESTR", example) # dd/mm/yyyy example = re.sub("(\d{4}(:? |\-|\/)\d{1,31}(:? |\-|\/)\d{1,12})", "DATESTR", example) # yyyy/dd/mm # email id example = re.sub("[\w\.-]+@[\w\.-]+", "EMAILIDSTR", example) # URL example = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', "URLSTR", example) example = re.sub('www.(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', "URLSTR", example) # TIME example = re.sub("\d{2}:\d{2} (:?AM|PM|am|pm)", "TIMESTR", example) example = re.sub("\d{2}:\d{2}:\d{3} (:?AM|PM|am|pm)", "TIMESTR", example) # MONEY example = re.sub(r'\£ \d+', "MONEYSTR", example, 0) example = re.sub(r'\£\d+', "MONEYSTR", example, 0) example = re.sub(r'\d+(:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0) example = re.sub(r'\d+ (:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0) example = re.sub(r'\d.\d+(:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0) example = re.sub(r'\d.\d+ (:?\£|pound|pounds|euros|euro)', "MONEYSTR", example, 0) example = re.sub(r'\\xc2\\xa\d+', "MONEYSTR", example, 0) example = re.sub(r'\\xc2\\xa\d+.\d+', "MONEYSTR", example, 0) # Split alpha numeric and sp. symbol example = " ".join(re.findall(r"[^,.:;\(\)\/\\_]+|[,.:;\(\)\/\\_]", example)) example = " ".join(re.findall(r"[^\d_]+|\d+", example)) example = re.sub('(?!^)([A-Z][a-z]+)', r' \1', example) # NUMBERS example = re.sub(r'\d+', 'NUMSTR', example) return example 等,但您可以在The MathWork's own documentation on format中详细记录这些选项。