From 06174be449feb4b7187dff1ec1ca711fa1cdd101 Mon Sep 17 00:00:00 2001 From: "mark.dickinson" Date: Tue, 24 Nov 2009 14:27:02 +0000 Subject: [PATCH] Fix some documentation examples involving the repr of a float. git-svn-id: http://svn.python.org/projects/python/trunk@76489 6015fed2-1504-0410-9fe1-9d1591cc4771 --- Doc/faq/design.rst | 6 +++--- Doc/library/decimal.rst | 8 ++++---- Doc/library/math.rst | 2 +- Doc/library/sqlite3.rst | 4 ++-- Doc/library/turtle.rst | 2 +- Doc/tutorial/floatingpoint.rst | 2 +- Doc/tutorial/inputoutput.rst | 8 ++++---- Doc/tutorial/stdlib2.rst | 9 ++++++--- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 94afaff14e..02e3fad6b0 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -75,9 +75,9 @@ necessary to make ``eval(repr(f)) == f`` true for any float f. The ``str()`` function prints fewer digits and this often results in the more sensible number that was probably intended:: - >>> 0.2 - 0.20000000000000001 - >>> print 0.2 + >>> 1.1 - 0.9 + 0.20000000000000007 + >>> print 1.1 - 0.9 0.2 One of the consequences of this is that it is error-prone to compare the result diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index ee87023300..43f4b4a77e 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -35,9 +35,9 @@ arithmetic. It offers several advantages over the :class:`float` datatype: people learn at school." -- excerpt from the decimal arithmetic specification. * Decimal numbers can be represented exactly. In contrast, numbers like - :const:`1.1` do not have an exact representation in binary floating point. End - users typically would not expect :const:`1.1` to display as - :const:`1.1000000000000001` as it does with binary floating point. + :const:`1.1` and :const:`2.2` do not have an exact representations in binary + floating point. End users typically would not expect ``1.1 + 2.2`` to display + as :const:`3.3000000000000003` as it does with binary floating point. * The exactness carries over into arithmetic. In decimal floating point, ``0.1 + 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: >>> str(a) '1.34' >>> float(a) - 1.3400000000000001 + 1.34 >>> round(a, 1) # round() first converts to binary floating point 1.3 >>> int(a) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 78a8a56042..d297c78df6 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -90,7 +90,7 @@ Number-theoretic and representation functions loss of precision by tracking multiple intermediate partial sums:: >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) - 0.99999999999999989 + 0.9999999999999999 >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) 1.0 diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index d437028bab..3f5161a0b6 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -83,7 +83,7 @@ This example uses the iterator form:: >>> for row in c: ... print row ... - (u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001) + (u'2006-01-05', u'BUY', u'RHAT', 100, 35.14) (u'2006-03-28', u'BUY', u'IBM', 1000, 45.0) (u'2006-04-06', u'SELL', u'IBM', 500, 53.0) (u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0) @@ -601,7 +601,7 @@ Now we plug :class:`Row` in:: >>> type(r) >>> r - (u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.140000000000001) + (u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14) >>> len(r) 5 >>> r[2] diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index 759997a818..32650a71f5 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -875,7 +875,7 @@ Color control >>> tup = (0.2, 0.8, 0.55) >>> turtle.pencolor(tup) >>> turtle.pencolor() - (0.20000000000000001, 0.80000000000000004, 0.5490196078431373) + (0.2, 0.8, 0.5490196078431373) >>> colormode(255) >>> turtle.pencolor() (51, 204, 140) diff --git a/Doc/tutorial/floatingpoint.rst b/Doc/tutorial/floatingpoint.rst index 29c7a660e7..3554e4f451 100644 --- a/Doc/tutorial/floatingpoint.rst +++ b/Doc/tutorial/floatingpoint.rst @@ -115,7 +115,7 @@ Another consequence is that since 0.1 is not exactly 1/10, summing ten values of ... sum += 0.1 ... >>> sum - 0.99999999999999989 + 0.9999999999999999 Binary floating-point arithmetic holds many surprises like this. The problem with "0.1" is explained in precise detail below, in the "Representation Error" diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 0259749ee2..8d23cc16bb 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -49,10 +49,10 @@ Some examples:: 'Hello, world.' >>> repr(s) "'Hello, world.'" - >>> str(0.1) - '0.1' - >>> repr(0.1) - '0.10000000000000001' + >>> str(1.0/7.0) + '0.142857142857' + >>> repr(1.0/7.0) + '0.14285714285714285' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' diff --git a/Doc/tutorial/stdlib2.rst b/Doc/tutorial/stdlib2.rst index 0197a6fb32..4ae85b139c 100644 --- a/Doc/tutorial/stdlib2.rst +++ b/Doc/tutorial/stdlib2.rst @@ -362,10 +362,13 @@ results in decimal floating point and binary floating point. The difference becomes significant if the results are rounded to the nearest cent:: >>> from decimal import * - >>> Decimal('0.70') * Decimal('1.05') + >>> x = Decimal('0.70') * Decimal('1.05') + >>> x Decimal('0.7350') - >>> .70 * 1.05 - 0.73499999999999999 + >>> x.quantize(Decimal('0.01')) # round to nearest cent + Decimal('0.74') + >>> round(.70 * 1.05, 2) # same calculation with floats + 0.73 The :class:`Decimal` result keeps a trailing zero, automatically inferring four place significance from multiplicands with two place significance. Decimal -- 2.11.4.GIT