Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?

These accuracy problems are due to the internal representation of floating point numbers and there’s not much you can do to avoid it.

By the way, printing these values at run-time often still leads to the correct results, at least using modern C++ compilers. For most operations, this isn’t much of an issue.

Leave a Comment