Built-in primitives for complex

C

Carl-Olof Almbladh

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 standard document,
despite the fact that implementations are already available
Also, because it is built in, one cannot figure out
the syntax from the header files.

I have a few questions
a) How do I initialize a say double complex from
a pair of doubles, or, more generally, how
do I convert a pair of doubles to a double complex.
Of course one could do
z = x + I*y
where hopefully the complex multiply and add
can be optimized away, but there must be better
ways.

b) Primitives for say complex conjugates.

If someone can point to open sources of information
I would be grateful.

Carl-Olof Almbladh
 
K

Kevin Bracey

a) How do I initialize a say double complex from
a pair of doubles, or, more generally, how
do I convert a pair of doubles to a double complex.
Of course one could do
z = x + I*y
where hopefully the complex multiply and add
can be optimized away, but there must be better
ways.

Nope. That's it. How can that be bettered? It exactly reflects standard
mathematical notation. A compiler must be capable of optimising away the
apparent add and multiply, as such an expression is a valid initialiser
for a static variable.

There is a minor issue there to do with signalling NaNs, although I'm not
sure any current implementations support signalling NaNs fully & the standard
doesn't cover them anyway. So it's a moot point.

Also note that a good implementation should implement imaginary types, in
which case there's no actual complex arithmetic there anyway. I*y is
imaginary * real -> imaginary, and the "add" just combines the two parts:
real + imaginary -> complex.
b) Primitives for say complex conjugates.

These are in <complex.h>. conj() does a complex conjugate; in a typical good
implementation this will be a macro invoking a built-in operator or function.
 
D

Dan Pop

In said:
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 standard document,

One can get N869 for free (use Google) which is practically as good as
the standard itself for your purposes. One complex related example has
a glitch, but the same glitch is present in the standard itself:

[#24] EXAMPLE 1 Provided that <complex.h> has 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+3.0i.

The correct version is:

double complex c = 5 + 3 * I;

Dan
 
J

Julian V. Noble

Dan said:
In said:
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 standard document,

One can get N869 for free (use Google) which is practically as good as
the standard itself for your purposes. One complex related example has
a glitch, but the same glitch is present in the standard itself:

[#24] EXAMPLE 1 Provided that <complex.h> has 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+3.0i.

The correct version is:

double complex c = 5 + 3 * I;

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (e-mail address removed)
Currently looking for a job in the European Union

I note you write it 5 + 3*I whereas the OP said 5 + I*3 . Are they
equivalent and/or equally acceptable?

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the toothache
patiently." -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
 
C

Chris Barts

I note you write it 5 + 3*I whereas the OP said 5 + I*3 . Are they
equivalent and/or equally acceptable?

They must be. If I is defined as the complex unit, which it seems to be,
and multiplication still has a higher precedence than addition, a fact
only a fool would change, then 5 + 3*I must be equal to 5 + I*3.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top