mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Fix some documentation examples involving the repr of a float.
This commit is contained in:
parent
9a03f2fd03
commit
6b87f117ca
8 changed files with 22 additions and 19 deletions
|
@ -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"
|
||||
|
|
|
@ -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) + '...'
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue