source: public/doc/gnu-c/Machine-Epsilon.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 9.3 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
68important to know: <em>base</em> (the number that the exponent specifies
69a power of), <em>precision</em> (number of digits in the significand),
70and <em>range</em> (difference between most positive and most negative
71values). The allocation of bits between exponent and significand
72decides the answers to those questions.
73</p>
74<p>A measure of the precision is the answer to the question: what is
75the smallest number that can be added to <code>1.0</code> such that the
76sum 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> */
84static const double eps = 0x1p-52; /* <span class="roman">about 2.220e-16</span> */
85static 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
89Standard 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
93run time, and we can easily generalize the operation by replacing
94the constant <code>1.0</code> with a user-supplied value:
95</p>
96<div class="example">
97<pre class="example">double
98macheps (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>) &gt; <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
120values in hexadecimal, we get output like this:
121</p>
122<div class="example">
123<pre class="example">macheps ( 0) = 0x1.0000000000000p-1074
124macheps ( 1) = 0x1.0000000000000p-52
125macheps ( 2) = 0x1.0000000000000p-51
126macheps ( 3) = 0x1.8000000000000p-52
127macheps ( 4) = 0x1.0000000000000p-50
128macheps ( 5) = 0x1.4000000000000p-51
129macheps ( 6) = 0x1.8000000000000p-51
130macheps ( 7) = 0x1.c000000000000p-51
131macheps ( 8) = 0x1.0000000000000p-49
132macheps ( 9) = 0x1.2000000000000p-50
133macheps ( 10) = 0x1.4000000000000p-50
134macheps (Inf) = infinity
135macheps (NaN) = nan
136</pre></div>
137
138<p>Notice that <code>macheps</code> has a special test for a NaN to prevent an
139infinite loop.
140</p>
141
142<p>Our code made another test for a zero argument to avoid getting a
143zero return. The returned value in that case is the smallest
144representable 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
151architectures may differ.
152</p>
153<p>The C library includes some related functions that can also be used to
154determine machine epsilons at run time:
155</p>
156<div class="example">
157<pre class="example">#include &lt;math.h&gt; /* <span class="roman">Include for these prototypes.</span> */
158
159double nextafter (double x, double y);
160float nextafterf (float x, float y);
161long 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
166result as <code>1.0 + macheps (1.0)</code> and <code>1.0 + DBL_EPSILON</code>.
167See <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
170about all numbers. At the boundaries where normalization changes the
171exponent, 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
175the <em>positive</em> and <em>negative</em>, or <em>big</em> and
176<em>small</em>, machine epsilons. You can produce their values like
177this:
178</p>
179<div class="example">
180<pre class="example">eps_neg = 1.0 - nextafter (1.0, -1.0);
181eps_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
185compile time, then you can substitute literal <var>y</var> values with
186either <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 ());
190eps_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>
194functions return <var>y</var> if <var>x</var> equals <var>y</var></em>. Our two
195assignments then produce <code>+0x1.fffffffffffffp+1023</code> (about
1961.798e+308) for <var>eps_neg</var> and Infinity for <var>eps_pos</var>. Thus,
197the call <code>nextafter (INFINITY, -INFINITY)</code> can be used to find
198the 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
201the output from <code>macheps (0.0)</code>).
202</p>
203
204<hr>
205<div class="header">
206<p>
207Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.