forked from johannesgerer/jburkardt-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_numbers.html
204 lines (174 loc) · 6.55 KB
/
complex_numbers.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<html>
<head>
<title>
COMPLEX_NUMBERS - Complex Numbers in C++ Programming
</title>
</head>
<body bgcolor="#EEEEEE" link="#CC0000" alink="#FF3300" vlink="#000055">
<h1 align = "center">
COMPLEX_NUMBERS <br> Complex Numbers in C++ Programming
</h1>
<hr>
<p>
<b>COMPLEX_NUMBERS</b> is a C++ program which
demonstrates very briefly some of the features of using complex
numbers in a C++ program.
</p>
<p>
The first issue is how to declare a complex variable, including
the choice of single precision or double precision, whether
the variable is a scalar, vector, or array, and whether the variable
is initialized with a value, or assigned one.
</p>
<p>
A second issue concerns the question of how a complex variable is
to be printed out.
</p>
<p>
Another issue concerns how a complex variable is to operated on
by the arithmetic operators of addition, subtraction, multiplication,
division, and exponentiation.
</p>
<p>
The language also provides a number of intrinsic functions that
can be applied to a complex variable. The names of these functions
can sometimes be easy to forget. Moreover, it is occasionally true
that there may be a selection of functions with similar names
(say, "exp", "cexp" and "dcexp") which may or may not produce the
desired results.
</p>
<p>
Another issue concerns the details of double precision calculation.
Even a single accidental use of a single precision function name
in a double precision computation can result in the loss of half
the digits of accuracy. Thus, it sometimes really matters whether
you use "cmplx" or "dcmplx" to assign values to a double precision
complex variable.
</p>
<p>
An unusual feature of the C++ implementation of complex numbers is that
"complex <float>" and "complex <double>" are not as easily
used as an ordinary type when trying to "cast" values. That is,
it may be very tempting to try to assign a complex value with a
statement such as
<blockquote>
a = ( complex <double> ) ( 1.0, 2.0 ); (illegal!)
</blockquote>
but this is wrong because (1.0,2.0) is not a legitimate numeric type
that is to be converted to another type, which is what a cast operator
requires. The statement, unfortunately,
is not necessarily illegal, so the compiler won't warn you, and my compiler
will actually end up setting <b>a</b> to 2, which is not what I
wanted at all. The correct assignment is
<blockquote>
a = complex <double> ( 1.0, 2.0 );
</blockquote>
and you can read this as a call to a function whose arguments are
the real and imaginary parts of the desired result. People who are
used to the idea that parentheses never hurt should take note of
this counterexample!
</p>
<p>
A second, and more peculiar, feature to me is that it seems as though
the complex numbers created in C++ cannot easily interoperate with
the real and integer values. In particular, assuming the declaration
<blockquote>
a = complex <double> ( 1.0, 2.0 );
</blockquote>
my compiler complained about statements like these:
<pre>
a = a + 1; (illegal!)
a = a * 4; (illegal!)
a = a / 8; (illegal!)
a = 1 / a; (illegal!)
a = pow ( 2, a ); (illegal!)
a = pow ( 2.1, a ); (illegal!)
</pre>
all of which became legal when I replaced the arithmetic operands
by corresponding complex <double> variables of the same value.
</p>
<p>
A third peculiarity of the C++ version of complex numbers is that the
function <b>norm ( z )</b> is stated to return
<i>the norm of the complex number <b>z</b></i>, but in fact,
returns the <i>square</i> of the norm of the number. The use of the word
"norm" is a misuse, since the norm has a commonly accepted mathematical
definition as the square root of the sum of the squares of the
real and imaginary parts. Thus, the C++ function returns the "norm" of 4+4i
as 32 whereas the norm of 4+4i is sqrt(32). As ever in computer languages,
once a bad choice is made, it's never rescinded. Be prepared for
needless and inevitable confusion, though, when using this function.
</p>
<h3 align = "center">
Licensing:
</h3>
<p>
The computer code and data files described and made available on this web page
are distributed under
<a href = "../../txt/gnu_lgpl.txt">the GNU LGPL license.</a>
</p>
<h3 align = "center">
Languages:
</h3>
<p>
<b>COMPLEX_NUMBERS</b> is available in
<a href = "../../c_src/complex_numbers/complex_numbers.html">a C version</a> and
<a href = "../../cpp_src/complex_numbers/complex_numbers.html">a C++ version</a> and
<a href = "../../f77_src/complex_numbers/complex_numbers.html">a FORTRAN77 version</a> and
<a href = "../../f_src/complex_numbers/complex_numbers.html">a FORTRAN90 version</a> and
<a href = "../../m_src/complex_numbers/complex_numbers.html">a MATLAB version</a>.
</p>
<h3 align = "center">
Related Data and Programs:
</h3>
<p>
<a href = "../../cpp_src/c4lib/c4lib.html">
C4LIB</a>,
a C++ library which
implements certain elementary functions for "C4" or
single precision complex variables;
</p>
<p>
<a href = "../../cpp_src/c8lib/c8lib.html">
C8LIB</a>,
a C++ library which
implements certain elementary functions for "C8" or
double precision complex variables;
</p>
<h3 align = "center">
Source Code:
</h3>
<p>
<ul>
<li>
<a href = "complex_numbers.cpp">complex_numbers.cpp</a>,
the source code.
</li>
<li>
<a href = "complex_numbers.sh">complex_numbers.sh</a>,
commands that compile and load the program.
</li>
</ul>
</p>
<h3 align = "center">
Examples and Tests:
</h3>
<p>
<ul>
<li>
<a href = "complex_numbers_output.txt">complex_numbers_output.txt</a>,
the output file;
</li>
</ul>
</p>
<p>
You can go up one level to <a href = "../cpp_src.html">
the C++ source codes</a>.
</p>
<hr>
<i>
Last revised on 06 November 2010.
</i>
<!-- John Burkardt -->
</body>
</html>