473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

complex arithmetic in C99

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
14 2393
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

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

"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
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
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
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
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
"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
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
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
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
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
4
by: Carl-Olof Almbladh | last post by:
Dear all, In C99, complex is now built in, and the C library contains functions for complex exp, etc. It is not so easy to find info about the syntax if one is not willing to buy the ISO/ANSI...
17
by: Julian V. Noble | last post by:
Dear C Mavens, I am writing a Computing Prescription for CiSE on complex arithmetic. I would like to be sure that I do not include any gaffes about C99--especially about what is in the header...
27
by: David Marsh | last post by:
I understand that C99 supports a complex type and complex arithmetic. There is nothing about it in the FAQ and online searches turned up very little except synopses. Can anyone point me toward...
25
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...
6
by: jacob navia | last post by:
Consider this code: < code > #include <complex.h> int main(void) { double complex c = 0.4+2.9I; c = ~c; } < end code >
9
by: void main | last post by:
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...
4
by: BiDi | last post by:
I have been trying to subclass complex, but I am not able to get the right-hand arithmetic operators working. As shown below, if an object of my subclass 'xcomplex' is added on the right of a...
4
by: Reshmi | last post by:
Hi, I have this C code which does complex number arithmetic. When I try to write a similar file for C++, it says that "creal’ was not declared in this scope". Can anyone give a better idea to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.