473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Initialize Complex Numbers

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\ct est.c 3 Compiler error (trap). Stopping compilation

What am I doing wrong?

Jul 23 '08 #1
9 9949

"void main" <no****@nospam. invalidwrote in message
news:61******** ********@aioe.o rg...
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\ct est.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 complexarithmet ic §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(INFINI TY, c) * a;
y = copysign(INFINI TY, 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 **
Jul 23 '08 #2
void main <no****@nospam. invalidwrites:
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.
[valid code snipped]
What am I doing wrong?
You are complaining in comp.lang.c rather than submitting a bug report
to your compiler provider. And you are doing so quite deliberately.

You are a troll. Go away.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 23 '08 #3
Dann Corbit <dc*****@connx. comwrote:
>
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;
Note that that example is defective: "complex" is not a valid type
specifier on its own, it should be "float complex", "double complex", or
"long double complex" instead.
--
Larry Jones

I don't like these stories with morals. -- Calvin
Jul 23 '08 #4
la************@ siemens.com wrote:
Dann Corbit <dc*****@connx. comwrote:
>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;

Note that that example is defective: "complex" is not a valid type
specifier on its own, it should be "float complex", "double complex", or
"long double complex" instead.
I thought that this defect was removed in later editions.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jul 23 '08 #5
jacob navia <ja***@nospam.c omwrites:
la************@ siemens.com wrote:
>Dann Corbit <dc*****@connx. comwrote:
>>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;
Note that that example is defective: "complex" is not a valid type
specifier on its own, it should be "float complex", "double complex", or
"long double complex" instead.

I thought that this defect was removed in later editions.
It was. The error was noted in DR #293
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_293.htmand
corrected in TC3, which was incorporated into
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 23 '08 #6
Keith Thompson wrote:
void main <no****@nospam. invalidwrites:
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.

[valid code snipped]
What am I doing wrong?

You are complaining in comp.lang.c rather than submitting a bug report
to your compiler provider. And you are doing so quite deliberately.
I wasn't complaining about anything. If this is a private group and
outsiders are not welcome here, you could have just said so without
the baseless accusations.

Instead, in my first post here I was told the issue does not show up
in MSVC, so it must be a troll? The world is not MSVC.

This thread's a troll because I'm posting here deliberately??
You are a troll. Go away.
Thanks, I will.

I asked the same question someplace else and got a helpful response.

For the benefit of those who run into the same issue, here's the
deal: Some compilers can not handle initialization of complex numbers
at global level. Moving those variables to local level or doing initial
assignment to globals in code both work even with the imaginary part
being zero or left out.

Minor inconvenience, but I think I can live with that.

Jul 24 '08 #7
void main <no****@nospam. invalidwrites:
Keith Thompson wrote:
>void main <no****@nospam. invalidwrites:
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.

[valid code snipped]
What am I doing wrong?

You are complaining in comp.lang.c rather than submitting a bug report
to your compiler provider. And you are doing so quite deliberately.

I wasn't complaining about anything. If this is a private group and
outsiders are not welcome here, you could have just said so without
the baseless accusations.
No, it's not a private group.

It's possible that I was mistaken in my accusation. Let me explain
the background; perhaps you can clarify this.

lcc-win is a C compiler maintained by jacob navia (yes, he spells his
name in lower case), who posts here regularly. jacob has been
involved in a number of heated discussions here, some of them
involving his perceived attitude towards bug reports.

Several times, we've seem someone anonymously post a seemingly
innocent question here about whether some unnamed compiler's behavior
is correct. The thing is, the posted code appeared to be specifically
designed to trigger a specific bug in lcc-win that we had just
recently discussed. The post did not mention lcc-win by name or
mention the recent discussion.

My suspicion, and I think that of some of the other regulars, is that
such posts are specifically designed to provoke a strong reaction from
jacob navia, triggering yet another heated discussion -- something
this newsgroup definitely doesn't need.

Your post at the top of this thread seemed to fit that pattern. If
that was a coincidence, and your question was sincere, then I
apologize. (I'm not comfortable with conditional apologies, but I'm
still not certain what's going on here.)

For future reference:

If a compiler responds to a valid or invalid source file by crashing,
such as the message you quoted in your original message:

Error c:\tests\clc\ct est.c 3 Compiler error (trap). Stopping compilation

that's definitely a bug in the compiler, and it should be reported to
the compiler vendor.

If you're asking whether a compiler is behaving correctly (in your
case, it wasn't), it can be helpful to identify the compiler.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 25 '08 #8
la************@ siemens.com wrote:
Dann Corbit <dc*****@connx. comwrote:
>>
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;

Note that that example is defective: "complex" is not a valid type
specifier on its own, it should be "float complex", "double complex", or
"long double complex" instead.
FWIW, lcc-win also accepts "void complex", which may seem somewhat less
portable, but using it in code does look very sophisticated!

Aug 15 '08 #9
Jack Pot wrote, On 16/08/08 00:21:
la************@ siemens.com wrote:
>Dann Corbit <dc*****@connx. comwrote:
>>According to a sample in the C99 standard:
24 EXAMPLE 1 Provided that <complex.hhas been #included, the
declaration s int i = 3.5;
complex c = 5 + 3 * I;
Note that that example is defective: "complex" is not a valid type
specifier on its own, it should be "float complex", "double complex", or
"long double complex" instead.

FWIW, lcc-win also accepts "void complex", which may seem somewhat less
portable, but using it in code does look very sophisticated!
It probably reduces the portability as well since I doubt that "void
complex" is not one of the complex types specified by the standard.

Jacob, if lcc-win accepts "void complex" I think you should add it to
your bug list. This does *not* mean that you should fix it immediately,
it is up to you how you prioritise bug fixes and if/when you fix them.
--
Flash Gordon
Aug 16 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4143
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld cmplx, temp;
5
5409
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are either "float" or "complex" types. These numbers are so large that I either get an overflow error, or some funky code like #INF or 1.#INDj. However I really need these numbers to be calculated (although precision isn't key). Is there a way to get...
17
3168
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am able to CRASH the backend, there is a bug here somewhere... test=# select version(); version
6
2257
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
3677
by: seia0106 | last post by:
Hello, I have an array X=, whose even and odd indices should represent real and imaginary parts of complex numbers. This I want to use in a routine that uses the typedef double Cx; for storing complex numbers. I want to declare an array of complex numbers(with 8 elements) of this type(Cx), such that even and odd indices of array X are loaded as real
3
2061
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class instance for "x" above, I'd like to make the format string apply to an element or elements of the instance. Can I somehow overload the "%" operator for that? Thanks.
3
6252
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
7
2379
by: schaefer.mp | last post by:
To compute the absolute value of a negative base raised to a fractional exponent such as: z = (-3)^4.5 you can compute the real and imaginary parts and then convert to the polar form to get the correct value: real_part = ( 3^-4.5 ) * cos( -4.5 * pi ) imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )
25
9724
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for instance double _Complex m = 2+3*I; printf("%Zg\n",m);
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.