gpt4 book ai didi

matlab - 如何在不获取 inf 的情况下计算 matlab 中的指数?

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

标题说明了一切:我想在 matlab 中计算大数的指数,但我溢出了,它只返回无穷大。

>> 100^1000

ans =

Inf

我上次检查时,100^1000 明显小于无穷大 :)

最佳答案

正如 Daniel 已经指出的,这个数字太大了,MATLAB 本身无法输出。此号码是通过 realmax 获得的对于不同的数据类型。作为表示/使用如此庞大的数字的替代方法,您可以使用相应的 mantissaexponent 以及 base-10 representation 代替,这是通常的 MATLAB 表示。获取这两个的函数在此处列出 -

function [mantissa, base10_exponent] = base10_mantissa_exponent(base,exponent)

act_exp = exponent*log10(abs(base));
base10_exponent = floor(act_exp);
mantissa = power(10,act_exp - base10_exponent);

if rem(exponent,2)==1 && sign(base)==-1
mantissa = -mantissa;
end

return;

接下来列出了几个示例运行以及与实际 MATLAB 运行的比较以进行验证。

例 #1

base = -125.343;
exponent = 101;
usual_approach = base^exponent
[mantissa, base10_exponent] = base10_mantissa_exponent(base,exponent)

输出-

usual_approach =
-8.0930e+211
mantissa =
-8.0930
base10_exponent =
211

Ex #2(问题中讨论的问题)

base = 100;
exponent = 1000;

输出-

usual_approach =
Inf
mantissa =
1
base10_exponent =
2000

关于matlab - 如何在不获取 inf 的情况下计算 matlab 中的指数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533237/

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