By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

complex arithmetic in C99

P: n/a
Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at
complex.h, I don't see any library calls for + - * or /. How are
the basic arithmetic operations carried out?

David Marsh
Jan 10 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
David Marsh wrote:
Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at complex.h, I
don't see any library calls for + - * or /. How are the basic arithmetic
operations carried out?

David Marsh
#include <complex.h>
int main(void)
{
complex c = 1.2+0.67*I;
}

This defines the complex number 1.2+0.67i.
Jan 10 '07 #2

P: n/a

David Marsh wrote:
Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at
complex.h, I don't see any library calls for + - * or /. How are
the basic arithmetic operations carried out?

David Marsh
ISO/IEC 9899:1999 (E) ISO/IEC
page 128 Language 6.7.8
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.

Jan 10 '07 #3

P: n/a

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr...
David Marsh wrote:
>Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at complex.h, I
don't see any library calls for + - * or /. How are the basic arithmetic
operations carried out?

David Marsh

#include <complex.h>
int main(void)
{
complex c = 1.2+0.67*I;
}

This defines the complex number 1.2+0.67i.
Are float _Complex , double _Complex and long double _Complex valid ISO C
types? LS
Jan 12 '07 #4

P: n/a
Lane Straatman wrote:
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr...
David Marsh wrote:
Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at complex.h, I
don't see any library calls for + - * or /. How are the basic arithmetic
operations carried out?

David Marsh
#include <complex.h>
int main(void)
{
complex c = 1.2+0.67*I;
}

This defines the complex number 1.2+0.67i.
Are float _Complex , double _Complex and long double _Complex valid ISO C
types?
Yes. When complex.h is included, complex is nothing more than a macro
which expands to _Complex.

And _Complex by itself is not a valid type. The code as is shouldn't
compile without problems. It should read

#include <complex.h>
int main(void)
{
complex double c = 1.2+0.67*I;
}

The location of the double keyword is not important -- it may be before
or after complex -- but you can't omit it unless you choose to rely on
some compiler extension.

Jan 12 '07 #5

P: n/a
Harald van Dijk a écrit :
Lane Straatman wrote:
>>"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr. ..
>>>David Marsh wrote:

Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at complex.h, I
don't see any library calls for + - * or /. How are the basic arithmetic
operations carried out?

David Marsh

#include <complex.h>
int main(void)
{
complex c = 1.2+0.67*I;
}

This defines the complex number 1.2+0.67i.

Are float _Complex , double _Complex and long double _Complex valid ISO C
types?


Yes. When complex.h is included, complex is nothing more than a macro
which expands to _Complex.

And _Complex by itself is not a valid type. The code as is shouldn't
compile without problems. It should read

#include <complex.h>
int main(void)
{
complex double c = 1.2+0.67*I;
}

The location of the double keyword is not important -- it may be before
or after complex -- but you can't omit it unless you choose to rely on
some compiler extension.
Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >

Page 128
Jan 12 '07 #6

P: n/a
jacob navia wrote:
Harald van Dijk a écrit :
Lane Straatman wrote:
>"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr.. .

David Marsh wrote:

Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at complex.h, I
don't see any library calls for + - * or /. How are the basic arithmetic
operations carried out?

David Marsh

#include <complex.h>
int main(void)
{
complex c = 1.2+0.67*I;
}

This defines the complex number 1.2+0.67i.

Are float _Complex , double _Complex and long double _Complex valid ISO C
types?

Yes. When complex.h is included, complex is nothing more than a macro
which expands to _Complex.

And _Complex by itself is not a valid type. The code as is shouldn't
compile without problems. It should read

#include <complex.h>
int main(void)
{
complex double c = 1.2+0.67*I;
}

The location of the double keyword is not important -- it may be before
or after complex -- but you can't omit it unless you choose to rely on
some compiler extension.

Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >
The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.

Jan 12 '07 #7

P: n/a
Harald van Dijk a écrit :
jacob navia wrote:
>>Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >


The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.
Damm!!!

Because of that example,I had:
typedef long double _Complex complex;
in my complex.h!
Jan 12 '07 #8

P: n/a
"Harald van Dijk" <tr*****@gmail.comwrites:
jacob navia wrote:
[...]
>Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >

The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.
And the example is corrected ("complex" --"double complex") in
n1124.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 12 '07 #9

P: n/a
jacob navia said:
Harald van D?k a crit :
>jacob navia wrote:
>>>Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >


The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.

Damm!!!

Because of that example,I had:
typedef long double _Complex complex;
in my complex.h!
I doubt whether many here will be surprised to learn that there is yet
another bug in lcc-win32. It seems that its users may be able to give the
old excuse, "it must be a bug in the compiler", a new lease of life.

