Arithmetic/Complex: Difference between revisions
Content added Content deleted
(→{{header|Java}}: Add conjugate) |
(→{{header|C}}: conjugate) |
||
Line 265: | Line 265: | ||
The more recent [[C99]] standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a ''different'' header than the <complex> templates that are defined by [[C++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm] |
The more recent [[C99]] standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a ''different'' header than the <complex> templates that are defined by [[C++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm] |
||
<lang c>#include <complex.h> |
<lang c>#include <complex.h> |
||
#include <stdio.h> |
|||
void cprint(double complex c) |
void cprint(double complex c) |
||
Line 290: | Line 291: | ||
// negation |
// negation |
||
c = -a; |
c = -a; |
||
printf("\n-a="); cprint(c |
printf("\n-a="); cprint(c); |
||
// conjugate |
|||
c = conj(a); |
|||
printf("\nconj a="); cprint(c); printf("\n"); |
|||
}</lang> |
}</lang> |
||
Line 325: | Line 329: | ||
Complex ans; |
Complex ans; |
||
ans.real = -a.real; |
ans.real = -a.real; |
||
ans.imag = -a.imag; |
|||
return ans; |
|||
} |
|||
Complex conj(Complex a){ |
|||
Complex ans; |
|||
ans.imag = -a.imag; |
ans.imag = -a.imag; |
||
return ans; |
return ans; |
||
Line 344: | Line 354: | ||
printf("\na*b="); put(mult(a,b)); |
printf("\na*b="); put(mult(a,b)); |
||
printf("\n1/a="); put(inv(a)); |
printf("\n1/a="); put(inv(a)); |
||
printf("\n-a="); put(neg(a) |
printf("\n-a="); put(neg(a)); |
||
printf("\nconj a="); put(conj(a)); printf("\n"); |
|||
}</lang> |
}</lang> |
||