Ch2.3.3 Overflow and Underflow

Overflow and Underflow & R

  • Because R's numeric data type can hold such a wide range of numbers, underflow and overflow are less likely to be a problem than they could be with other systems.
  • We still need to understand risks of underflow and overflow when R is working with other operating environments.

title

Overflow and Underflow

  • Underflow occurs when a number is too small, regardless of sign, to be represented as anything other than 0.
  • Overflow occurs when a number is too large to be represented as a numeric type.

title

Overflow and Underflow

  • Double precision numeric data types can represent numbers on both ends of the scale that most applications require.
.Machine$double.xmax
[1] 1.797693e+308
.Machine$double.xmin
[1] 2.225074e-308

Machine Min and Powerball Chances

  • Chances of winning Powerball lottery with single ticket is:
  • 1 in 292,201,338
  • See weblink
1/292201338
[1] 3.422298e-09

title

.Machine$double.xmin
[1] 2.225074e-308

Underflow & Small Numbers

  • Lowest density vacuum created in a lab was approx \( 1 \times 10^{-18} \) Pascals.
  • Probability of rolling die 20 times and getting same number each time is approx \( 2.74 \times 10^{-16} \).
  • Is there a smallest practical measurement?
.Machine$double.xmin
[1] 2.225074e-308

Underflow & Planck Length

  • The Planck length, the smallest theoretical measurable distance, is \( 1.61619926 \times 10^{-35} \).
  • Considered smallest length at which space can be defined.
.Machine$double.xmin
[1] 2.225074e-308

Underflow & Planck Length

title

.Machine$double.xmin
[1] 2.225074e-308

title

Quantum Foam

title

  • Artist rendition of spacetime at Planck scale as quantum foam.
  • This foam may have an enormous energy, but may still resemble a zero-energy vacuum on large scales because its effects cancel at the tiniest scales.
  • https://physics.aps.org/articles/v12/105

Wormholes and Timetravel

title

.Machine$double.xmin
[1] 2.225074e-308

title

Underflow & Planck Length

  • Thus even at extreme lower boundary of small-scale physics and probability, the numbers involved are far larger than the lower bound of double precision.
  • These exceptionally small numbers are 270 orders of magnitude larger than smallest number a double precision number can hold, but they are there if we need them.

title

Example: Achieving Underflow

  • Floating point can work around underflow even further.
  • For example, the simplest way to actually to achieve reach underflow should be to divide .Machine$double.xmin by 2, a result which should yield 0 (but doesn't):
0.5*.Machine$double.xmin
[1] 1.112537e-308
0.0001*.Machine$double.xmin
[1] 2.225074e-312

Example: Achieving Underflow

  • The internal floating point representation moves leading zeros to mantissa to avoid underflow.
0.5*.Machine$double.xmin
[1] 1.112537e-308
  • These numbers are called denormalized and push underflow boundary even lower than .Machine$double.xmin.

title

Denormalized Numbers

  • IEEE standard denormalizes numbers to fill the gap between 0 and the smallest normalized floating point number.
  • This provides a gradual underflow to zero; see weblink.

\[ (-1)^s 2^{c-127} (1+f) \]

title

Denormalized Numbers

  • The implementation of denormalization is machine dependent and may not necessarily be available.
  • In practice, we are unlikely to reach the underflow boundary with a real-world problem.

title

Machine Epsilon and Machine Min

  • Note that .Machine$double.xmin value is substantially smaller than machine \( \epsilon \).
library(pracma)
c(.Machine$double.xmin,eps(1))
[1] 2.225074e-308  2.220446e-16

title

Machine Epsilon and Machine Min

  • The value of the next floating point number after \( x \) changes based on how large \( x \) is.
library(pracma)
x <- .Machine$double.xmin
c(eps(0),x)
[1] 2.225074e-308 2.225074e-308

title

Machine Min is Mostly the Min

  • Strange result here:
x <- .Machine$double.xmin
c(eps(0),x,eps(x))
[1] 2.225074e-308 2.225074e-308 4.940656e-324

title

Distribution of Floating Point Numbers

  • Floating point numbers are closer together closer to 0.
  • Interestingly, there are roughly the same number of floating point numbers between 0 and 1 as there are greater than 1.

title

Overflow

  • Overflow occurs when number is too large to be expressed.
  • Like undeflow, most real-world applications do not require numbers this large.
.Machine$double.xmax
[1] 1.797693e+308

title

Machine Max & Machine Epsilon

  • Recall magnitude of \( \epsilon(x) \) proportional to magnitude of \( x \).
x <- .Machine$double.xmax
c(x,eps(x))
[1] 1.797693e+308           Inf

title

Machine Max

  • Machine max is larger than almost all numbers that might be used in practice.
  • Cosmologists estimate about \( 10^{80} \) hydrogen atoms in universe.
.Machine$double.xmax
[1] 1.797693e+308

title

Machine Max

  • Average star weighs about \( 10^{35} \) grams.
  • Surface area of Milky Way's disk is roughly \( 10^{42} m^2 \).
.Machine$double.xmax
[1] 1.797693e+308

title

Machine Max

  • For the integer data format, we can achieve overflow easily:
as.integer(2^31 - 1)
[1] 2147483647
as.integer(2^31)
[1] NA

Machine Max is Mostly the Max

  • Recall that the computer has a mechanism to handle numbers below .Machine$double.xmin.
  • No comparable method for .Machine$double.xmax.
(x <- .Machine$double.xmax)
[1] 1.797693e+308
c(x + 1, 2*x, x + eps(x))
[1] 1.797693e+308           Inf           Inf

Observations

  • R does not cause an error for underflow and overflow.
  • It simply uses the best available option.
  • For underflow, using 0 may be a good enough answer.
  • For overflow, it is unlikely that infinity is a good enough answer, but it can signal that something has gone wrong.

title

Compatibility

  • Not all systems will process data with double precision floating point numbers.
  • R is capable of interacting with and exchanging data with systems that only support single precision arithmetic, where the limits are substantially smaller.

title

Single Precision

title title

  • Single precision smallest: \[ 5.87747175411144 \times 10^{-39} \]
  • Single precision largest: \[ 3.40282366920938 \times 10^{38} \]
  • While smaller and larger than most applications, we have seen examples where insufficient.

Conclusion

  • We should keep in mind where the boundaries are and be aware if they are approaching.
  • It is possible that we may reach a boundary during an intervening step in a complex calculation.
  • Monitoring and understanding the calculation's complete process will allow us to avoid those conditions, or at least be aware that a result may not be valid.
  • We will examine that in the next subsection.