annotate Doc/tutorial/floatingpoint.rst @ 64166:d0e9febfa00a 3.1

Merged revisions 84172 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines Restore GIL in nis_cat in case of error. ........
author Martin v. Löwis <martin@v.loewis.de>
date Thu, 19 Aug 2010 09:03:03 +0000
parents 056436065290
children 3c9232a684ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 .. _tut-fp-issues:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 **************************************************
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 Floating Point Arithmetic: Issues and Limitations
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 **************************************************
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 .. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 Floating-point numbers are represented in computer hardware as base 2 (binary)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 fractions. For example, the decimal fraction ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
12
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 0.125
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
14
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
16
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 0.001
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 has value 0/2 + 0/4 + 1/8. These two fractions have identical values, the only
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 real difference being that the first is written in base 10 fractional notation,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 and the second in base 2.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
22
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23 Unfortunately, most decimal fractions cannot be represented exactly as binary
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 fractions. A consequence is that, in general, the decimal floating-point
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 numbers you enter are only approximated by the binary floating-point numbers
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 actually stored in the machine.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
27
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 The problem is easier to understand at first in base 10. Consider the fraction
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
29 1/3. You can approximate that as a base 10 fraction::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
30
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 0.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
32
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 or, better, ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
34
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 0.33
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 or, better, ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
38
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 0.333
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
40
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 and so on. No matter how many digits you're willing to write down, the result
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
42 will never be exactly 1/3, but will be an increasingly better approximation of
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 1/3.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 In the same way, no matter how many base 2 digits you're willing to use, the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 2, 1/10 is the infinitely repeating fraction ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
49 0.0001100110011001100110011001100110011001100110011...
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
50
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
51 Stop at any finite number of bits, and you get an approximation. On most
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
52 machines today, floats are approximated using a binary fraction with
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
53 the numerator using the first 53 bits starting with the most significant bit and
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
54 with the denominator as a power of two. In the case of 1/10, the binary fraction
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
55 is ``3602879701896397 / 2 ** 55`` which is close to but not exactly
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
56 equal to the true value of 1/10.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
58 Many users are not aware of the approximation because of the way values are
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
59 displayed. Python only prints a decimal approximation to the true decimal
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
60 value of the binary approximation stored by the machine. On most machines, if
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
61 Python were to print the true decimal value of the binary approximation stored
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
62 for 0.1, it would have to display ::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64 >>> 0.1
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 0.1000000000000000055511151231257827021181583404541015625
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
67 That is more digits than most people find useful, so Python keeps the number
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
68 of digits manageable by displaying a rounded value instead ::
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
69
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
70 >>> 1 / 10
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
71 0.1
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
72
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
73 Just remember, even though the printed result looks like the exact value
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
74 of 1/10, the actual stored value is the nearest representable binary fraction.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
76 Interestingly, there are many different decimal numbers that share the same
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
77 nearest approximate binary fraction. For example, the numbers ``0.1`` and
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
78 ``0.10000000000000001`` and
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
79 ``0.1000000000000000055511151231257827021181583404541015625`` are all
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
80 approximated by ``3602879701896397 / 2 ** 55``. Since all of these decimal
53359
43e46e4e02f0 Remove leftover word.
Georg Brandl <georg@python.org>
parents: 53337
diff changeset
81 values share the same approximation, any one of them could be displayed
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
82 while still preserving the invariant ``eval(repr(x)) == x``.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83
63286
056436065290 Merged revisions 83224 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55589
diff changeset
84 Historically, the Python prompt and built-in :func:`repr` function would choose
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54957
diff changeset
85 the one with 17 significant digits, ``0.10000000000000001``. Starting with
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
86 Python 3.1, Python (on most systems) is now able to choose the shortest of
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
87 these and simply display ``0.1``.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 Note that this is in the very nature of binary floating-point: this is not a bug
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90 in Python, and it is not a bug in your code either. You'll see the same kind of
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91 thing in all languages that support your hardware's floating-point arithmetic
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 (although some languages may not *display* the difference by default, or in all
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 output modes).
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51384
diff changeset
95 Python's built-in :func:`str` function produces only 12 significant digits, and
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96 you may wish to use that instead. It's unusual for ``eval(str(x))`` to
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 reproduce *x*, but the output may be more pleasant to look at::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
98
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
99 >>> str(math.pi)
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
100 '3.14159265359'
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
101
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
102 >>> repr(math.pi)
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
103 '3.141592653589793'
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
104
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
105 >>> format(math.pi, '.2f')
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
106 '3.14'
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
107
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
108 It's important to realize that this is, in a real sense, an illusion: you're
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
109 simply rounding the *display* of the true machine value.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
110
53480
3162eb09c2c3 Remove the round() discussion which is now out-of-date.
Raymond Hettinger <python@rcn.com>
parents: 53359
diff changeset
111 One illusion may beget another. For example, since 0.1 is not exactly 1/10,
53491
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
112 summing three values of 0.1 may not yield exactly 0.3, either::
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
113
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
114 >>> .1 + .1 + .1 == .3
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
115 False
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
116
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
117 Also, since the 0.1 cannot get any closer to the exact value of 1/10 and
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
118 0.3 cannot get any closer to the exact value of 3/10, then pre-rounding with
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
119 :func:`round` function cannot help::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
120
53491
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
121 >>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
122 False
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
123
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
124 Though the numbers cannot be made closer to their intended exact values,
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
125 the :func:`round` function can be useful for post-rounding so that results
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54957
diff changeset
126 with inexact values become comparable to one another::
53491
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
127
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54957
diff changeset
128 >>> round(.1 + .1 + .1, 10) == round(.3, 10)
53491
383e32ab607c Improve the rounding and summing examples.
Raymond Hettinger <python@rcn.com>
parents: 53480
diff changeset
129 True
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
130
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 Binary floating-point arithmetic holds many surprises like this. The problem
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
132 with "0.1" is explained in precise detail below, in the "Representation Error"
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
133 section. See `The Perils of Floating Point <http://www.lahey.com/float.htm>`_
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134 for a more complete account of other common surprises.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 As that says near the end, "there are no easy answers." Still, don't be unduly
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 wary of floating-point! The errors in Python float operations are inherited
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
138 from the floating-point hardware, and on most machines are on the order of no
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
139 more than 1 part in 2\*\*53 per operation. That's more than adequate for most
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54957
diff changeset
140 tasks, but you do need to keep in mind that it's not decimal arithmetic and
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
141 that every float operation can suffer a new rounding error.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
142
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 While pathological cases do exist, for most casual use of floating-point
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 arithmetic you'll see the result you expect in the end if you simply round the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
145 display of your final results to the number of decimal digits you expect.
47283
f28cbab11acd Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 43219
diff changeset
146 :func:`str` usually suffices, and for finer control see the :meth:`str.format`
f28cbab11acd Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 43219
diff changeset
147 method's format specifiers in :ref:`formatstrings`.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
148
49438
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
149 For use cases which require exact decimal representation, try using the
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
150 :mod:`decimal` module which implements decimal arithmetic suitable for
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
151 accounting applications and high-precision applications.
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
152
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
153 Another form of exact arithmetic is supported by the :mod:`fractions` module
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
154 which implements arithmetic based on rational numbers (so the numbers like
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
155 1/3 can be represented exactly).
9e41c5e7e360 Issue 3412: Mention fractions and decimal in the tutorial section on floating point.
Raymond Hettinger <python@rcn.com>
parents: 49437
diff changeset
156
43115
95d6fe2e8e0e Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents: 42604
diff changeset
157 If you are a heavy user of floating point operations you should take a look
95d6fe2e8e0e Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents: 42604
diff changeset
158 at the Numerical Python package and many other packages for mathematical and
95d6fe2e8e0e Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents: 42604
diff changeset
159 statistical operations supplied by the SciPy project. See <http://scipy.org>.
49437
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
160
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
161 Python provides tools that may help on those rare occasions when you really
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
162 *do* want to know the exact value of a float. The
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
163 :meth:`float.as_integer_ratio` method expresses the value of a float as a
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
164 fraction::
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
165
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
166 >>> x = 3.14159
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
167 >>> x.as_integer_ratio()
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54957
diff changeset
168 (3537115888337719, 1125899906842624)
49437
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
169
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
170 Since the ratio is exact, it can be used to losslessly recreate the
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
171 original value::
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
172
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
173 >>> x == 3537115888337719 / 1125899906842624
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
174 True
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
175
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
176 The :meth:`float.hex` method expresses a float in hexadecimal (base
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
177 16), again giving the exact value stored by your computer::
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
178
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
179 >>> x.hex()
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
180 '0x1.921f9f01b866ep+1'
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
181
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
182 This precise hexadecimal representation can be used to reconstruct
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
183 the float value exactly::
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
184
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
185 >>> x == float.fromhex('0x1.921f9f01b866ep+1')
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
186 True
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
187
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
188 Since the representation is exact, it is useful for reliably porting values
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
189 across different versions of Python (platform independence) and exchanging
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
190 data with other languages that support the same format (such as Java and C99).
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
191
53494
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
192 Another helpful tool is the :func:`math.fsum` function which helps mitigate
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
193 loss-of-precision during summation. It tracks "lost digits" as values are
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
194 added onto a running total. That can make a difference in overall accuracy
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
195 so that the errors do not accumulate to the point where they affect the
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
196 final total:
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
197
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
198 >>> sum([0.1] * 10) == 1.0
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
199 False
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
200 >>> math.fsum([0.1] * 10) == 1.0
caf2b91ef06a Add link to math.fsum().
Raymond Hettinger <python@rcn.com>
parents: 53491
diff changeset
201 True
49437
c0478651460f Issue 3288: document as_integer_ratio(), fromhex(), and hex().
Raymond Hettinger <python@rcn.com>
parents: 48677
diff changeset
202
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
203 .. _tut-fp-error:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
205 Representation Error
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 ====================
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
207
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 This section explains the "0.1" example in detail, and shows how you can perform
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
209 an exact analysis of cases like this yourself. Basic familiarity with binary
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 floating-point representation is assumed.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
211
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 :dfn:`Representation error` refers to the fact that some (most, actually)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 decimal fractions cannot be represented exactly as binary (base 2) fractions.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
215 others) often won't display the exact decimal number you expect.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
216
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
217 Why is that? 1/10 is not exactly representable as a binary fraction. Almost all
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 machines today (November 2000) use IEEE-754 floating point arithmetic, and
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
219 almost all platforms map Python floats to IEEE-754 "double precision". 754
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
220 doubles contain 53 bits of precision, so on input the computer strives to
51384
7a0525613644 Merged revisions 69350 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49438
diff changeset
221 convert 0.1 to the closest fraction it can of the form *J*/2**\ *N* where *J* is
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
222 an integer containing exactly 53 bits. Rewriting ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
223
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 1 / 10 ~= J / (2**N)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
225
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 as ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
227
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
228 J ~= 2**N / 10
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
229
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
230 and recalling that *J* has exactly 53 bits (is ``>= 2**52`` but ``< 2**53``),
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
231 the best value for *N* is 56::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
232
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
233 >>> 2**52 <= 2**56 // 10 < 2**53
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
234 True
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 That is, 56 is the only value for *N* that leaves *J* with exactly 53 bits. The
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
237 best possible value for *J* is then that quotient rounded::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
238
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
239 >>> q, r = divmod(2**56, 10)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 >>> r
48677
6e4f47a69efa Remove long integer output.
Georg Brandl <georg@python.org>
parents: 47283
diff changeset
241 6
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
242
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
243 Since the remainder is more than half of 10, the best approximation is obtained
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
244 by rounding up::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
245
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
246 >>> q+1
48677
6e4f47a69efa Remove long integer output.
Georg Brandl <georg@python.org>
parents: 47283
diff changeset
247 7205759403792794
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
248
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
249 Therefore the best possible approximation to 1/10 in 754 double precision is::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
250
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
251 7205759403792794 / 2 ** 56
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
252
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
253 Dividing both the numerator and denominator by two reduces the fraction to::
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
254
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
255 3602879701896397 / 2 ** 55
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
256
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
257 Note that since we rounded up, this is actually a little bit larger than 1/10;
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
258 if we had not rounded up, the quotient would have been a little bit smaller than
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
259 1/10. But in no case can it be *exactly* 1/10!
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
260
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
261 So the computer never "sees" 1/10: what it sees is the exact fraction given
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
262 above, the best 754 double approximation it can get::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
263
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
264 >>> 0.1 * 2 ** 55
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
265 3602879701896397.0
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
266
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
267 If we multiply that fraction by 10\*\*55, we can see the value out to
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
268 55 decimal digits::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
269
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
270 >>> 3602879701896397 * 10 ** 55 // 2 ** 55
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
271 1000000000000000055511151231257827021181583404541015625
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
272
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
273 meaning that the exact number stored in the computer is equal to
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
274 the decimal value 0.1000000000000000055511151231257827021181583404541015625.
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
275 Instead of displaying the full decimal value, many languages (including
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
276 older versions of Python), round the result to 17 significant digits::
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
277
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
278 >>> format(0.1, '.17f')
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
279 '0.10000000000000001'
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
280
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
281 The :mod:`fractions` and :mod:`decimal` modules make these calculations
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
282 easy::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
283
53337
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
284 >>> from decimal import Decimal
8300b274fdd3 Update for the new float.__repr__()
Raymond Hettinger <python@rcn.com>
parents: 51776
diff changeset
285 >>> from fractions import Fraction
54957
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
286
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
287 >>> Fraction.from_float(0.1)
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
288 Fraction(3602879701896397, 36028797018963968)
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
289
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
290 >>> (0.1).as_integer_ratio()
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
291 (3602879701896397, 36028797018963968)
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
292
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
293 >>> Decimal.from_float(0.1)
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
294 Decimal('0.1000000000000000055511151231257827021181583404541015625')
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
295
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
296 >>> format(Decimal.from_float(0.1), '.17')
380af9e80c2e Clean-up floating point tutorial
Raymond Hettinger <python@rcn.com>
parents: 53494
diff changeset
297 '0.10000000000000001'