0

我似乎很难在 MATLAB 中累积然后输出一个变量。请看下面的代码。我要做的就是将十进制表示添加到总数中。我已经尝试过感觉像是一百万种东西。我想我误解了如何遍历 MATLAB 向量。看起来很简单。

%clear the command window
clc();
%clear all of the variable stored
clear all;
% close all plots and figures
close();

num = 1.32421875000000000000;
format long g;
%bin_num = dec2bin(num);
fprintf("binary representation of num:  %.45f \n", num);
num = num + (1/2^52);
fprintf("the next largest number is:  %.45f \n", num);
num = num - (1/2^52);
%bin = '0011111111110101001011111111111111111111111111111111111111111111'
sign = '0';
exponent = '0 1 1 1 1 1 1 1 1 1 1';
decexponent = bin2dec(exponent);
mantissa = [0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
for i = 1:1:length(mantissa)
     sumtotal = sumtotal + mantissa(i)*(1/(2^i)));
end
%num = (-1)^bin2dec(sign)*(2^(decexponent-1023))*(1 + sum);
fprintf("the next smallest number is:  %.45f \n", (sumtotal));
4

1 回答 1

0

有一些错误。我删除了错误并且它有效。输出如下

num 的二进制表示:1.324218750000000000000000000000000000000000000

下一个最大的数字是:1.324218750000000222044604925031308084726333618

下一个最小的数字是:0.3242187499999997777955395074968691915273666382

使用下面的代码,请告诉我你想这样做..

%clear the command window
clc();
%clear all of the variable stored
clear all;
% close all plots and figures
close();

num = 1.32421875000000000000;
format long g;
%bin_num = dec2bin(num);
fprintf("binary representation of num:  %.45f \n", num);
num = num + (1/2^52);
fprintf("the next largest number is:  %.45f \n", num);
num = num - (1/2^52);
%bin = '0011111111110101001011111111111111111111111111111111111111111111'
sign = '0';
exponent = '0 1 1 1 1 1 1 1 1 1 1';
decexponent = bin2dec(exponent);
sumtotal=0;
mantissa = [0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
for i = 1:1:length(mantissa)
     sumtotal = sumtotal + mantissa(i)*(1/(2^i));
end
%num = (-1)^bin2dec(sign)*(2^(decexponent-1023))*(1 + sum);
fprintf("the next smallest number is:  %.45f \n", (sumtotal));
于 2020-06-30T05:49:36.623 回答