source: public/doc/gnu-c/Error-Recovery.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: 6.8 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>Error Recovery (GNU C Language Manual)</title>
23
24<meta name="description" content="Error Recovery (GNU C Language Manual)">
25<meta name="keywords" content="Error Recovery (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="Exact-Floating-Constants.html" rel="next" title="Exact Floating Constants">
34<link href="Fused-Multiply_002dAdd.html" rel="prev" title="Fused Multiply-Add">
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="Error-Recovery"></span><div class="header">
59<p>
60Next: <a href="Exact-Floating-Constants.html" accesskey="n" rel="next">Exact Floating Constants</a>, Previous: <a href="Fused-Multiply_002dAdd.html" accesskey="p" rel="prev">Fused Multiply-Add</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="Error-Recovery-1"></span><h3 class="section">28.11 Error Recovery</h3>
64<span id="index-error-recovery-_0028floating-point_0029"></span>
65<span id="index-floating_002dpoint-error-recovery"></span>
66
67<p>When two numbers are combined by one of the four basic
68operations, the result often requires rounding to storage
69precision. For accurate computation, one would like to be able
70to recover that rounding error. With historical floating-point
71designs, it was difficult to do so portably, but now that IEEE
72754 arithmetic is almost universal, the job is much easier.
73</p>
74<p>For addition with the default <em>round-to-nearest</em> rounding
75mode, we can determine the error in a sum like this:
76</p>
77<div class="example">
78<pre class="example">volatile double err, sum, tmp, x, y;
79
80if (fabs (x) &gt;= fabs (y))
81 {
82 sum = x + y;
83 tmp = sum - x;
84 err = y - tmp;
85 }
86else /* fabs (x) &lt; fabs (y) */
87 {
88 sum = x + y;
89 tmp = sum - y;
90 err = x - tmp;
91 }
92</pre></div>
93
94<p><span id="index-twosum"></span>
95Now, <code>x + y</code> is <em>exactly</em> represented by <code>sum + err</code>.
96This basic operation, which has come to be called <em>twosum</em>
97in the numerical-analysis literature, is the first key to tracking,
98and accounting for, rounding error.
99</p>
100<p>To determine the error in subtraction, just swap the <code>+</code> and
101<code>-</code> operators.
102</p>
103<p>We used the <code>volatile</code> qualifier (see <a href="volatile.html">volatile</a>) in the
104declaration of the variables, which forces the compiler to store and
105retrieve them from memory, and prevents the compiler from optimizing
106<code>err = y - ((x + y) - x)</code> into <code>err = 0</code>.
107</p>
108<p>For multiplication, we can compute the rounding error without
109magnitude tests with the FMA operation (see <a href="Fused-Multiply_002dAdd.html">Fused Multiply-Add</a>),
110like this:
111</p>
112<div class="example">
113<pre class="example">volatile double err, prod, x, y;
114prod = x * y; /* <span class="roman">rounded product</span> */
115err = fma (x, y, -prod); /* <span class="roman">exact product <code>= <var>prod</var> + <var>err</var></code></span> */
116</pre></div>
117
118<p>For addition, subtraction, and multiplication, we can represent the
119exact result with the notional sum of two values. However, the exact
120result of division, remainder, or square root potentially requires an
121infinite number of digits, so we can at best approximate it.
122Nevertheless, we can compute an error term that is close to the true
123error: it is just that error value, rounded to machine precision.
124</p>
125<p>For division, you can approximate <code>x / y</code> with <code>quo + err</code>
126like this:
127</p>
128<div class="example">
129<pre class="example">volatile double err, quo, x, y;
130quo = x / y;
131err = fma (-quo, y, x) / y;
132</pre></div>
133
134<p>For square root, we can approximate <code>sqrt (x)</code> with <code>root +
135err</code> like this:
136</p>
137<div class="example">
138<pre class="example">volatile double err, root, x;
139root = sqrt (x);
140err = fma (-root, root, x) / (root + root);
141</pre></div>
142
143<p>With the reliable and predictable floating-point design provided
144by IEEE 754 arithmetic, we now have the tools we need to track
145errors in the five basic floating-point operations, and we can
146effectively simulate computing in twice working precision, which
147is sometimes sufficient to remove almost all traces of arithmetic
148errors.
149</p>
150
151
152
153<hr>
154<div class="header">
155<p>
156Next: <a href="Exact-Floating-Constants.html" accesskey="n" rel="next">Exact Floating Constants</a>, Previous: <a href="Fused-Multiply_002dAdd.html" accesskey="p" rel="prev">Fused Multiply-Add</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>
157</div>
158
159
160
161</body>
162</html>
Note: See TracBrowser for help on using the repository browser.