【问题标题】:How to compute an exponent in matlab without getting inf?如何在不获取inf的情况下在matlab中计算指数?
【发布时间】:2014-08-27 17:15:44
【问题描述】:

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

>> 100^1000

ans =

   Inf

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

【问题讨论】:

    标签: matlab numeric numerical


    【解决方案1】:

    正如丹尼尔已经指出的那样,这个数字太大了,甚至 MATLAB 本身都无法输出。这个数字是通过realmax 获得的,用于不同的数据类型。作为表示/使用如此巨大数字的替代方法,您可以使用相应的 mantissaexponentbase-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
    

    【讨论】:

      【解决方案2】:

      使用 64 位浮点运算,100^1000inf,因为它大于可能的最大值。如果符号数学工具箱可用,请使用 vpasym 处理大数:

      sym('100^1000')
      

      vpa('100^1000')
      

      【讨论】:

        【解决方案3】:

        为了处理大数还有High Precision Float这个类,用它可以写出结果

        a = hpf(100);
        b = hpf(1000);
        c = a^b;
        

        给出c = 1.e2000。对struct(c) 的调用可提供有关号码在内部存储方式的信息。

        struct(c)
        
        ans = 
        
        NumberOfDigits: [64 4]
           DecimalBase: 4
                  Base: 10000
               Numeric: 0
                  Sign: 1
              Exponent: 2001
                Migits: [1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-22
          • 1970-01-01
          • 1970-01-01
          • 2011-09-11
          • 2015-01-24
          • 1970-01-01
          • 2017-11-22
          相关资源
          最近更新 更多