Why does the number 1e9999… (31 9s) cause problems in R?

前端 未结 3 1525
执笔经年
执笔经年 2021-02-05 01:16

When entering 1e9999999999999999999999999999999 into R, R hangs and will not respond - requiring it to be terminated.

It seems to happen across 3 different

相关标签:
3条回答
  • 2021-02-05 01:31

    R might use sometimes bignums. Perhaps 1e9999999999999999999999999999999 is some threshold, or perhaps the parsing routines have a limited buffer for reading the exponent. Your observation would be consistent with a 32 char (null-terminated) buffer for the exponent.

    I'll rather ask that question on forums or mailing list specific to R, which are rumored to be friendly.

    Alternatively, since R is free software, you could investigate its source code.

    0 讨论(0)
  • 2021-02-05 01:39

    This is interesting, but I think R has systemic problems with parsing numbers that have very large exponents:

    > 1e10000000000000000000000000000000
    [1] 0
    > 1e1000000000000000000000000000000
    [1] Inf
    > 1e100000000000000000000
    [1] Inf
    > 1e10000000000000000000
    [1] 0
    > 1e1000
    [1] Inf
    > 1e100
    [1] 1e+100
    

    There we go, finally something reasonable. According to this output and Joshua Ulrich's comment below, R appears to support representing numbers up to about 2e308 and parsing numbers with exponents up to about +2*10^9, but it cannot represent them. After that, there is undefined behavior apparently due to overflow.

    0 讨论(0)
  • 2021-02-05 01:49

    This looks like an extreme case in the parser. The XeY format is described in Section 10.3.1: Literal Constants of the R Language Definition and points to ?NumericConstants for "up-to-date information on the currently accepted formats".

    The problem seems to be how the parser handles the exponent. The numeric constant is handled by NumericValue (line 4361 of main/gram.c), which calls mkFloat (line 4124 of main/gram.c), which calls R_atof (line 1584 of main/util.c), which calls R_strtod4 (line 1461 of main/util.c). (All as of revision 60052.)

    Line 1464 of main/utils.c shows expn declared as int and it will overflow at line 1551 if the exponent is too large. The signed integer overflow causes undefined behavior.

    For example, the code below produces values for exponents < 308 or so and Inf for exponents > 308.

    const <- paste0("1e",2^(1:31)-2)
    for(n in const) print(eval(parse(text=n)))
    

    You can see the undefined behavior for exponents > 2^31 (R hangs for an exponent = 2^31):

    const <- paste0("1e",2^(31:61)+1)
    for(n in const) print(eval(parse(text=n)))
    

    I doubt this will get any attention from R-core because R can only store numeric values between about 2e-308 to 2e+308 (see ?double) and this number is way beyond that.

    0 讨论(0)
提交回复
热议问题