Real numbers in R are stored in double precision, which means that 53 bit floating point arithmetic is used (see IEEE 754-2008 for more detail). Double precision is the standard for numerical calculations, where speed is required. This cannot represent irrational numbers and rational numbers, whose denominator is not a power of 2. For example, we get
0.1 + 0.2 - 0.3 # [1] 5.551115e-17
since 0.1, 0.2 and 0.3 have an infinite binary expansion. For example 0.1 in binary is
0.0001100110011001100110011001100110011001100110...
Consequently, the above operation involves a rounding error.
See FAQ 7.31 for further information.
The problem is not limited to non-integer numbers. Any number with more than 53 significant digits in binary expansion is rounded in the machine representation. For example,
(10^19 + 10^4) - 10^19 # [1] 10240
or, equivalently,
(1e19 + 1e4) - 1e19 # [1] 10240
The binary expansion of the number 10^19 + 10^4 is
1000101011000111001000110000010010001001111010000010011100010000
It has 60 significant binary digits. Machine representation rounds numbers to 53 significant digits, so we get the number with binary expansion
1000101011000111001000110000010010001001111010000010100000000000
which is 10000000000000010240 in decimal. The number 10^19 is represented exactly, so subtracting it yields the above result.
The fact that numbers like 0.1 are not represented exactly does not mean that we cannot get correct result, at least in simple cases, if the calculations are done with care. Some facts and suggestions concerning the work with decimal numbers in R useful for beginners may be found at accuracy of decimal numbers.
Package accuracy contains tools for testing and improving accuracy of statistical results.
Package rcdd contains tools related to computational geometry using GMP. Package is contained in CRAN Task View Optimization.
Package gmp provides basic arithmetic operations for exact rational and large integer numbers using GMP Library. An example of its use may be found at rational numbers.
Package Rmpfr (see also Rmpfr at R-Forge) provides multiple-precision floating-point computations with correct rounding using MPFR Library and GMP Library. An example of its use may be found at high precision arithmetic.
High precision arithmetic means that one may perform a numerical calculation with precision of an arbitrary number of binary or decimal digits.
A good option for high precision decimal arithmetic in simple cases is Berkeley calculator (bc). Its disadvantage is that it is an external program that has to be called from R for each expression separately.
High precision binary arithmetic is available from R using Rmpfr package, which provides an R interface to the libraries MPFR and GMP.
See high precision arithmetic for an example how to call bc from R or how to use Rmpfr package. The section also contains examples how to use Ryacas and Yacas for precise numerical calculation from R and for other alternatives.
If the result of a numerical calculation is a rational number with not too large denominator, then you may succeed to obtain the fraction using function fractions() from MASS package.
Computation with arbitrary rational numbers represented as a fraction may be done using gmp package and GMP Library.
See rational numbers for more detail.
In cases, where exact arithmetic is needed, one can use symbolic computation, which is available in any computer algebra system. A list may be found at Comparison of computer algebra systems. In particular, if you have Yacas installed, then you may use Ryacas extension package to access Yacas directly from R. Another option for symbolic computation from R is rSymPy package, which requires only Java to be installed on the system.
Symbolic computation means to provide an exact result. The symbolic arithmetic can work exactly with arbitrary rational numbers and to some extent also with irrational numbers represented by algebraic expressions. Different software supports different types of expressions. For example, some of the symbolic software packages can expand (sqrt(2) + 1)7 correctly into 169 sqrt(2) + 239, but not every symbolic calculator can handle this.
See computer algebra systems and R for more detail.
The largest value of the R numeric data type is (1 - 2-53)21024, which is approximately 1.797693e+308. If you need to work with numbers, which are larger, but 53 bit precision is sufficient, you may use logarithms to represent the numbers and the package Brobdingnag to simplify the code.
The default random number generator used in function runif() generates numbers with 32 random bits. In R documentation, this is discussed in ?base::RNGkind. In some situations, 32 bits may not be sufficient. See random numbers for more information.
Created Feb 26, 2009.