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

Skip to content

Commit 417d667

Browse files
committed
Text from Tim & Guido discussing floating point arithmetic and what users
need to understand about the binary & decimal fp, so that representation weirdness is documented somewhere. This makes it easier to repond to "bug" reports caused by user confusion & ignorance of the issues. This closes SF patch #426208.
1 parent 1dc98c4 commit 417d667

1 file changed

Lines changed: 265 additions & 0 deletions

File tree

Doc/tut/tut.tex

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,4 +4085,269 @@ \section{Commentary \label{commentary}}
40854085
be useful.
40864086
40874087
4088+
\chapter{Floating Point Arithmetic: Issues and Limitations
4089+
\label{fp-issues}}
4090+
4091+
Floating-point numbers are represented in computer hardware as
4092+
base 2 (binary) fractions. For example, the decimal fraction
4093+
4094+
\begin{verbatim}
4095+
0.125
4096+
\end{verbatim}
4097+
4098+
has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction
4099+
4100+
\begin{verbatim}
4101+
0.001
4102+
\end{verbatim}
4103+
4104+
has value 0/2 + 0/4 + 1/8. These two fractions have identical values,
4105+
the only real difference being that the first is written in base 10
4106+
fractional notation, and the second in base 2.
4107+
4108+
Unfortunately, most decimal fractions cannot be represented exactly as
4109+
binary fractions. A consequence is that, in general, the decimal
4110+
floating-point numbers you enter are only approximated by the binary
4111+
floating-point numbers actually stored in the machine.
4112+
4113+
The problem is easier to understand at first in base 10. Consider the
4114+
fraction 1/3. You can approximate that as a base 10 fraction:
4115+
4116+
\begin{verbatim}
4117+
0.3
4118+
\end{verbatim}
4119+
4120+
or, better,
4121+
4122+
\begin{verbatim}
4123+
0.33
4124+
\end{verbatim}
4125+
4126+
or, better,
4127+
4128+
\begin{verbatim}
4129+
0.333
4130+
\end{verbatim}
4131+
4132+
and so on. No matter how many digits you're willing to write down, the
4133+
result will never be exactly 1/3, but will be an increasingly better
4134+
approximation to 1/3.
4135+
4136+
In the same way, no matter how many base 2 digits you're willing to
4137+
use, the decimal value 0.1 cannot be represented exactly as a base 2
4138+
fraction. In base 2, 1/10 is the infinitely repeating fraction
4139+
4140+
\begin{verbatim}
4141+
0.0001100110011001100110011001100110011001100110011...
4142+
\end{verbatim}
4143+
4144+
Stop at any finite number of bits, and you get an approximation. This
4145+
is why you see things like:
4146+
4147+
\begin{verbatim}
4148+
>>> 0.1
4149+
0.10000000000000001
4150+
\end{verbatim}
4151+
4152+
On most machines today, that is what you'll see if you enter 0.1 at
4153+
a Python prompt. You may not, though, because the number of bits
4154+
used by the hardware to store floating-point values can vary across
4155+
machines, and Python only prints a decimal approximation to the true
4156+
decimal value of the binary approximation stored by the machine. On
4157+
most machines, if Python were to print the true decimal value of
4158+
the binary approximation stored for 0.1, it would have to display
4159+
4160+
\begin{verbatim}
4161+
>>> 0.1
4162+
0.1000000000000000055511151231257827021181583404541015625
4163+
\end{verbatim}
4164+
4165+
instead! The Python prompt (implicitly) uses the builtin
4166+
\function{repr()} function to obtain a string version of everything it
4167+
displays. For floats, \code{repr(\var{float})} rounds the true
4168+
decimal value to 17 significant digits, giving
4169+
4170+
\begin{verbatim}
4171+
0.10000000000000001
4172+
\end{verbatim}
4173+
4174+
\code{repr(\var{float})} produces 17 significant digits because it
4175+
turns out that's enough (on most machines) so that
4176+
\code{eval(repr(\var{x})) == \var{x}} exactly for all finite floats
4177+
\var{x}, but rounding to 16 digits is not enough to make that true.
4178+
4179+
Note that this is in the very nature of binary floating-point: this is
4180+
not a bug in Python, it is not a bug in your code either, and you'll
4181+
see the same kind of thing in all languages that support your
4182+
hardware's floating-point arithmetic.
4183+
4184+
Python's builtin \function{str()} function produces only 12
4185+
significant digits, and you may wish to use that instead. It's
4186+
unusual for \code{eval(str(\var{x}))} to reproduce \var{x}, but the
4187+
output may be more pleasant to look at:
4188+
4189+
\begin{verbatim}
4190+
>>> print str(0.1)
4191+
0.1
4192+
\end{verbatim}
4193+
4194+
It's important to realize that this is, in a real sense, an illusion:
4195+
the value in the machine is not exactly 1/10, you're simply rounding
4196+
the \emph{display} of the true machine value.
4197+
4198+
Other surprises follow from this one. For example, after seeing
4199+
4200+
\begin{verbatim}
4201+
>>> 0.1
4202+
0.10000000000000001
4203+
\end{verbatim}
4204+
4205+
you may be tempted to use the \function{round()} function to chop it
4206+
back to the single digit you expect. But that makes no difference:
4207+
4208+
\begin{verbatim}
4209+
>>> round(0.1, 1)
4210+
0.10000000000000001
4211+
\end{verbatim}
4212+
4213+
The problem is that the binary floating-point value stored for "0.1"
4214+
was already the best possible binary approximation to 1/10, so trying
4215+
to round it again can't make it better: it was already as good as it
4216+
gets.
4217+
4218+
Another consequence is that since 0.1 is not exactly 1/10, adding 0.1
4219+
to itself 10 times may not yield exactly 1.0, either:
4220+
4221+
\begin{verbatim}
4222+
>>> sum = 0.0
4223+
>>> for i in range(10):
4224+
... sum += 0.1
4225+
...
4226+
>>> sum
4227+
0.99999999999999989
4228+
\end{verbatim}
4229+
4230+
Binary floating-point arithmetic holds many surprises like this. The
4231+
problem with "0.1" is explained in precise detail below, in the
4232+
"Representation Error" section. See
4233+
\citetitle[http://www.lahey.com/float.htm]{The Perils of Floating
4234+
Point} for a more complete account of other common surprises.
4235+
4236+
As that says near the end, ``there are no easy answers.'' Still,
4237+
don't be unduly wary of floating-point! The errors in Python float
4238+
operations are inherited from the floating-point hardware, and on most
4239+
machines are on the order of no more than 1 part in 2**53 per
4240+
operation. That's more than adequate for most tasks, but you do need
4241+
to keep in mind that it's not decimal arithmetic, and that every float
4242+
operation can suffer a new rounding error.
4243+
4244+
While pathological cases do exist, for most casual use of
4245+
floating-point arithmetic you'll see the result you expect in the end
4246+
if you simply round the display of your final results to the number of
4247+
decimal digits you expect. \function{str()} usually suffices, and for
4248+
finer control see the discussion of Pythons's \code{\%} format
4249+
operator: the \code{\%g}, \code{\%f} and \code{\%e} format codes
4250+
supply flexible and easy ways to round float results for display.
4251+
4252+
4253+
\section{Representation Error
4254+
\label{fp-error}}
4255+
4256+
This section explains the ``0.1'' example in detail, and shows how
4257+
you can perform an exact analysis of cases like this yourself. Basic
4258+
familiarity with binary floating-point representation is assumed.
4259+
4260+
\dfn{Representation error} refers to that some (most, actually)
4261+
decimal fractions cannot be represented exactly as binary (base 2)
4262+
fractions. This is the chief reason why Python (or Perl, C, \Cpp,
4263+
Java, Fortran, and many others) often won't display the exact decimal
4264+
number you expect:
4265+
4266+
\begin{verbatim}
4267+
>>> 0.1
4268+
0.10000000000000001
4269+
\end{verbatim}
4270+
4271+
Why is that? 1/10 is not exactly representable as a binary fraction.
4272+
Almost all machines today (November 2000) use IEEE-754 floating point
4273+
arithmetic, and almost all platforms map Python floats to IEEE-754
4274+
"double precision". 754 doubles contain 53 bits of precision, so on
4275+
input the computer strives to convert 0.1 to the closest fraction it can
4276+
of the form \var{J}/2**\var{N} where \var{J} is an integer containing
4277+
exactly 53 bits. Rewriting
4278+
4279+
\begin{verbatim}
4280+
1 / 10 ~= J / (2**N)
4281+
\end{verbatim}
4282+
4283+
as
4284+
4285+
\begin{verbatim}
4286+
J ~= 2**N / 10
4287+
\end{verbatim}
4288+
4289+
and recalling that \var{J} has exactly 53 bits (is \code{>= 2**52} but
4290+
\code{< 2**53}), the best value for \var{N} is 56:
4291+
4292+
\begin{verbatim}
4293+
>>> 2L**52
4294+
4503599627370496L
4295+
>>> 2L**53
4296+
9007199254740992L
4297+
>>> 2L**56/10
4298+
7205759403792793L
4299+
\end{verbatim}
4300+
4301+
That is, 56 is the only value for \var{N} that leaves \var{J} with
4302+
exactly 53 bits. The best possible value for \var{J} is then that
4303+
quotient rounded:
4304+
4305+
\begin{verbatim}
4306+
>>> q, r = divmod(2L**56, 10)
4307+
>>> r
4308+
6L
4309+
\end{verbatim}
4310+
4311+
Since the remainder is more than half of 10, the best approximation is
4312+
obtained by rounding up:
4313+
4314+
\begin{verbatim}
4315+
>>> q+1
4316+
7205759403792794L
4317+
\end{verbatim}
4318+
4319+
Therefore the best possible approximation to 1/10 in 754 double
4320+
precision is that over 2**56, or
4321+
4322+
\begin{verbatim}
4323+
7205759403792794 / 72057594037927936
4324+
\end{verbatim}
4325+
4326+
Note that since we rounded up, this is actually a little bit larger than
4327+
1/10; if we had not rounded up, the quotient would have been a little
4328+
bit smaller than 1/10. But in no case can it be *exactly* 1/10!
4329+
4330+
So the computer never ``sees'' 1/10: what it sees is the exact
4331+
fraction given above, the best 754 double approximation it can get:
4332+
4333+
\begin{verbatim}
4334+
>>> .1 * 2L**56
4335+
7205759403792794.0
4336+
\end{verbatim}
4337+
4338+
If we multiply that fraction by 10**30, we can see the (truncated)
4339+
value of its 30 most significant decimal digits:
4340+
4341+
\begin{verbatim}
4342+
>>> 7205759403792794L * 10L**30 / 2L**56
4343+
100000000000000005551115123125L
4344+
\end{verbatim}
4345+
4346+
meaning that the exact number stored in the computer is approximately
4347+
equal to the decimal value 0.100000000000000005551115123125. Rounding
4348+
that to 17 significant digits gives the 0.10000000000000001 that Python
4349+
displays (well, will display on any 754-conforming platform that does
4350+
best-possible input and output conversions in its C library --- yours may
4351+
not!).
4352+
40884353
\end{document}

0 commit comments

Comments
 (0)