473,969 Members | 57,769 Online

# math.h functions for type _Complex

#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;
z1 = .4 + .7i;
z2 = pow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", z1);
printf("%f, %f \n", z2);
printf("%f, %f \n", z3);
system("PAUSE") ;
return 0;
}
In this program, z2 is not equal to z3 . z3 looks correct. On my
implementation (devcpp on its maiden voyage), z2 becomes the real part
squared, and the complex part zeros out and is probably undefined.

Q1)With C99's new types, how does one use the old math functions?

Q2)Is complex or _Complex a better type defn? LS
Feb 1 '07 #1
21 4242
On Feb 1, 1:18 pm, "Lane Straatman" <inva...@invali d.netwrote:
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;
z1 = .4 + .7i;
z2 = pow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", z1);
printf("%f, %f \n", z2);
printf("%f, %f \n", z3);
system("PAUSE") ;
return 0;}

In this program, z2 is not equal to z3 . z3 looks correct. On my
implementation (devcpp on its maiden voyage), z2 becomes the real part
squared, and the complex part zeros out and is probably undefined.

Q1)With C99's new types, how does one use the old math functions?

You should use math functions designed to work with complex operands.
For example, instead of pow, use cpow.
Q2)Is complex or _Complex a better type defn? LS
The former.

Try something like:

#include <stdio.h>
#include <stdlib.h>
#include <complex.h>

int main (void)
{
double complex z1, z2, z3;
z1 = .4 + .7I;
z2 = cpow(z1, 2.0);
z3 = z1 * z1;
printf("%lf %lf\n", creal(z1), cimag(z1));
printf("%lf %lf\n", creal(z2), cimag(z2));
printf("%lf %lf\n", creal(z3), cimag(z3));
return EXIT_SUCCESS;
}
--
Hope this helps,
Steven

Feb 1 '07 #2

<at************ *@gmail.comwrot e in message
news:11******** **************@ l53g2000cwa.goo glegroups.com.. .
On Feb 1, 1:18 pm, "Lane Straatman" <inva...@invali d.netwrote:
>Q1)With C99's new types, how does one use the old math functions?
You should use math functions designed to work with complex operands.
For example, instead of pow, use cpow.
>Q2)Is complex or _Complex a better type defn? LS
The former.
I think so too.
Try something like:
[refines source with better type defns and output]
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <stdbool.h>
#define N 41
#ifdef __bool_true_fal se_are_defined
#define N 42
#endif
int main (void)
{
double complex z1, z2, z3;
bool flag;
z1 = .4 + .7I;
z2 = cpow(z1, 2.0);
z3 = z1 * z1;
flag = false;
flag = true;
if (flag)
{
printf("%lf %lf\n", creal(z1), cimag(z1));
printf("%lf %lf\n", creal(z2), cimag(z2));
printf("%lf %lf\n", creal(z3), cimag(z3));
printf("%d\n", N);
}
system("PAUSE") ;
return EXIT_SUCCESS;
}
This program is of no great profoundness; I don't know of any way to get
used to new aspects of syntax without simply plodding ahead beginning with
elementary-looking stuff. It's a bit of a trick just to get the headers
included. LS
Feb 1 '07 #3
"Lane Straatman" <in*****@invali d.netwrites:
#include <stdio.h>
#include <stdlib.h>
You don't use anything in <stdlib.h>.
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;
_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex". If your compiler
allows _Complex by itself, it may be due to a misunderstandin g by its
authors; there's an incorrect example in the standard. C99 6.7.8
Example 1 is:

int i = 3.5;
complex c = 5 + 3 * I;

which is illegal. N1124 corrects this to:

int i = 3.5;
double complex c = 5 + 3 * I;
z1 = .4 + .7i;
z2 = pow(z1, 2);
You want cpow, not pow.
z3 = z1 * z1;
printf("%f, %f \n", z1);
printf("%f, %f \n", z2);
printf("%f, %f \n", z3);
A format of "%f, %f \n" requires two arguments of type double. You're
giving it a single argument of a complex type, which invokes undefined
behavior. There's are printf formats for complex numbers; you need to
print the real and complex parts separately, using the creal() and
cimag() functions.
system("PAUSE") ;
I get:

return 0;
}
In this program, z2 is not equal to z3 . z3 looks correct. On my
implementation (devcpp on its maiden voyage), z2 becomes the real part
squared, and the complex part zeros out and is probably undefined.

