MATLAB:涉及大量数字的计算 [英] MATLAB: computations involving large numbers

查看:64
本文介绍了MATLAB:涉及大量数字的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MATLAB中执行涉及大量数字的计算.举一个简单的例子,一个任意精度的计算器将显示((1/120)^ 132)*(370!)/(260!)约为1.56,但是MATLAB无法执行这样的计算(power(120,-132)*factorial(370)/factorial(260) = NaN).

How can one perform computations in MATLAB that involve large numbers. As a simple example, an arbitrary precision calculator would show that ((1/120)^132)*(370!)/(260!) is approximately 1.56, but MATLAB is not able to perform such a computation (power(120,-132)*factorial(370)/factorial(260) = NaN).

我也尝试了以下方法,但这不起作用:

I have also tried the following, which does not work:

syms a b c d;
a=120; b=-132; c=370; d=260;
f=sym('power(a,b)*gamma(c+1)/gamma(d+1)')
double(f); % produces error that instructs use of `vpa`
vpa(f) % produces (gamma(c + 1.0)*power(a, b))/gamma(d + 1.0)

推荐答案

如果只想计算一些大数的阶乘,则可以使用Java任意精度工具,如下所示:

If you just want to calculate the factorial of some large numbers, you can use the Java arbitrary precision tools, like so:

result = java.math.BigDecimal(1);
for ix = 1:300
    result = result.multiply(java.math.BigDecimal(ix));
end
disp(result)
306057512216440636035370461297268629388588804173576999416776741259476533176716867465515291422477573349939147888701726368864263907759003154226842927906974559841225476930271954604008012215776252176854255965356903506788725264321896264299365204576448830388909753943489625436053225980776521270822437639449120128678675368305712293681943649956460498166450227716500185176546469340112226034729724066333258583506870150169794168850353752137554910289126407157154830282284937952636580145235233156936482233436799254594095276820608062232812387383880817049600000000000000000000000000000000000000000000000000000000000000000000000000

在这种情况下,值result是一个Java对象.您可以在此处查看可用的方法: http://docs .oracle.com/javase/6/docs/api/java/math/BigDecimal.html

The value result in this case is a java object. You can see the available methods here: http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html

我仍然不确定我是否会相信(1e6)!的这种方法.您必须尝试一下才能看到.

I'm still not sure that I would trust this method for (1e6)! though. You'll have to experiment and see.

这篇关于MATLAB:涉及大量数字的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