Rational and BigDecimal
Exact arithmetic.
Rational and BigDecimal is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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)Rationals Auto-Simplify
Rationals reduce to lowest terms automatically, so 2/4 becomes 1/2.
puts Rational(6, 8)
puts Rational(10, 5)Exact Fraction Math
Adding rationals stays exact, unlike floats. One third plus one third plus one third is exactly one.
third = Rational(1, 3)
puts (third + third + third)Numerator and Denominator
Read the parts with numerator and denominator.
r = Rational(3, 4)
puts r.numerator
puts r.denominatorConverting Rationals
to_f turns a rational into a float when you need a decimal approximation.
puts Rational(1, 8).to_fLoading BigDecimal
BigDecimal handles exact decimals, ideal for money. Require it from the standard library first.
require "bigdecimal"
a = BigDecimal("0.1")
b = BigDecimal("0.2")
puts (a + b).to_s("F")Always Use Strings
Create BigDecimals from strings, not floats, so no precision is lost before it starts.
require "bigdecimal"
good = BigDecimal("1.1")
puts good.to_s("F")Money Calculation
BigDecimal keeps cents exact across many operations.
require "bigdecimal"
price = BigDecimal("19.99")
qty = 3
total = price * qty
puts total.to_s("F")Rounding BigDecimal
round on a BigDecimal controls decimal places precisely.
require "bigdecimal"
n = BigDecimal("2.675")
puts n.round(2).to_s("F")Choosing the Right Tool
Use Rational for exact fractions and ratios. Use BigDecimal for exact decimal money. Use Float for fast scientific work where small error is fine.
require "bigdecimal"
puts Rational(1, 3)
puts BigDecimal("0.33").to_s("F")Quick Check
Check exact arithmetic.
Recap
You learned exact arithmetic:
- Rational for exact fractions that auto-simplify
numerator,denominator,to_f- BigDecimal from strings for exact money math
- choose the right type for the job
That completes the Ruby Numbers and Math course.
puts (Rational(1, 2) + Rational(1, 3))Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Rational and BigDecimal” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Rational and BigDecimal”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
What will I learn in “Rational and BigDecimal”?
Exact arithmetic. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Rational and BigDecimal” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integers and Floats
- Arithmetic and Operators
- The Math Module
- Rational and BigDecimal