简体   繁体   中英

Why does the R documentation state that numbers larger than the largest normalized floating-point number can occur?

R uses the IEEE 754 standard for double precision floating point numbers. Based on my understanding of this standard, whilst there are very small subnormal values which fill the underflow gap, there is no such equivalent for larger numbers.

I obtain the largest floating point number as follows:

.Machine$double.xmax
[1] 1.797693e+308

Printing this in hexadecimal to see the full binary representation, I get

sprintf("%+13.13a", .Machine$double.xmax)
[1] "+0x1.fffffffffffffp+1023"

Now, the exponent has its maximum possible value (we have biased exponent 2046, and the biased exponent 2047 is reserved for special cases, ie, infinity/NaN), and the significand has its maximum possible value (all 53 binary digits are 1). Hence, I would argue that no larger number can be represented.

However the R documentation for .Machine (see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/.Machine ) states the following for double.xmax :

the largest normalized floating-point number. Typically, it is equal to (1 - double.neg.eps) * double.base ^ double.max.exp , but on some machines it is only the second or third largest such number, being too small by 1 or 2 units in the last digit of the significand. Normally 1.797693e+308. Note that larger unnormalized numbers can occur .

What is meant by this? How could larger numbers occur?

Edit: Update below because the original answer wasn't technically correct (because I wasn't on computer to check). Please skip first paragraph for answer.

If you provide a number that is too large then the digits will be rounded but the exponent will be in the ballpark. You didn't provide the full length decimal, but let's pretend you did. If 1.797693e+308 is the max number then 1.797693999e+308 would get abbreviated to 1.797693e+308 . R wouldn't fail and you would be very confused. But you might have a bigger number like 58.88888999+310 and it might round to 58.8888888+310 . You would need to use a package made for very large numbers. This is because the max exponent is a base 2 number (which 309 is not) and you can larger exponents than you have significant digits.

Edit:

.Machine$double.xmax
# [1] 1.797693e+308

format(.Machine$double.xmax, scientific = FALSE)
# [1] "179769313486231570838400602864442228000008602082842266064064680402680408280648240046204888888288080622822420842246006644866884860462806420066668022046626024066662068886808602862886866800048228686262462640668044406484606206082824406288200264266406808068464046840608044222802268424008466606886862062820068082688"

.Machine$double.xmax + 100
# [1] 1.797693e+308

format(.Machine$double.xmax + 100, scientific = FALSE)
# [1] "179769313486231570838400602864442228000008602082842266064064680402680408280648240046204888888288080622822420842246006644866884860462806420066668022046626024066662068886808602862886866800048228686262462640668044406484606206082824406288200264266406808068464046840608044222802268424008466606886862062820068082688"

(.Machine$double.xmax + 100) == (.Machine$double.xmax)
# [1] TRUE

# Problem that can rarely occur:
# index <- index + 1 # This will give the same answer for numbers that are too large... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM