Ch2.2.1 Binary Numbers

Ch2.2 Internal Data Storage

  • Internally, R has a variety of ways to store numbers.
  • We have already seen the aggregate data types such as matrices and vectors.

title

Ch2.2 Internal Data Storage

  • The two storage types we are going to focus on here are integer and numeric data types.
  • Most numbers in R are stored as floating point numbers.

title

Ch2.2 Internal Data Storage

  • R does not use integers often and even when used as index for a vector, integers are stored as floating point numbers.
  • Floating point numbers are made up of several components, including two integers for the value and size.

title

Ch2.2.1 Binary Numbers

  • When R creates an integer, as we described in Section 1.2.1, R uses the native 32-bit integer format.
  • The 32-bit integer format uses 31 bits to store the number and one bit to store the sign of the number.
  • As we will see, the integer format is not the default in R.
  • When working with numbers, integers are usually coerced into the floating point numeric format, described later.
  • But the basic binary format underpins the storage of numerical data.

Integer Data Type

  • Integer data type cannot store any fractional part of number.
  • Thus not many numbers can be represented.
  • The largest positive number R can hold as an integer is \( 2^{31}-1 = 2147483647 \).
  • As we increment by one, we quickly reach a number too large for the data type (see next slide.
  • The \( \textsf{as.integer} \) function converts result into an integer.
  • Otherwise R converts everything to numeric.

Largest Positive Integer

as.integer(2^31 - 2)
[1] 2147483646
as.integer(2^31 - 1)
[1] 2147483647
as.integer(2^31)
[1] NA

Smallest Negative Integer

as.integer(-2^31 + 2)
[1] -2147483646
as.integer(-2^31 + 1)
[1] -2147483647
as.integer(-2^31)
[1] NA

Using L Appendage

  • L = Long = declaration for 32-bit integer
-2147483646L
[1] -2147483646
-2147483646L - 1L
[1] -2147483647
-2147483646L - 2L
[1] NA

Max of Data Type

  • The largest positive and negative numbers are called the maximum and minimum of the data type.
  • The largest number is the number with the greatest magnitude, regardless of sign.
as.integer(2^31 - 1)
[1] 2147483647

Min of Data Type

  • The smallest number has lowest magnitude.
  • These are the numbers closest to 0.
  • In the case of integers, this is 1 and -1.
.Machine$double.eps
[1] 2.220446e-16

Max of Data Type

  • The max value of a 32-bit integer, roughly 2.1 billion, is too small for many common figures.
as.integer(2^31 - 1)
[1] 2147483647
  • 2020 population of world = 7.8 billon.
  • The value of 13! is more than 6 billion.
factorial(13)
[1] 6227020800

Max of Data Type

Scientists using Google's supercomputers have calculated that there are 43 252 003 274 489 856 000 different configurations in a standard 3x3x3 cube. This number would have taken 35 years for a standard PC to calculate.

title

Min of Data Type

  • Note integer data type cannot store numbers smaller than 1.
  • If we entered \( 1/2 \), it would round down to zero.
as.integer(0.5)
[1] 0
as.integer(1.9)
[1] 1

Integer as Numeric

  • R has no trouble with numbers larger than \( 2^{31}-1 \).
  • R uses numeric format to hold these integers.
2^31
[1] 2147483648
class(2^31)
[1] "numeric"