1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Machine Epsilon (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Machine Epsilon (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Machine Epsilon (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Floating-Point-in-Depth.html" rel="up" title="Floating Point in Depth">
|
---|
33 | <link href="Complex-Arithmetic.html" rel="next" title="Complex Arithmetic">
|
---|
34 | <link href="Rounding-Control.html" rel="prev" title="Rounding Control">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Machine-Epsilon"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Complex-Arithmetic.html" accesskey="n" rel="next">Complex Arithmetic</a>, Previous: <a href="Rounding-Control.html" accesskey="p" rel="prev">Rounding Control</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Machine-Epsilon-1"></span><h3 class="section">28.18 Machine Epsilon</h3>
|
---|
64 | <span id="index-machine-epsilon-_0028floating-point_0029"></span>
|
---|
65 | <span id="index-floating_002dpoint-machine-epsilon"></span>
|
---|
66 |
|
---|
67 | <p>In any floating-point system, three attributes are particularly
|
---|
68 | important to know: <em>base</em> (the number that the exponent specifies
|
---|
69 | a power of), <em>precision</em> (number of digits in the significand),
|
---|
70 | and <em>range</em> (difference between most positive and most negative
|
---|
71 | values). The allocation of bits between exponent and significand
|
---|
72 | decides the answers to those questions.
|
---|
73 | </p>
|
---|
74 | <p>A measure of the precision is the answer to the question: what is
|
---|
75 | the smallest number that can be added to <code>1.0</code> such that the
|
---|
76 | sum differs from <code>1.0</code>? That number is called the
|
---|
77 | <em>machine epsilon</em>.
|
---|
78 | </p>
|
---|
79 | <p>We could define the needed machine-epsilon constants for <code>float</code>,
|
---|
80 | <code>double</code>, and <code>long double</code> like this:
|
---|
81 | </p>
|
---|
82 | <div class="example">
|
---|
83 | <pre class="example">static const float epsf = 0x1p-23; /* <span class="roman">about 1.192e-07</span> */
|
---|
84 | static const double eps = 0x1p-52; /* <span class="roman">about 2.220e-16</span> */
|
---|
85 | static const long double epsl = 0x1p-63; /* <span class="roman">about 1.084e-19</span> */
|
---|
86 | </pre></div>
|
---|
87 |
|
---|
88 | <p>Instead of the hexadecimal constants, we could also have used the
|
---|
89 | Standard C macros, <code>FLT_EPSILON</code>, <code>DBL_EPSILON</code>, and
|
---|
90 | <code>LDBL_EPSILON</code>.
|
---|
91 | </p>
|
---|
92 | <p>It is useful to be able to compute the machine epsilons at
|
---|
93 | run time, and we can easily generalize the operation by replacing
|
---|
94 | the constant <code>1.0</code> with a user-supplied value:
|
---|
95 | </p>
|
---|
96 | <div class="example">
|
---|
97 | <pre class="example">double
|
---|
98 | macheps (double x)
|
---|
99 | { /* <span class="roman">Return machine epsilon for <var>x</var>,</span> */
|
---|
100 | <span class="roman">such that <var>x</var> + macheps (<var>x</var>) > <var>x</var>.</span> */
|
---|
101 | static const double base = 2.0;
|
---|
102 | double eps;
|
---|
103 |
|
---|
104 | if (isnan (x))
|
---|
105 | eps = x;
|
---|
106 | else
|
---|
107 | {
|
---|
108 | eps = (x == 0.0) ? 1.0 : x;
|
---|
109 |
|
---|
110 | while ((x + eps / base) != x)
|
---|
111 | eps /= base; /* <span class="roman">Always exact!</span> */
|
---|
112 | }
|
---|
113 |
|
---|
114 | return (eps);
|
---|
115 | }
|
---|
116 | </pre></div>
|
---|
117 |
|
---|
118 | <p>If we call that function with arguments from <code>0</code> to
|
---|
119 | <code>10</code>, as well as Infinity and NaN, and print the returned
|
---|
120 | values in hexadecimal, we get output like this:
|
---|
121 | </p>
|
---|
122 | <div class="example">
|
---|
123 | <pre class="example">macheps ( 0) = 0x1.0000000000000p-1074
|
---|
124 | macheps ( 1) = 0x1.0000000000000p-52
|
---|
125 | macheps ( 2) = 0x1.0000000000000p-51
|
---|
126 | macheps ( 3) = 0x1.8000000000000p-52
|
---|
127 | macheps ( 4) = 0x1.0000000000000p-50
|
---|
128 | macheps ( 5) = 0x1.4000000000000p-51
|
---|
129 | macheps ( 6) = 0x1.8000000000000p-51
|
---|
130 | macheps ( 7) = 0x1.c000000000000p-51
|
---|
131 | macheps ( 8) = 0x1.0000000000000p-49
|
---|
132 | macheps ( 9) = 0x1.2000000000000p-50
|
---|
133 | macheps ( 10) = 0x1.4000000000000p-50
|
---|
134 | macheps (Inf) = infinity
|
---|
135 | macheps (NaN) = nan
|
---|
136 | </pre></div>
|
---|
137 |
|
---|
138 | <p>Notice that <code>macheps</code> has a special test for a NaN to prevent an
|
---|
139 | infinite loop.
|
---|
140 | </p>
|
---|
141 |
|
---|
142 | <p>Our code made another test for a zero argument to avoid getting a
|
---|
143 | zero return. The returned value in that case is the smallest
|
---|
144 | representable floating-point number, here the subnormal value
|
---|
145 | <code>2**(-1074)</code>, which is about <code>4.941e-324</code>.
|
---|
146 | </p>
|
---|
147 | <p>No special test is needed for an Infinity, because the
|
---|
148 | <code>eps</code>-reduction loop then terminates at the first iteration.
|
---|
149 | </p>
|
---|
150 | <p>Our <code>macheps</code> function here assumes binary floating point; some
|
---|
151 | architectures may differ.
|
---|
152 | </p>
|
---|
153 | <p>The C library includes some related functions that can also be used to
|
---|
154 | determine machine epsilons at run time:
|
---|
155 | </p>
|
---|
156 | <div class="example">
|
---|
157 | <pre class="example">#include <math.h> /* <span class="roman">Include for these prototypes.</span> */
|
---|
158 |
|
---|
159 | double nextafter (double x, double y);
|
---|
160 | float nextafterf (float x, float y);
|
---|
161 | long double nextafterl (long double x, long double y);
|
---|
162 | </pre></div>
|
---|
163 |
|
---|
164 | <p>These return the machine number nearest <var>x</var> in the direction of
|
---|
165 | <var>y</var>. For example, <code>nextafter (1.0, 2.0)</code> produces the same
|
---|
166 | result as <code>1.0 + macheps (1.0)</code> and <code>1.0 + DBL_EPSILON</code>.
|
---|
167 | See <a href="https://www.gnu.org/software/libc/manual/html_node/FP-Bit-Twiddling.html#FP-Bit-Twiddling">FP Bit Twiddling</a> in <cite>The GNU C Library Reference Manual</cite>.
|
---|
168 | </p>
|
---|
169 | <p>It is important to know that the machine epsilon is not symmetric
|
---|
170 | about all numbers. At the boundaries where normalization changes the
|
---|
171 | exponent, the epsilon below <var>x</var> is smaller than that just above
|
---|
172 | <var>x</var> by a factor <code>1 / base</code>. For example, <code>macheps
|
---|
173 | (1.0)</code> returns <code>+0x1p-52</code>, whereas <code>macheps (-1.0)</code> returns
|
---|
174 | <code>+0x1p-53</code>. Some authors distinguish those cases by calling them
|
---|
175 | the <em>positive</em> and <em>negative</em>, or <em>big</em> and
|
---|
176 | <em>small</em>, machine epsilons. You can produce their values like
|
---|
177 | this:
|
---|
178 | </p>
|
---|
179 | <div class="example">
|
---|
180 | <pre class="example">eps_neg = 1.0 - nextafter (1.0, -1.0);
|
---|
181 | eps_pos = nextafter (1.0, +2.0) - 1.0;
|
---|
182 | </pre></div>
|
---|
183 |
|
---|
184 | <p>If <var>x</var> is a variable, such that you do not know its value at
|
---|
185 | compile time, then you can substitute literal <var>y</var> values with
|
---|
186 | either <code>-inf()</code> or <code>+inf()</code>, like this:
|
---|
187 | </p>
|
---|
188 | <div class="example">
|
---|
189 | <pre class="example">eps_neg = x - nextafter (x, -inf ());
|
---|
190 | eps_pos = nextafter (x, +inf() - x);
|
---|
191 | </pre></div>
|
---|
192 |
|
---|
193 | <p>In such cases, if <var>x</var> is Infinity, then <em>the <code>nextafter</code>
|
---|
194 | functions return <var>y</var> if <var>x</var> equals <var>y</var></em>. Our two
|
---|
195 | assignments then produce <code>+0x1.fffffffffffffp+1023</code> (about
|
---|
196 | 1.798e+308) for <var>eps_neg</var> and Infinity for <var>eps_pos</var>. Thus,
|
---|
197 | the call <code>nextafter (INFINITY, -INFINITY)</code> can be used to find
|
---|
198 | the largest representable finite number, and with the call
|
---|
199 | <code>nextafter (0.0, 1.0)</code>, the smallest representable number (here,
|
---|
200 | <code>0x1p-1074</code> (about 4.491e-324), a number that we saw before as
|
---|
201 | the output from <code>macheps (0.0)</code>).
|
---|
202 | </p>
|
---|
203 |
|
---|
204 | <hr>
|
---|
205 | <div class="header">
|
---|
206 | <p>
|
---|
207 | Next: <a href="Complex-Arithmetic.html" accesskey="n" rel="next">Complex Arithmetic</a>, Previous: <a href="Rounding-Control.html" accesskey="p" rel="prev">Rounding Control</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
208 | </div>
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | </body>
|
---|
213 | </html>
|
---|