Rational and BigDecimal
Exact arithmetic.
Why Exact Arithmetic?
Floats lose precision. For money and fractions Ruby offers Rational and BigDecimal that stay exact.
puts 0.1 + 0.2Creating a Rational
A Rational is an exact fraction. Write it with r suffix or Rational(num, den).
puts (1r / 3)
puts Rational(2, 4)All lessons in this course
- Integers and Floats
- Arithmetic and Operators
- The Math Module
- Rational and BigDecimal