source: public/doc/gnu-c/Complex-Arithmetic.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: 7.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>Complex Arithmetic (GNU C Language Manual)</title>
23
24<meta name="description" content="Complex Arithmetic (GNU C Language Manual)">
25<meta name="keywords" content="Complex Arithmetic (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="Round_002dTrip-Base-Conversion.html" rel="next" title="Round-Trip Base Conversion">
34<link href="Machine-Epsilon.html" rel="prev" title="Machine Epsilon">
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="Complex-Arithmetic"></span><div class="header">
59<p>
60Next: <a href="Round_002dTrip-Base-Conversion.html" accesskey="n" rel="next">Round-Trip Base Conversion</a>, Previous: <a href="Machine-Epsilon.html" accesskey="p" rel="prev">Machine Epsilon</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="Complex-Arithmetic-1"></span><h3 class="section">28.19 Complex Arithmetic</h3>
64<span id="index-complex-arithmetic-in-floating_002dpoint-calculations"></span>
65<span id="index-floating_002dpoint-arithmetic-with-complex-numbers"></span>
66
67<p>We&rsquo;ve already looked at defining and referring to complex numbers
68(see <a href="Complex-Data-Types.html">Complex Data Types</a>). What is important to discuss here are
69some issues that are unlikely to be obvious to programmers without
70extensive experience in both numerical computing, and in complex
71arithmetic in mathematics.
72</p>
73<p>The first important point is that, unlike real arithmetic, in complex
74arithmetic, the danger of significance loss is <em>pervasive</em>, and
75affects <em>every one</em> of the basic operations, and <em>almost
76all</em> of the math-library functions. To understand why, recall the
77rules for complex multiplication and division:
78</p>
79<div class="example">
80<pre class="example">a = u + I*v /* <span class="roman">First operand.</span> */
81b = x + I*y /* <span class="roman">Second operand.</span> */
82
83prod = a * b
84 = (u + I*v) * (x + I*y)
85 = (u * x - v * y) + I*(v * x + u * y)
86
87quo = a / b
88 = (u + I*v) / (x + I*y)
89 = [(u + I*v) * (x - I*y)] / [(x + I*y) * (x - I*y)]
90 = [(u * x + v * y) + I*(v * x - u * y)] / (x**2 + y**2)
91</pre></div>
92
93<p>There are four critical observations about those formulas:
94</p>
95<ul>
96<li> the multiplications on the right-hand side introduce the
97possibility of premature underflow or overflow;
98
99</li><li> the products must be accurate to twice working precision;
100
101</li><li> there is <em>always</em> one subtraction on the right-hand sides
102that is subject to catastrophic significance loss; and
103
104</li><li> complex multiplication has up to <em>six</em> rounding errors, and
105complex division has <em>ten</em> rounding errors.
106
107</li></ul>
108
109<span id="index-branch-cuts"></span>
110<p>Another point that needs careful study is the fact that many functions
111in complex arithmetic have <em>branch cuts</em>. You can view a
112function with a complex argument, <code>f (z)</code>, as <code>f (x + I*y)</code>,
113and thus, it defines a relation between a point <code>(x, y)</code> on the
114complex plane with an elevation value on a surface. A branch cut
115looks like a tear in that surface, so approaching the cut from one
116side produces a particular value, and from the other side, a quite
117different value. Great care is needed to handle branch cuts properly,
118and even small numerical errors can push a result from one side to the
119other, radically changing the returned value. As we reported earlier,
120correct handling of the sign of zero is critically important for
121computing near branch cuts.
122</p>
123<p>The best advice that we can give to programmers who need complex
124arithmetic is to always use the <em>highest precision available</em>,
125and then to carefully check the results of test calculations to gauge
126the likely accuracy of the computed results. It is easy to supply
127test values of real and imaginary parts where all five basic
128operations in complex arithmetic, and almost all of the complex math
129functions, lose <em>all</em> significance, and fail to produce even a
130single correct digit.
131</p>
132<p>Even though complex arithmetic makes some programming tasks
133easier, it may be numerically preferable to rework the algorithm
134so that it can be carried out in real arithmetic. That is
135commonly possible in matrix algebra.
136</p>
137<p>GNU C can perform code optimization on complex number multiplication and
138division if certain boundary checks will not be needed. The
139command-line option <samp>-fcx-limited-range</samp> tells the compiler that
140a range reduction step is not needed when performing complex division,
141and that there is no need to check if a complex multiplication or
142division results in the value <code>Nan + I*NaN</code>. By default these
143checks are enabled. You can explicitly enable them with the
144<samp>-fno-cx-limited-range</samp> option.
145</p>
146
147<hr>
148<div class="header">
149<p>
150Next: <a href="Round_002dTrip-Base-Conversion.html" accesskey="n" rel="next">Round-Trip Base Conversion</a>, Previous: <a href="Machine-Epsilon.html" accesskey="p" rel="prev">Machine Epsilon</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>
151</div>
152
153
154
155</body>
156</html>
Note: See TracBrowser for help on using the repository browser.