如何在不获取inf的情况下在matlab中计算指数? [英] How to compute an exponent in matlab without getting inf?
问题描述
标题说明了一切:我想用大数字在matlab中计算一个指数,但是我溢出了,它只返回无穷大.
The title says it all: I want to calculate an exponent in matlab with big numbers, but I get overflow and it just returns infinity.
>> 100^1000
ans =
Inf
我上次检查时,100 ^ 1000绝对小于无穷大:)
Last time I checked, 100^1000 is decidedly smaller than infinity :)
推荐答案
正如丹尼尔(Daniel)所指出的那样,它太大了,甚至无法由MATLAB本身输出.此数字是通过 realmax
获得的,用于不同的数据类型.作为表示/使用如此大的数字的替代方法,您可以将相应的mantissa
和exponent
与base-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
这篇关于如何在不获取inf的情况下在matlab中计算指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!