There was an error while loading. Please reload this page.
1 parent cc32a11 commit 4af3629Copy full SHA for 4af3629
1 file changed
Doc/tutorial/floatingpoint.rst
@@ -109,14 +109,24 @@ It's important to realize that this is, in a real sense, an illusion: you're
109
simply rounding the *display* of the true machine value.
110
111
One illusion may beget another. For example, since 0.1 is not exactly 1/10,
112
-summing ten values of 0.1 may not yield exactly 1.0, either::
113
-
114
- >>> sum = 0.0
115
- >>> for i in range(10):
116
- ... sum += 0.1
117
- ...
118
- >>> sum
119
- 0.9999999999999999
+summing three values of 0.1 may not yield exactly 0.3, either::
+
+ >>> .1 + .1 + .1 == .3
+ False
+Also, 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
+:func:`round` function cannot help::
120
121
+ >>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
122
123
124
+Though the numbers cannot be made closer to their intended exact values,
125
+the :func:`round` function can be useful for post-rounding so that results
126
+have inexact values that are comparable to one another::
127
128
+ >>> round(.1 + .1 + .1, 1) == round(.3, 1)
129
+ True
130
131
Binary floating-point arithmetic holds many surprises like this. The problem
132
with "0.1" is explained in precise detail below, in the "Representation Error"
0 commit comments