37

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

It seems to happen across 3 different computers, OSes (Windows 7 and Ubuntu). It happens in RStudio, RGui and RScript.

Here's some code to generate the number more easily:

boom <- paste(c("1e", rep(9, 31)), collapse="")
eval(parse(text=boom))

Now clearly this isn't a practical problem. I have no need to use numbers of this magnitude. It's just a question of curiosity.

Curiously, if you try 1e9999999999999999999999999999998 or 1e10000000000000000000000000000000 (add or subtract one from the power), you get Inf and 0 respectively. This number is clearly some kind of boundary, but between what and why here?

I considered that it might be:

  • A floating point problem, but I think they max out at 1.7977e308, long before the number in question.
  • An issue with 32-bit integers, but 2^32 is 4294967296, much smaller than the number in question.
  • Really weird. This is my dominant theory.

EDIT: As of 2015-09-15 at the latest, this no longer causes R to hang. They must have patched it.

6

3 Answers 3

25

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.

8
  • I fear that the set of things for which "doubt this will get any attention from R-core" is constantly growing. I sent two mails in the last few weeks and got zero replies. :-/ Jul 30, 2012 at 16:05
  • 1
    Between that set and the "I would ask R-core this question (or submit this patch) but I'm sure I'd get roundly spanked" set, it's tough for me to work up much gumption to participate. Jul 30, 2012 at 20:13
  • 4
    I would hope that any overflow, no matter the context, would be considered patchworthy though. Jul 30, 2012 at 20:14
  • 2
    @sebastian-c: issues should occur with any exponent > 2147483647. I actually get a hang for 1e2147483647, and 1e2147483648 = 0, while 1e2147483646 = Inf (as expected). Jul 31, 2012 at 1:21
  • 2
    I think I can see what is special about 31 9's. It is exactly one less that 10^32 and 10 has 2 as a factor. So there is a 2^32 in there just waiting to cause trouble. 9999999999999999999999999999999 is 2^31-1 which is 2147483647 after overflow. The result of overflow is modulus, or the remainder after repeated integer division. (10^31-1)/(2^32) = (2^31*5^31)/2^32 - 1/2^32 = 5^31/2 - 1/2^32 = (5^31-1) + 1/2 - 1/2^32. Take off the quotient of 5^31-1 leaves a fractional part of (2^31-1)/2^32 which is a remainder of 2^31-1 or 2147483647.
    – John Watts
    Aug 1, 2012 at 1:32
5

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.

6
  • 2
    It can parse numeric constants with exponents +/-2*10^9, but it cannot represent them because double precision values are limited to about 2e-308 to 2e+308 (see ?double). Jul 30, 2012 at 17:56
  • 2
    I wouldn't say it was properly parsing them. The representation keeps changing between +Inf and 0 depending only on the number of digits in the exponent. If it were parsing properly it would either reject the input or map all these inputs to +Inf.
    – John Watts
    Jul 30, 2012 at 18:48
  • I only said it could parse exponents up to the signed integer bound and the result only changes between 0 and Inf once the exponent is larger than that and you overflow the exponent variable in the parser: const <- paste0("1e",2^(10:32-1); for(n in const) print(eval(parse(text=n))). Jul 30, 2012 at 18:58
  • Gotcha. So would it be fair to say the following? 1. It parses and represents up to about 2e+308. 2. It parses but cannot represent up to about 2e+2^32 so they become +Inf. 3. Beyond that the behavior is undefined.
    – John Watts
    Jul 30, 2012 at 21:48
  • Yes, that's my understand of what's happening. Line 1446 of main/utils.c shows expn declared as int and it will overflow at line 1553 if n is too large, which will cause undefined behavior. Jul 30, 2012 at 22:01
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.