Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b87f11

Browse files
committed
Fix some documentation examples involving the repr of a float.
1 parent 9a03f2f commit 6b87f11

8 files changed

Lines changed: 22 additions & 19 deletions

File tree

Doc/faq/design.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ necessary to make ``eval(repr(f)) == f`` true for any float f. The ``str()``
7575
function prints fewer digits and this often results in the more sensible number
7676
that was probably intended::
7777

78-
>>> 0.2
79-
0.20000000000000001
80-
>>> print 0.2
78+
>>> 1.1 - 0.9
79+
0.20000000000000007
80+
>>> print 1.1 - 0.9
8181
0.2
8282

8383
One of the consequences of this is that it is error-prone to compare the result

Doc/library/decimal.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ arithmetic. It offers several advantages over the :class:`float` datatype:
3535
people learn at school." -- excerpt from the decimal arithmetic specification.
3636

3737
* Decimal numbers can be represented exactly. In contrast, numbers like
38-
:const:`1.1` do not have an exact representation in binary floating point. End
39-
users typically would not expect :const:`1.1` to display as
40-
:const:`1.1000000000000001` as it does with binary floating point.
38+
:const:`1.1` and :const:`2.2` do not have an exact representations in binary
39+
floating point. End users typically would not expect ``1.1 + 2.2`` to display
40+
as :const:`3.3000000000000003` as it does with binary floating point.
4141

4242
* The exactness carries over into arithmetic. In decimal floating point, ``0.1
4343
+ 0.1 + 0.1 - 0.3`` is exactly equal to zero. In binary floating point, the result
@@ -193,7 +193,7 @@ floating point flying circus:
193193
>>> str(a)
194194
'1.34'
195195
>>> float(a)
196-
1.3400000000000001
196+
1.34
197197
>>> round(a, 1) # round() first converts to binary floating point
198198
1.3
199199
>>> int(a)

Doc/library/math.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Number-theoretic and representation functions
9090
loss of precision by tracking multiple intermediate partial sums::
9191

9292
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
93-
0.99999999999999989
93+
0.9999999999999999
9494
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
9595
1.0
9696

Doc/library/sqlite3.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ This example uses the iterator form::
8383
>>> for row in c:
8484
... print row
8585
...
86-
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001)
86+
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)
8787
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
8888
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
8989
(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
@@ -601,7 +601,7 @@ Now we plug :class:`Row` in::
601601
>>> type(r)
602602
<type 'sqlite3.Row'>
603603
>>> r
604-
(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.140000000000001)
604+
(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14)
605605
>>> len(r)
606606
5
607607
>>> r[2]

Doc/library/turtle.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ Color control
875875
>>> tup = (0.2, 0.8, 0.55)
876876
>>> turtle.pencolor(tup)
877877
>>> turtle.pencolor()
878-
(0.20000000000000001, 0.80000000000000004, 0.5490196078431373)
878+
(0.2, 0.8, 0.5490196078431373)
879879
>>> colormode(255)
880880
>>> turtle.pencolor()
881881
(51, 204, 140)

Doc/tutorial/floatingpoint.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Another consequence is that since 0.1 is not exactly 1/10, summing ten values of
115115
... sum += 0.1
116116
...
117117
>>> sum
118-
0.99999999999999989
118+
0.9999999999999999
119119

120120
Binary floating-point arithmetic holds many surprises like this. The problem
121121
with "0.1" is explained in precise detail below, in the "Representation Error"

Doc/tutorial/inputoutput.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ Some examples::
4949
'Hello, world.'
5050
>>> repr(s)
5151
"'Hello, world.'"
52-
>>> str(0.1)
53-
'0.1'
54-
>>> repr(0.1)
55-
'0.10000000000000001'
52+
>>> str(1.0/7.0)
53+
'0.142857142857'
54+
>>> repr(1.0/7.0)
55+
'0.14285714285714285'
5656
>>> x = 10 * 3.25
5757
>>> y = 200 * 200
5858
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'

Doc/tutorial/stdlib2.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ results in decimal floating point and binary floating point. The difference
362362
becomes significant if the results are rounded to the nearest cent::
363363

364364
>>> from decimal import *
365-
>>> Decimal('0.70') * Decimal('1.05')
365+
>>> x = Decimal('0.70') * Decimal('1.05')
366+
>>> x
366367
Decimal('0.7350')
367-
>>> .70 * 1.05
368-
0.73499999999999999
368+
>>> x.quantize(Decimal('0.01')) # round to nearest cent
369+
Decimal('0.74')
370+
>>> round(.70 * 1.05, 2) # same calculation with floats
371+
0.73
369372

370373
The :class:`Decimal` result keeps a trailing zero, automatically inferring four
371374
place significance from multiplicands with two place significance. Decimal

0 commit comments

Comments
 (0)