4

I have some numbers a_i (for i=1 to 10000). I need to compute exp(a_i)/sum(exp(a_j)) using matlab.

Of course, it is impossible to calculate straight away. I found some tricks, the most interesting being:

"Suppose we want to find exp(7.0873e002). This will be a large number indeed but still just barely within matlab's capability of direct calculation. However, we can find the separate exponent and mantissa without calling on 'exp' as follows;

 a = 7.0873e2;
 x = a/log(10);
 D = floor(x); % D will be an integer
 F = 10^(x-D); % F will lie in 1 <= F < 10

Then D will be the power of ten and F the mantissa

 F = 6.27376373225551 % The mantissa
 D = 307 % The exponent (power of ten)

Compare that with the direct answer:

 exp(a) = 6.273763732256170e+307"

I tried something similar, but the result in may case is Inf:

 a = 7.0873e5;
 x = a/log(10);
 D = floor(x);
 F = 10^(x-D);

 exp(a) = Inf

Anyone has an idea?

4

1 回答 1

3

您的答案在F和中D。因为你a的比他们声明的示例a(即e5vs e2)大得多,它们几乎不在 Matlab 的范围内,所以你的必须远远超出范围,因此变为inf. 但这没关系,因为DF保留您的答案,您不应该检查它exp(a),该示例仅计算exp(a)以证明概念证明。但这段代码的全部意义在于为您提供一种查找exp巨数的方法。

在你的情况下,你得到

D =

      307797

F =

    3.374110424643062 % Use format long

因此你的答案是3.374110424643062e+307797

于 2013-07-18T09:50:06.630 回答