Skip to content

Commit bd2cc41

Browse files
[3.12] gh-104479: Update outdated tutorial floating-point reference (GH-104681) (#104960)
(cherry picked from commit 2cf04e4) Co-authored-by: Mark Dickinson <[email protected]>
1 parent 8010cef commit bd2cc41

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Doc/tutorial/floatingpoint.rst

+13-10
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Binary floating-point arithmetic holds many surprises like this. The problem
148148
with "0.1" is explained in precise detail below, in the "Representation Error"
149149
section. See `Examples of Floating Point Problems
150150
<https://jvns.ca/blog/2023/01/13/examples-of-floating-point-problems/>`_ for
151-
a pleasant summary of how binary floating point works and the kinds of
151+
a pleasant summary of how binary floating-point works and the kinds of
152152
problems commonly encountered in practice. Also see
153153
`The Perils of Floating Point <https://www.lahey.com/float.htm>`_
154154
for a more complete account of other common surprises.
@@ -174,7 +174,7 @@ Another form of exact arithmetic is supported by the :mod:`fractions` module
174174
which implements arithmetic based on rational numbers (so the numbers like
175175
1/3 can be represented exactly).
176176

177-
If you are a heavy user of floating point operations you should take a look
177+
If you are a heavy user of floating-point operations you should take a look
178178
at the NumPy package and many other packages for mathematical and
179179
statistical operations supplied by the SciPy project. See <https://scipy.org>.
180180

@@ -268,12 +268,14 @@ decimal fractions cannot be represented exactly as binary (base 2) fractions.
268268
This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many
269269
others) often won't display the exact decimal number you expect.
270270

271-
Why is that? 1/10 is not exactly representable as a binary fraction. Almost all
272-
machines today (November 2000) use IEEE-754 floating point arithmetic, and
273-
almost all platforms map Python floats to IEEE-754 "double precision". 754
274-
doubles contain 53 bits of precision, so on input the computer strives to
275-
convert 0.1 to the closest fraction it can of the form *J*/2**\ *N* where *J* is
276-
an integer containing exactly 53 bits. Rewriting ::
271+
Why is that? 1/10 is not exactly representable as a binary fraction. Since at
272+
least 2000, almost all machines use IEEE 754 binary floating-point arithmetic,
273+
and almost all platforms map Python floats to IEEE 754 binary64 "double
274+
precision" values. IEEE 754 binary64 values contain 53 bits of precision, so
275+
on input the computer strives to convert 0.1 to the closest fraction it can of
276+
the form *J*/2**\ *N* where *J* is an integer containing exactly 53 bits.
277+
Rewriting
278+
::
277279

278280
1 / 10 ~= J / (2**N)
279281

@@ -308,7 +310,8 @@ by rounding up:
308310
>>> q+1
309311
7205759403792794
310312

311-
Therefore the best possible approximation to 1/10 in 754 double precision is::
313+
Therefore the best possible approximation to 1/10 in IEEE 754 double precision
314+
is::
312315

313316
7205759403792794 / 2 ** 56
314317

@@ -321,7 +324,7 @@ if we had not rounded up, the quotient would have been a little bit smaller than
321324
1/10. But in no case can it be *exactly* 1/10!
322325

323326
So the computer never "sees" 1/10: what it sees is the exact fraction given
324-
above, the best 754 double approximation it can get:
327+
above, the best IEEE 754 double approximation it can get:
325328

326329
.. doctest::
327330

0 commit comments

Comments
 (0)