Debian Codegraph @

texmf-dist/doc/metapost/base/source-manual/mpman-app-numbersystems.tex

raw
1 \svnInfo $Id$
2 \section{High-precision and interval arithmetic with MetaPost}
3 \label{hparith}
4
5 In addition to the fixed-point arithmetics inherited from \MF, MetaPost
6 can also do higher-precision arithmetics. In total, MetaPost can handle
7 numeric quantities in five internal representation formats or number
8 systems. Number systems differ in rounding errors\index{rounding error}
9 introduced by and the speed of arithmetic operations. Simply storing a
10 numeric value in a variable may already introduce a rounding error, so
11 that can already be considered an arithmetic operation.
12
13 The internal representation format used for numeric quantities can be
14 determined by a command-line switch
15 \texttt{-numbersystem}\index{command-line!mpost?\texttt{mpost}!-numbersystem?\texttt{-numbersystem}}
16 when invoking the MetaPost executable. Argument is a string and can be
17 one of \texttt{scaled}, \texttt{double}, \texttt{binary}, \texttt{interval} or
18 \texttt{decimal}. The argument is stored in an internal string variable
19 \texttt{numbersystem}\index{numbersystem?\texttt{numbersystem}}\label{Dnumbersystem}.
20 Assigning a value to this variable at run-time triggers an error.
21
22 The \texttt{scaled}\index{scaled?\texttt{scaled}} number system refers
23 to 32~bit fixed-point arithmetics described in Section~\ref{datatypes}.
24 This is the default number system. Precision is ca.~10~decimal digits,
25 5 digits before and after the comma. All arithmetic operations are done
26 in software.
27
28 The \texttt{double}\index{double?\texttt{double}} number system does
29 IEEE standard floating-point arithmetics with 64~bits (or double)
30 precision. In the internal representation, double floating-point
31 numbers use $52+1$~bits for the mantissa, which determines precision,
32 11~bits for the exponent, which determines the valid range of numbers,
33 and one bit for the sign. The smallest absolute value that can be
34 represented is ca.~$2.2\cdot10^{-308}$, the largest value is
35 ca.~$1.8\cdot10^{308}$. The 53~bit mantissa makes for a precision of
36 ca.~15 decimal digits. The smallest possible difference between two
37 distinct numbers in double floating-point number representation is
38 $2^{-53} \approx 1.1\cdot10^{-16}$. The largest integer value that can
39 be represented exactly is $2^{53}-1 \approx 9,0\cdot10^{15}$. Variable
40 \texttt{warningcheck}\index{warningcheck?\texttt{warningcheck}} is set
41 to $2^{52}$ in \texttt{double} mode. Arithmetic operations make use of
42 a hardware floating-point unit (FPU), if available.
43
44 While the IEEE double precision floating-point format provides plenty
45 room for storing numeric values, still, precision and range are finite
46 and fix. For users that need higher precision or range, MetaPost
47 provides support for (almost) arbitrary precision floating-point
48 arithmetics. The \texttt{binary}\index{binary?\texttt{binary}} number
49 system is similar to the \texttt{double} number system, except that the
50 number of bits used for the mantissa is not fixed, but variable.
51 Precision is determined by an internal variable
52 \texttt{numberprecision}\index{numberprecision?\texttt{numberprecision}}\label{Dnumberprecision}
53 in decimal digits. Valid numbers are in the range 1 to 1000. Higher
54 values make for better precision at the expense of performance of
55 arithmetic operations. Default precision is 34~decimal digits
56 (ca.~113~bits in the mantissa). Exponent in the internal representation
57 is an integer in the range $[-9,999,999; +9,999,999]$. All arithmetic
58 operations are done in software using the MPFR library~\cite{lib:mpfr}
59 and are usually orders of magnitude slower than in \texttt{double} mode.
60
61 The \index{interval?interval number system}\texttt{interval} number
62 system MPFI~\cite{lib:mpfi}, introduced in version \texttt{2.10}, is built on the top of the MPFR library
63 and an interval quantity \texttt{[}\textit{number}\texttt{,}\textit{number}\texttt{]} is created with
64 \begin{center}\index{interval\_set?\texttt{interval\_set}}\label{Dintervalset}\texttt{w := interval\_set(}\textit{number}\texttt{,}\textit{number}\texttt{);}\end{center}
65 An interval is a set, the left extrema being
66 \begin{center}\index{interval\_get\_left\_endpoint?\texttt{interval\_get\_left\_endpoint}}\label{Dintervalleft}\texttt{l := interval\_get\_left\_endpoint(}\textit{interval}\texttt{);}\end{center}
67 and the right extrema
68 \begin{center}\index{interval\_get\_right\_endpoint?\texttt{interval\_get\_right\_endpoint}}\label{Dintervalright}\texttt{r := interval\_get\_right\_endpoint(}\textit{interval}\texttt{);}\end{center}
69 and it's always $l\leq r$\,; a number \texttt{x} is always seen as the interval \texttt{[x,x]}, i.e. a set with one element.
70 At first glance, interval arithmetic can be confusing: if $w$ is an interval,
71 $w*w$ is the set $\left\{ x*y: x\in w, y\in w\right\}$, but $f(w)=w^2$ is the set $\left\{f(x): x\in w\right\} = \left\{x^2: x\in w\right\}$:
72 if $w=[-1,1]$ then $w*w=[-1,1]$ and $w^2=[0,1]$. On the other hand, it's easy to verify that $w*w*w = w^3$ always. For this reason,
73 given that currently the
74 implementation of the MetaPost functions in MPFI mimics those of MPFR and they are not adapted to the interval arithmetic,
75 the main utility of interval mode is the implementation of algorithms in MetaPost language.
76
77
78
79
80
81 Number system \texttt{decimal}\index{decimal?\texttt{decimal}} provides
82 arbitrary precision floating-point arithmetics similar to the
83 \texttt{binary} number system. Except that it uses a base of~10 for the
84 internal representation instead of a base of~2. The point is that with
85 base~2 floating-point numbers some decimal numbers cannot be represented
86 exactly, among them such strange numbers like $0.1$. In a base~2
87 floating-point number format, this value has an infinit repeating
88 representation, which cannot be stored in a mantissa of finite precision
89 without introducing a rounding error. While such initial errors may be
90 small, they use to accummulate when doing calculations. Sometimes
91 increasing precision by switching from \texttt{double} to
92 \texttt{binary} mode is sufficient to get satisfying results again. On
93 the other hand, there's a demand for doing calculations exactly like
94 humans do with pencil and paper, e.g., for certain financial
95 calculations. The only way to ensure exact result is switching base of
96 the internal representation to~10. Again, precision can be determined
97 by assigning a value to variable
98 \texttt{numberprecision}\index{numberprecision?\texttt{numberprecision}}.
99 Valid numbers are in the range 1 to 1000. Default precision is
100 34~decimal digits. Exponent in the internal representation is an
101 integer in the range $[-9,999,999; +9,999,999]$. All arithmetic
102 operations are done in software using the decNumber
103 library~\cite{lib:decnumber} and are usually slower than in
104 \texttt{binary} mode.
105
106 In all number systems except the traditional fixed-point
107 (\texttt{scaled}) number system, numbers can be given in scientific
108 notation, i.e., input like \texttt{1.23e4} is interpreted as the value
109 $12,300$ instead of the product of the numeric value $1.23$ and (array)
110 variable \verb|e[4]|.
111
112
113
114 %%% Local Variables:
115 %%% mode: latex
116 %%% TeX-PDF-mode: t
117 %%% TeX-master: "mpman"
118 %%% End: