Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 57 / 68

Watch out for floating-point error

This lesson covers floating-point error in Julia, with the aim of understanding why decimal arithmetic can give surprising results.

Computers hold decimals in binary, so even a simple calculation such as 0.1 + 0.2 may not come out exactly 0.3. Displayed, the error shows; compared, you get false.

The sample code shows that println(0.1 + 0.2) prints a value carrying an error and that 0.1 + 0.2 == 0.3 comes back false. This happens in most languages that use floating point.

A common early stumble is comparing decimals directly with == and not getting the expected result. In Julia the safe approach is isapprox(), which compares within a tolerance.

In professional work, that error accumulates especially readily in numerical computing, which makes precision-aware algorithm design important.

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

๐Ÿงช This site can't compile or run Julia 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)