It needn't be that way, you know.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 12 '07 #10

P: n/a
Richard Heathfield a crit :
jacob navia said:

>>Harald van D?k a crit :
>>>jacob navia wrote:

Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >
The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.

Damm!!!

Because of that example,I had:
typedef long double _Complex complex;
in my complex.h!


I doubt whether many here will be surprised to learn that there is yet
another bug in lcc-win32. It seems that its users may be able to give the
old excuse, "it must be a bug in the compiler", a new lease of life.

It needn't be that way, you know.
Pathetic. I have a bug in my compiler because THERE IS A BUG IN THE
STANDARD, and obviously it is MY FAULT heathfield...

Yes, It needn't be that way, you know. They should publish standards
without bugs. But since they are just humans, like me, they do make
bugs and wrote a bug in their example. Unaware of that I followed that
example, that I considered normative.

Of course beings like you never make any mistake.

Never :-)
Jan 12 '07 #11

P: n/a
jacob navia said:
Richard Heathfield a crit :
>jacob navia said:
>>>Because of that example,I had:
typedef long double _Complex complex;
in my complex.h!


I doubt whether many here will be surprised to learn that there is yet
another bug in lcc-win32. It seems that its users may be able to give the
old excuse, "it must be a bug in the compiler", a new lease of life.

It needn't be that way, you know.

Pathetic. I have a bug in my compiler because THERE IS A BUG IN THE
STANDARD, and obviously it is MY FAULT heathfield...
In this case, it isn't a normative part of the Standard, so yes, it's your
fault for trusting an example.
Yes, It needn't be that way, you know. They should publish standards
without bugs. But since they are just humans, like me, they do make
bugs and wrote a bug in their example. Unaware of that I followed that
example, that I considered normative.
That was your mistake and your fault. See Note 6 of the Foreword, which says
in part:

"In accordance with Part 3 of the ISO/IEC Directives, this foreword, the
introduction, notes, footnotes, and examples are also for information
only."

Of course beings like you never make any mistake.
You are mistaken. Again.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 12 '07 #12

P: n/a
jacob navia <ja***@jacob.remcomp.frwrites:
Harald van Dijk a écrit :
>jacob navia wrote:
>>>Thez C standard 6.7.8 "Initialization":
< quote >
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.
< quote >
The example is non-normative and wrong, and there is an open DR about
it (#293). The relevant constraint it violates is 6.7.2p2.

Damm!!!

Because of that example,I had:
typedef long double _Complex complex;
in my complex.h!
You might have better luck if you use n1124 as a reference rather than
the C99 standard itself. All changes are marked with change bars, so
you can easily see the differences.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 12 '07 #13

P: n/a

"Keith Thompson" wrote :
<snip>
You might have better luck if you use n1124 as a reference rather than
the C99 standard itself. All changes are marked with change bars, so
you can easily see the differences.
I came back to this older thread to see if I could confirm my suspicion that
_Complex were a keyword in the C99. I see no change bars here. I wonder if
_Complex came to the language earlier than did _Bool .
http://www.billfordx.net/screendumps/n1142_shot1.htm

Are the keywords that have been added to C99 mostly of the form _Something ?
I sure wish that electronic versions of the standard weren't a king's
ransom. LS
Jan 15 '07 #14

P: n/a
"Lane Straatman" <in*****@invalid.netwrites:
"Keith Thompson" wrote :
<snip>
>You might have better luck if you use n1124 as a reference rather than
the C99 standard itself. All changes are marked with change bars, so
you can easily see the differences.
I came back to this older thread to see if I could confirm my suspicion that
_Complex were a keyword in the C99. I see no change bars here. I wonder if
_Complex came to the language earlier than did _Bool .
http://www.billfordx.net/screendumps/n1142_shot1.htm

Are the keywords that have been added to C99 mostly of the form _Something ?
I sure wish that electronic versions of the standard weren't a king's
ransom. LS
I paid $18 for my copy of the C99 standard in PDF format.

There are no changes in keywords between C99 and n1124. The new
keywords in C99 (relative to C90) are:

_Bool
_Complex
_Imaginary
inline
restrict

The committee *could* have spelled the last two as _Inline and
_Restrict, and perhaps added "inline" and "restrict" macros in some
new standard header, but they chose not to. I think they decided that
breaking existing code that uses "bool", "complex", or "imaginary" as
identifiers would have caused too many problems, but "inline" was
probably mostly used for compiler extensions very similar to the new
C99 feature, and "restrict" probably wasn't used as an identifier very
often.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 15 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.