gpt4 book ai didi

matlab - 大数运算

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:22 25 4
gpt4 key购买 nike

我有一些数字 a_i(i=1 到 10000)。我需要使用 matlab 计算 exp(a_i)/sum(exp(a_j))。

当然,不可能直接算出来。我发现了一些技巧,最有趣的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"

我试过类似的东西,但在可能的情况下结果是 Inf:

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

exp(a) = Inf

有人有想法吗?

最佳答案

您的答案在 FD 中。因为你的 a 比他们说的例子 a (即 e5 vs e2)大得多在 Matlab 的范围内,你的一定超出了范围,因此变成了 inf。但这并不重要,因为 DF 包含您的答案,您不应该根据 exp(a) 检查它, 该示例仅计算 exp(a) 以演示概念验证。但这段代码的全部意义在于为您提供一种查找巨型数字的 exp 的方法。

在你的情况下你得到

D =

307797

F =

3.374110424643062 % Use format long

因此你的答案是 3.374110424643062e+307797

关于matlab - 大数运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com