Document floating-point arithmetic
[gromacs.git] / docs / user-guide / floating-point.rst
blob387df4afc622e5623bb897a53719b0d1f0a45eae
1 Floating point arithmetic
2 =========================
4 |Gromacs| spends its life doing arithmetic on real numbers, often summing many
5 millions of them. These real numbers are encoded on computers in so-called
6 binary floating-point representation. This representation is somewhat like
7 scientific exponential notation (but uses binary rather than decimal), and is
8 necessary for the fastest possible speed for calculations. Unfortunately the
9 laws of algebra only approximately apply to binary floating-point. In part,
10 this is because some real numbers that are represented simply and exactly in
11 decimal (like 1/5=0.2) have no exact representation in binary floating-point,
12 just as 1/3 cannot be represented in decimal. There are many sources you can
13 find with a search engine that discuss this issue more exhaustively, such as
14 `Wikipedia <https://en.wikipedia.org/wiki/Floating-point_arithmetic>`__ and
15 David Goldberg's 1991 paper *What every computer scientist should know about
16 floating-point arithmetic* (`article <https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html>`__,
17 `addendum <https://docs.oracle.com/cd/E37069_01/html/E39019/z400228248508.html>`__).
18 Bruce Dawson also has a written a number of very valuable blog posts on modern
19 floating-point programming at his
20 `Random ASCII site <https://randomascii.wordpress.com/category/floating-point/>`__
21 that are worth reading.
23 So, the sum of a large number of binary representations of exact decimal
24 numbers need not equal the expected algebraic or decimal result. Users observe
25 this phenomenon in sums of partial charges expressed to two decimal places that
26 sometimes only approximate the integer total charge to which they contribute
27 (however a deviation in the first decimal place would always be indicative of a
28 badly-formed topology).  When |Gromacs| has to represent such floating-point
29 numbers in output, it sometimes uses a computer form of scientific notation
30 known as E notation. In such notation, a number like -9.999971e-01 is actually
31 -0.9999971, which is close enough to -1 for purposes of assessing the total
32 charge of a system.
34 It is also not appropriate for |Gromacs| to guess to round things, because such
35 rounding relies on assumptions about the inputs that need not be true. Instead
36 the user needs to understand how their tools work.