"void main" <no****@nospam.invalidwrote in message
news:61****************@aioe.org...
I'm rather new to complex numbers in C and was wondering, how do I
initialize a complex variable properly if the imaginary part is 0.
I tried
--------
#include <complex.h>
float complex c = 1.0f;
--------
and was told:
Error ctest.c: 3 invalid initialization type; found 'float' expected
'struct long double _Complex'
--------
#include <complex.h>
float complex c = 1.0f + I * 0.0f;
--------
and
--------
#include <complex.h>
float complex c = 1.0f + 0.0f * I;
--------
but both got me
Error c:\tests\clc\ctest.c 3 Compiler error (trap). Stopping compilation
What am I doing wrong?
According to a sample in the C99 standard:
24 EXAMPLE 1 Provided that <complex.hhas been #included, the declarations
int i = 3.5;
complex c = 5 + 3 * I;
define and initialize i with the value 3 and c with the value 5. 0 + i3. 0.
So I think you are doing it right. Show us the actual code you are trying
to compile.
Here are some sample C programs that use complex numbers from the standard.
If your compiler does not compile it, then you do not have a working C99
compiler.
From ISO/IEC 9899:1999 (E) ŠISO/IEC:
#include <math.h>
#include <complex.h>
/* Multiplyz * w... */
double complex _Cmultd(double complex z, double complex w)
{
#pragma STDC FP_CONTRACT OFF
double a,
b,
c,
d,
ac,
bd,
ad,
bc,
x,
y;
a = creal(z);
b = cimag(z)
c = creal(w);
d = cimag(w);
ac = a * c;
bd = b * d;
ad = a * d;
bc = b * c;
x = ac - bd;
y = ad + bc;
if (isnan(x) && isnan(y)) {
/* Recover infinities that computed as NaN+iNaN ... */
int recalc = 0;
if (isinf(a) || isinf(b)) { // z is infinite
/* "Box" the infinity and change NaNs in the other factor to 0
*/
a = copysign(isinf(a) ? 1.0 : 0.0, a);
b = copysign(isinf(b) ? 1.0 : 0.0, b);
if (isnan(c))
c = copysign(0.0, c);
if (isnan(d))
d = copysign(0.0, d);
recalc = 1;
}
if (isinf(c) || isinf(d)) { // w is infinite
/* "Box" the infinity and change NaNs in the other factor to 0
*/
c = copysign(isinf(c) ? 1.0 : 0.0, c);
d = copysign(isinf(d) ? 1.0 : 0.0, d);
if (isnan(a))
a = copysign(0.0, a);
if (isnan(b))
b = copysign(0.0, b);
recalc = 1;
}
if (!recalc && (isinf(ac) || isinf(bd) ||
isinf(ad) || isinf(bc))) {
/* Recover infinities from overflow by changing NaNs to 0 ... */
if (isnan(a))
a = copysign(0.0, a);
if (isnan(b))
b = copysign(0.0, b);
if (isnan(c))
c = copysign(0.0, c);
if (isnan(d))
d = copysign(0.0, d);
recalc = 1;
}
if (recalc) {
x = INFINITY * (a * c - b * d);
y = INFINITY * (a * d + b * c);
}
}
return x + I * y;
}
/*
7 This implementation achieves the required treatment of infinities at
the cost of only one isnan test in ordinary (finite) cases. It is less than
ideal in that undue overflow and underflow may occur.
468 IEC60559-compatible complexarithmetic §G.5.1
ŠISO/IEC ISO/IEC 9899:1999 (E)
8 EXAMPLE 2 Division of two double _Complex operands could be implemented
as follows.
*/
#include <math.h>
#include <complex.h>
/* Dividez / w ... */
double complex _Cdivd(double complex z, double complex w)
{
#pragma STDC FP_CONTRACT OFF
double a,
b,
c,
d,
logbw,
denom,
x,
y;
int ilogbw = 0;
a = creal(z);
b = cimag(z);
c = creal(w);
d = cimag(w);
logbw = logb(fmax(fabs(c), fabs(d)));
if (isfinite(logbw)) {
ilogbw = (int) logbw;
c = scalbn(c, -ilogbw);
d = scalbn(d, -ilogbw);
}
denom = c * c + d * d;
x = scalbn((a * c + b * d) / denom, -ilogbw);
y = scalbn((b * c - a * d) / denom, -ilogbw);
/* Recover infinities and zeros that computed as NaN+iNaN; */
/* the only cases are non-zero/zero, infinite/finite, and finite/infinite,
.... */
if (isnan(x) && isnan(y)) {
if ((denom == 0.0) &&
(!isnan(a) || !isnan(b))) {
x = copysign(INFINITY, c) * a;
y = copysign(INFINITY, c) * b;
} else if ((isinf(a) || isinf(b)) &&
isfinite(c) && isfinite(d)) {
a = copysign(isinf(a) ? 1.0 : 0.0, a);
b = copysign(isinf(b) ? 1.0 : 0.0, b);
x = INFINITY * (a * c + b * d);
y = INFINITY * (b * c - a * d);
} else if (isinf(logbw) &&
isfinite(a) && isfinite(b)) {
c = copysign(isinf(c) ? 1.0 : 0.0, c);
d = copysign(isinf(d) ? 1.0 : 0.0, d);
x = 0.0 * (a * c + b * d);
y = 0.0 * (b * c - a * d);
}
}
return x + I * y;
}
/*
9 Scaling the denominator alleviates the main overflow and underflow
problem, which is more serious than for multiplication. In the spirit of the
multiplication example above, this code does not defend against overflow
and underflow in the calculation of the numerator. Scaling with the scalbn
function, instead of with division, provides better roundoff
characteristics.
§G.5.1 IEC60559-compatible complex arithmetic 469
*/
** Posted from
http://www.teranews.com **