How to Initialize Complex Numbers

V

void main

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?
 
K

Keith Thompson

void main said:
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.
 
L

lawrence.jones

Dann Corbit said:
According to a sample in the C99 standard:
24 EXAMPLE 1 Provided that <complex.h> has 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.
 
J

jacob navia

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.
 
V

void main

Keith said:
void main said:
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.
 
K

Keith Thompson

void main said:
Keith said:
void main said:
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\ctest.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.
 
J

Jack Pot

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!
 
F

Flash Gordon

Jack Pot wrote, On 16/08/08 00:21:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top