gpt4 book ai didi

r - 使用具有 gmp 和机器限制的大整数

转载 作者:行者123 更新时间:2023-12-04 09:48:31 25 4
gpt4 key购买 nike

我想知道是否可以使用大于 .Machine$double.xmax 的值的整数( ~1.79e308 ) 在 R. 我认为通过使用例如Rmpfrgmp R 中的库您可以分配任何大小的值,直到系统上的 RAM 限制?我认为这大于 .Machine$double.xmax但显然不是。

> require( gmp )
> as.bigz( .Machine$double.xmax )
Big Integer ('bigz') :
[1] 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
> as.bigz( 1e309 )
Big Integer ('bigz') :
[1] NA
>

有人可以解释为什么使用 64 位内存寻址的计算机不能存储大于 1.79e308 的值吗?抱歉 - 我没有计算机科学背景,但我正在努力学习。

谢谢。

最佳答案

Rmpfr 可以使用 mpfr_set_str 进行字符串转换...

val <- mpfr("1e309")

## 1 'mpfr' number of precision 17 bits
## [1] 9.999997e308

# set a precision (assume base 10)...
est_prec <- function(e) floor( e/log10(2) ) + 1

val <- mpfr("1e309", est_prec(309) )

## 1 'mpfr' number of precision 1027 bits
## [1]1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

.mpfr2bigz(val)

## Big Integer ('bigz') :
## [1] 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

# extract exponent from a scientific notation string
get_exp <- function( sci ) as.numeric( gsub("^.*e",'', sci) )

# Put it together
sci2bigz <- function( str ) {
.mpfr2bigz( mpfr( str, est_prec( get_exp( str ) ) ) )
}

val <- sci2bigz( paste0( format( Const("pi", 1027) ), "e309") )

identical( val, .mpfr2bigz( Const("pi",1027)*mpfr(10,1027)^309 ) )

## [1] TRUE

## Big Integer ('bigz') :
## [1] 3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587004

至于为什么要存储大于 .Machine$double.xmax 的数字,IEEE 规范中有关浮点编码的文档、R 常见问题解答和维基百科都包含在所有行话中,但我发现仅定义这些术语(使用 ?'.Machine' )很有帮助。 ..
double.xmax(最大归一化浮点数)= (1 - double.neg.eps) * double.base ^ double.max.exp 在哪里
  • double.neg.eps(一个小的正浮点数 x 使得 1 - x != 1)= double.base ^ double.neg.ulp.digits 其中
  • double.neg.ulp.digits = 满足 1 - double.base ^ i != 1
  • 的最大负整数
  • double.max.exp = 溢出的 double.base 的最小正幂和
  • double.base(浮点表示的基数)= 2(二进制)。

  • 考虑哪些有限浮点数可以与另一个浮点数区分开来; IEEE 规范告诉我们,对于 binary64 数,指数使用 11 位,因此我们有 2^(11-1)-1=1023 的最大指数,但我们想要溢出的最大指数,因此 double.max.exp 是 1024。
    # Maximum number of representations
    # double.base ^ double.max.exp
    base <- mpfr(2, 2048)
    max.exp <- mpfr( 1024, 2048 )

    # This is where the big part of the 1.79... comes from
    base^max.exp

    ## 1 'mpfr' number of precision 2048 bits
    ## [1] 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

    # Smallest definitive unit.
    # Find the largest negative integer...
    neg.ulp.digits <- -64; while( ( 1 - 2^neg.ulp.digits ) == 1 )
    neg.ulp.digits <<- neg.ulp.digits + 1

    neg.ulp.digits

    ## [1] -53

    # It makes a real small number...
    neg.eps <- base^neg.ulp.digits

    neg.eps

    ## 1 'mpfr' number of precision 2048 bits
    ## [1] 1.11022302462515654042363166809082031250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-16

    # Largest difinitive floating point number less than 1
    # times the number of representations
    xmax <- (1-neg.eps) * base^max.exp

    xmax

    ## 1 'mpfr' number of precision 2048 bits
    ## [1] 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

    identical( asNumeric(xmax), .Machine$double.xmax )

    ## [1] TRUE

    关于r - 使用具有 gmp 和机器限制的大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14818267/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com