Join 36000+ teachers and students using TTIO.
Because floating-point numbers have a limited number of digits, they cannot represent all real numbers accurately: when there are more digits than the format allows, the leftover ones are omitted - the number is rounded.
Since the 0.1 cannot get any closer to the exact value of 1/10 and 0.3 cannot get any closer to the exact value of 3/10, then pre-rounding with
round() function cannot help:
>>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1) False
Though the numbers cannot be made closer to their intended exact values, the
round() function can be useful for post-rounding so that results with inexact values become comparable to one another:
>>> round(.1 + .1 + .1, 10) == round(.3, 10) True
Binary floating-point arithmetic holds many surprises like this.