Answer the question
In order to leave comments, you need to log in
How to add imaginary unit in C language?
I wrote a small program with complex numbers in C language, but I can't figure out how to add an imaginary unit? Here is a small part of the program:
typedef struct {
double re, im;
}complex_t;
complex_t ComplexAdd(complex_t z1,complex_t z2){
complex_t res;
res.re = z1.re + z2.re;
res.im =z1.im + z2.im;
return res;
}
int main(){
complex_t z1,z2;
z1.re=2;z1.im =5;
z2.re=3;z2.im =1;
ComplexAdd(z1,z2);
return 0;
}
Answer the question
In order to leave comments, you need to log in
The standard already has complex numbers:
https://en.cppreference.com/w/c/language/arithmeti...
Why don't you use them?
True, not all compilers fully support them.
About a year ago, I tried to make friends with the Mikrosovt compiler code with standard complex numbers, normally assembled by gcc. I didn't succeed, I had to make my own crutches, but in general it was not difficult.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question