且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-13 23:10:57

正如丹尼尔(Daniel)所指出的那样,它太大了,甚至无法由MATLAB本身输出.此数字是通过 realmax 获得的,用于不同的数据类型.作为表示/使用如此大的数字的替代方法,您可以将相应的mantissaexponentbase-10 representation一起使用,这是通常的MATLAB表示形式.这里列出了获得这两个功能的函数-

As Daniel has already pointed out, it's too big a number to be even outputted by MATLAB itself. This number is obtained with realmax for different datatypes. As an alternative to represent/use such huge numbers, you can use the corresponding mantissa and exponent with base-10 representation instead, which is the usual MATLAB representation. The function to get those two is listed here -

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运行进行了比较以进行验证.

Few example runs and comparisons with actual MATLAB runs for validation are listed next.

Ex#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