Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何让gamma()返回实际数字而不是Inf_R_Floating Point - Fatal编程技术网

R:如何让gamma()返回实际数字而不是Inf

R:如何让gamma()返回实际数字而不是Inf,r,floating-point,R,Floating Point,运行gamma(200)返回R中的Inf。是否可能让R以某种方式返回实际数字?它看起来像是伽马(171.6)上面的任何东西,在R中返回一个Inf。问题是不能用双精度表示它: gamma(200)#值太大 #R> [1]Inf lgamma(200)#但日志不是 #R> [1]857.9337 exp(857)#问题! #R> [1]Inf .Machine$double.xmax#最大双倍值 #R> [1]1.797693e+308 伽马(171)#快到了! #R> [1]7.257416e+3

运行
gamma(200)
返回R中的
Inf
。是否可能让R以某种方式返回实际数字?它看起来像是伽马(171.6)上面的任何东西,在R中返回一个
Inf

问题是不能用双精度表示它:

gamma(200)#值太大
#R> [1]Inf
lgamma(200)#但日志不是
#R> [1]857.9337
exp(857)#问题!
#R> [1]Inf
.Machine$double.xmax#最大双倍值
#R> [1]1.797693e+308
伽马(171)#快到了!
#R> [1]7.257416e+306
您可以使用gamma函数的日志,而不是使用
lgama
。否则,您将需要使用比R的浮点精度更高的第三方库

谷歌搜索表明,如果无法使用gamma函数的日志,则
Rmpfr::igamma
函数可能就是您想要的:

Rmpfr::igamma(171,0)
#R> 1“mpfr”精度为53位的数字
#R> [1]7.257415615307999e+306
Rmpfr::igamma(200,0)
#R> 1“mpfr”精度为53位的数字
#R> [1]3.9432893368239526e+372

使用Benjamin Cristofersen提出的lgamma,您可以计算有效位和指数(以10为基数)作为单个变量:

(res <- gamma(100))
9.332622e+155

# Natural logarithm of result
(ln_res <- lgamma(100))
359.1342

# log base 10 of result
(log10_res <- ln_res/log(10))
155.97

# decimal part of the number above, raised to the 10th power
(significand_res <- 10 ^ (log10_res %% 1))
9.332622

# non-decimal part
(exp_res <- log10_res %/% 1)
155

(res)这感觉有点像一个X-Y问题。大概
gamma
调用是一个更大计算的一部分?你能说更多关于上下文的信息吗?