Q1)With C99's new types, how does one use the old math functions?
<complex.hdecla res math functions for complex types.
Q2)Is complex or _Complex a better type defn? LS
The new keyword is spelled "_Complex" only to avoid breaking existing
code. If you #include <complex.h>", you get "complex" as a macro that
expands to "_Complex". "_Complex" is almost deliberately ugly;
there's no need to use it in user code. Just use "complex" -- or
rather "double complex".

Here's a corrected version of your program:

#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex z1, z2, z3;
z1 = .4 + .7i;
z2 = cpow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", creal(z1), cimag(z1));
printf("%f, %f \n", creal(z2), cimag(z1));
printf("%f, %f \n", creal(z3), cimag(z1));
return 0;
}

--
Keith Thompson (The_Other_Keit h) 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.
Feb 2 '07 #4
Keith Thompson <ks***@mib.orgw rites:
"Lane Straatman" <in*****@invali d.netwrites:
> z2 = pow(z1, 2);

You want cpow, not pow.
I think that, if he additionally includes <tgmath.h>, he can use
plain "pow" here.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Feb 2 '07 #5
On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
"Lane Straatman" <inva...@invali d.netwrites:
#include <stdio.h>
#include <stdlib.h>

You don't use anything in <stdlib.h>.
There's a system call.
>
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;

_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex".
The problem wasn't my compiler. It was the manner in which I read
item 13 off a recent screenshot. The complex type is what is left
after you subtract '_Complex' and ';' from the line in which the type
compiles.
allows _Complex by itself, it may be due to a misunderstandin g by its
authors; there's an incorrect example in the standard. C99 6.7.8
Example 1 is:

int i = 3.5;
complex c = 5 + 3 * I;

which is illegal. N1124 corrects this to:

int i = 3.5;
double complex c = 5 + 3 * I;
I started with 3 and 5 but ended with 4 and 7. I was unaware of
cpow. Ben is right that tgmath.h would have overloaded operators that
would still let me use pow(), but the idea is that we're all going to
use the things that C99 offers, in particular, for complex
exponentiation, cpow().

z1 = .4 + .7i;
z2 = pow(z1, 2);

You want cpow, not pow.
z3 = z1 * z1;
printf("%f, %f \n", z1);
printf("%f, %f \n", z2);
printf("%f, %f \n", z3);

A format of "%f, %f \n" requires two arguments of type double. You're
giving it a single argument of a complex type, which invokes undefined
behavior. There's are printf formats for complex numbers; you need to
print the real and complex parts separately, using the creal() and
cimag() functions.
system("PAUSE") ;

I get:

return 0;
}
In this program, z2 is not equal to z3 . z3 looks correct. On my
implementation (devcpp on its maiden voyage), z2 becomes the real part
squared, and the complex part zeros out and is probably undefined.
Q1)With C99's new types, how does one use the old math functions?

<complex.hdecla res math functions for complex types.
Q2)Is complex or _Complex a better type defn? LS

The new keyword is spelled "_Complex" only to avoid breaking existing
code. If you #include <complex.h>", you get "complex" as a macro that
expands to "_Complex". "_Complex" is almost deliberately ugly;
there's no need to use it in user code. Just use "complex" -- or
rather "double complex".

Here's a corrected version of your program:

#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex z1, z2, z3;
z1 = .4 + .7i;
z2 = cpow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", creal(z1), cimag(z1));
printf("%f, %f \n", creal(z2), cimag(z1));
printf("%f, %f \n", creal(z3), cimag(z1));
return 0;

}
The above is basically what steven posted, beating your punch in
cyberspace, to judge from the time. The number 13 reference was to a
screenshot I made in the context of 1142.pdf that I think has
relevance now. I'm posting from google and feel like Mr. Magoo. LS

Feb 3 '07 #6
"lane straatman" <gr**********@n etzero.netwrite s:
On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
"Lane Straatman" <inva...@invali d.netwrites:
#include <stdio.h>
#include <stdlib.h>
You don't use anything in <stdlib.h>.
There's a system call.
You're right, my mistake. I deleted the system("PAUSE") ; call from my
copy of your program, since it the PAUSE command is non-portable.
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;
_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex".
The problem wasn't my compiler. It was the manner in which I read
item 13 off a recent screenshot. The complex type is what is left
after you subtract '_Complex' and ';' from the line in which the type
compiles.
Sorry, I can't figure out what you mean here.

