Why is this outputting 100.80000000000001 instead of just 100.8 ?
It is the nature of floating point arithmetic to produce a minuscule error due to how binary math is performed. Decimal fractions need to be resolved from a binary mantissa and exponent. There is no absolute precision in this process.
The simplest way to resolve it is with print formatting.
Now it will print as
Note above that we do not need to recast the value to str. The format engine treats it as a float given the directive we supply after the colon.
.2f => 2 decimal places
We are not mutating the value, only representing it in string form.
Thanks for the fix. It’s going to take me a while to understand what you mean, there’s several words in your reply I’ve never heard before.
Should this be reported as a bug to the person who wrote the lesson?
No, it’s not a bug. Normal behavior in any language.
When you have the time, google ‘floating point arithmetic’ to get a look at the mechanics of how floats are stored.