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