A
A
Abn302020-04-27 00:41:12
C++ / C#
Abn30, 2020-04-27 00:41:12

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;
}

But everything will work out, but there is no imaginary unit.
How to add it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-04-27
@jcmvbkbc

how to add an imaginary unit?

complex_t I = {0., 1.};

R
res2001, 2020-04-27
@res2001

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 question

Ask a Question

731 491 924 answers to any question