3.3 Dealing with integer and floating point numbers

  • Doing math with integer is always safe
.Machine$integer.max 
## [1] 2147483647
# but you can still overflow:
# note: it is a warning not an error 
.Machine$integer.max + 1L 
## Warning in .Machine$integer.max + 1L: NAs produced by integer overflow
## [1] NA
  • Floating point number are “limited”: 8 bytes
print(0.1234567891123456789012, digits = 22)
## [1] 0.1234567891123456856439

Hence:

0.1 + 0.2 == 0.3
## [1] FALSE

What can we do!

  • round is an usual trick

  • the author likes abs(x - y) < very_small_number

abs(sin(pi) - 0) < 2^-26
## [1] TRUE