Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 57 / 68

Watch Out for Floating-Point Rounding Errors

In this lesson you will learn about the error computers introduce when handling decimals, and what to watch for in monetary calculations. This is for anyone searching for "Ruby floating point error".

Computers represent decimals in binary, so even a straightforward calculation such as 0.1 + 0.2 may not land exactly on 0.3. Print it and the error is visible; compare it and you get false.

The sample code shows 0.1 + 0.2 printing 0.30000000000000004, error included, and the comparison 0.1 + 0.2 == 0.3 coming back false.

A common stumbling block for beginners is not noticing this error at all and comparing decimals for equality directly. Where the error matters - monetary calculations, for instance - you need a measure such as the BigDecimal class.

This is not specific to Ruby; it comes from how computers work and applies to most programming languages. In accounting systems and anywhere precision is required, allowing for it is essential.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)