bit = “binary” + “digit” (0 or 1)
byte = 8 bits
kB = kilobyte = 103 bytes
MB = megabytes = 106 bytes
GB = gigabytes = 109 bytes
TB = terabytes = 1012 bytes
PB = petabytes = 1015 bytes
Fixed-point number system is a computer model for integers. One storage unit may be M=8/16/32/64 bit.
The first bit stores the sign of the integer (i.e. 0 for positive and 1 for negative)
The rest of the bits store the absolute value of the integer in binary
Range of representable integers by M-bit storage unit is [−2M−1,2M−1−1] (don’t need to represent 0 anymore so could have capacity for 2M−1 negative numbers).
For M=8, [−128,127]. For M=16, [−65536,65535]. For M=32, [−2147483648,2147483647].
.Machine$integer.max
## [1] 2147483647
# integer type in R uses M=32 bits
M <- 32
big <- 2^(M-1) - 1
small <- -2^(M-1)
as.integer(big)
## [1] 2147483647
as.integer(big + 1)
## Warning: NAs introduced by coercion to integer range
## [1] NA
as.integer(small + 1)
## [1] -2147483647
as.integer(small)
## Warning: NAs introduced by coercion to integer range
## [1] NA
Keep track of overflow and underflow. If the result of a summation is R, which must be in the set [−2M−1,2M−1−1], there are only three possibilities for the true sum: R, R+2M (overflow), or R−2M (underflow).
Floating-point number system is a computer model for real numbers.
A real is represented by value=(−1)b31×2(b30b29…b23)2−127×(1.b22b21…b0)2.
IEEE 754-1985 and IEEE 754-2008
Single precision (32 bit): base b=2, p=23 (23 significant bits), emax=127, emin=−126 (8 exponent bits), bias=127. This implies a maximum magnitude of log10(2127)≈38 and precision to log10(223)≈7 decimal point. ±10±38.
Double precision (64 bit): base b=2, p=52 (52 significant bits), emax=1023, emin=−1022 (11 exponent bits), bias=1023. This implies a maximum magnitude of log10(21023)≈308 and precision to log10(252)≈16 decimal point. ±10±308.
In the above example, value=
2^((2^2 + 2^3 + 2^4 + 2^5 + 2^6) - 127) * (1 + 2^(-2))
## [1] 0.15625
To summarize
Single precision: ±10±38 with precision up to 7 decimal digits.
Double precision: ±10±308 with precision up to 16 decimal digits.
The floating-point numbers do not occur uniformly over the real number line.
The variable .Machine in R contains numerical characteristics of the machine.
How to test inf and nan? In R