The declaration you posted:

_Complex z1, z2, z3;

is illegal. Some compilers (including gcc 4.1.1) accept it. I
suspect they do so because of the incorrect exapmle in the C99
standard, corrected in n1124.pdf.

Was the code you posted not the actual code you compiled?

[...]
The above is basically what steven posted, beating your punch in
cyberspace, to judge from the time. The number 13 reference was to a
screenshot I made in the context of 1142.pdf that I think has
relevance now. I'm posting from google and feel like Mr. Magoo. LS
Not having seen your screenshot, I still have no idea what "13" refers
to. Would you care to clarify?

--
Keith Thompson (The_Other_Keit h) 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.
Feb 3 '07 #7

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"lane straatman" <gr**********@n etzero.netwrite s:
>On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex".
The problem wasn't my compiler. It was the manner in which I read
item 13 off a recent screenshot. The complex type is what is left
after you subtract '_Complex' and ';' from the line in which the type
compiles.
Sorry, I can't figure out what you mean here.
http://www.billfordx.net/screendumps/n1142_shot1.htm
I think there's a complex type of long double. If there isn't, it would
shed some light on the issue at hand. LS

Not having seen your screenshot, I still have no idea what "13" refers
to. Would you care to clarify?
I love the quality of the standard that makes it look like print. LS
Feb 3 '07 #8
"Lane Straatman" <in*****@invali d.netwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"lane straatman" <gr**********@n etzero.netwrite s:
On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex".
The problem wasn't my compiler. It was the manner in which I read
item 13 off a recent screenshot. The complex type is what is left
after you subtract '_Complex' and ';' from the line in which the type
compiles.
Sorry, I can't figure out what you mean here.
http://www.billfordx.net/screendumps/n1142_shot1.htm
I think there's a complex type of long double. If there isn't, it would
shed some light on the issue at hand. LS
n1124 is a PDF document. You should be able to copy-and-paste text
from the document and post it here, rather than having to set up a web
page with a jpg image of a screenshot.

The screenshot was a copy of paragraphs 11 through 13 of section 6.2.5
of n1124.pdf:

11 There are three _complex types_, designated as float _Complex,
double _Complex, and long double _Complex. The real floating and
complex types are collectively called the _floating types_.

12 For each floating type there is a _corresponding real type_, which
is always a real floating type. For real floating types, it is the
same type. For complex types, it is the type given by deleting the
keyword _Complex from the type name.

13 Each complex type has the same representation and alignment
requirements as an array type containing exactly two elements of
the corresponding real type; the first element is equal to the real
part, and the second element to the imaginary part, of the complex
number.

The phrase "a complex type of long double" doesn't make much sense,
I'm afraid. There is a type "long double _Complex"; its
"correspond ing real type" is long double. (long double itself, of
course, is not a complex type.) There is no type "_Complex", though
some compilers may allow it.

If you have a "#include <complex.h>", the type "long double _Complex"
can be referred to as "long double complex" -- and in fact that would
be the normal way to refer to it.

I fail to see how this sheds any light on the issue at hand -- perhaps
because I've completely lost track of what the issue at hand might be.
The original program you posted had several errors; I think they've
all been pointed out. Did you have any other questions, or is
everything sufficiently clear now?

--
Keith Thompson (The_Other_Keit h) 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.
Feb 3 '07 #9

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"Lane Straatman" <in*****@invali d.netwrites:
[...]
If you have a "#include <complex.h>", the type "long double _Complex"
can be referred to as "long double complex" -- and in fact that would
be the normal way to refer to it.

I fail to see how this sheds any light on the issue at hand -- perhaps
because I've completely lost track of what the issue at hand might be.
The original program you posted had several errors; I think they've
all been pointed out. Did you have any other questions, or is
everything sufficiently clear now?
I have a certain reverence for the integers; I'm not alone in my belief that
they came in from on high and then we figured everything else out. C at the
onset had an integer type of int. I think there's eleven integer types.
There might be a dozen. I think C99 admits of a single boolean type 'bool'.
It would be tied with any other type, as far as how new it is to C.

Are there complex types in C? If so, how many? LS
Feb 3 '07 #10

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