#### Archived

This topic is now archived and is closed to further replies.

# Complex numbers <-> Real Numbers

This topic is 6934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anyone know how to implement complex numbers in c and then change them back into real numbers when needed?

##### Share on other sites
Hmmm... converting complex to real... well I think I understand what you mean... here''s my 2 cents
------------------------------------------
say you had a complex number c = a + bi
you can find the magnitude
/c/ = sqrt(a^2 + b^2)
and the angle
angle = atan2(b,a)
------------------------------------------
or from angle and /c/ (the other way around)
c = /c/*cos(angle) + /c/*sin(angle)i
--------------------------------------------
one way to implement a complex number in C would be
to define a structure
typedef complexnumber
{
int a; // real part
int b; // imaginary part
} complexnumber, *complexnumber_ptr;

complexnumber myfirstcomplexno;

myfirstcomplexno.a = 10;
myfirstcomplexno.b = 3;
---------------------------------------
so the number would be c = a + bi = 10 + 3i
and write functions to convert and perform operations

Hope this helps,

Nick

"... you act as if stupidity were a virtue."
-- Flight of the Phoenix

##### Share on other sites
NickGA, ints are no good idea for a and b; they should be real.
typedef complexnumber{float a; // real partfloat b; // imaginary part} complexnumber, *complexnumber_ptr;
btw, ln(a+bi) = sqrt(a^2+b^2) + i*arctan(b/a)
logc+di(a+bi) = ln(a+bi)/ln(c+di)
e^(a+bi) = e^a*(cos(b) + i*sin(b))
(c+di)^(a+bi) = e^(ln(c+di)*(a+bi))
sin(a+bi) = sin(a)cosh(b) + i*cos(a)sinh(b)
cos(a+bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
....
all those functions are defined in the complex numbers, but I don''t think you''ll need them *g*

GA

• 15
• 13
• 35
• 39