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>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 | <!--
|
---|
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="Complex-Arithmetic"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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’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
|
---|
69 | some issues that are unlikely to be obvious to programmers without
|
---|
70 | extensive experience in both numerical computing, and in complex
|
---|
71 | arithmetic in mathematics.
|
---|
72 | </p>
|
---|
73 | <p>The first important point is that, unlike real arithmetic, in complex
|
---|
74 | arithmetic, the danger of significance loss is <em>pervasive</em>, and
|
---|
75 | affects <em>every one</em> of the basic operations, and <em>almost
|
---|
76 | all</em> of the math-library functions. To understand why, recall the
|
---|
77 | rules 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> */
|
---|
81 | b = x + I*y /* <span class="roman">Second operand.</span> */
|
---|
82 |
|
---|
83 | prod = a * b
|
---|
84 | = (u + I*v) * (x + I*y)
|
---|
85 | = (u * x - v * y) + I*(v * x + u * y)
|
---|
86 |
|
---|
87 | quo = 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
|
---|
97 | possibility 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
|
---|
102 | that is subject to catastrophic significance loss; and
|
---|
103 |
|
---|
104 | </li><li> complex multiplication has up to <em>six</em> rounding errors, and
|
---|
105 | complex 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
|
---|
111 | in complex arithmetic have <em>branch cuts</em>. You can view a
|
---|
112 | function with a complex argument, <code>f (z)</code>, as <code>f (x + I*y)</code>,
|
---|
113 | and thus, it defines a relation between a point <code>(x, y)</code> on the
|
---|
114 | complex plane with an elevation value on a surface. A branch cut
|
---|
115 | looks like a tear in that surface, so approaching the cut from one
|
---|
116 | side produces a particular value, and from the other side, a quite
|
---|
117 | different value. Great care is needed to handle branch cuts properly,
|
---|
118 | and even small numerical errors can push a result from one side to the
|
---|
119 | other, radically changing the returned value. As we reported earlier,
|
---|
120 | correct handling of the sign of zero is critically important for
|
---|
121 | computing near branch cuts.
|
---|
122 | </p>
|
---|
123 | <p>The best advice that we can give to programmers who need complex
|
---|
124 | arithmetic is to always use the <em>highest precision available</em>,
|
---|
125 | and then to carefully check the results of test calculations to gauge
|
---|
126 | the likely accuracy of the computed results. It is easy to supply
|
---|
127 | test values of real and imaginary parts where all five basic
|
---|
128 | operations in complex arithmetic, and almost all of the complex math
|
---|
129 | functions, lose <em>all</em> significance, and fail to produce even a
|
---|
130 | single correct digit.
|
---|
131 | </p>
|
---|
132 | <p>Even though complex arithmetic makes some programming tasks
|
---|
133 | easier, it may be numerically preferable to rework the algorithm
|
---|
134 | so that it can be carried out in real arithmetic. That is
|
---|
135 | commonly possible in matrix algebra.
|
---|
136 | </p>
|
---|
137 | <p>GNU C can perform code optimization on complex number multiplication and
|
---|
138 | division if certain boundary checks will not be needed. The
|
---|
139 | command-line option <samp>-fcx-limited-range</samp> tells the compiler that
|
---|
140 | a range reduction step is not needed when performing complex division,
|
---|
141 | and that there is no need to check if a complex multiplication or
|
---|
142 | division results in the value <code>Nan + I*NaN</code>. By default these
|
---|
143 | checks 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>
|
---|
150 | Next: <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> [<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>
|
---|