0
我正在研究一个MATLAB程序,并想知道如何得到我的返回值,当它显示为Inf
(对MATLAB来说太大)。我如何让MATLAB显示一个正在返回的Inf(Infinity)的数字?
% Question2
% Program is meant to calculate the product of all the odd numbers from 1 to 1000
% declare variable ‘product’ as zero
product = 1.;
% initialize counter, ‘n’, to 1000
n = 1000;
for i = 1:2:n
product = product * i;
end
fprintf('The product of all the odd numbers from 1 to %d is %d\n', n, product)
你可以使用没有循环的'prod(1:2:n)'。而且matlab对于任意的精度计算不是很好。 – Cheery 2014-11-06 04:04:19
那会摆脱inf并给我一个真正的整数? – 2014-11-06 04:06:59
不... ...'n'太大了! – rayryeng 2014-11-06 04:07:25