A   complex number   is a number which can be written as:




a
+
b
×
i


{\displaystyle a+b\times i}


(sometimes shown as:




b
+
a
×
i


{\displaystyle b+a\times i}


where   



a


{\displaystyle a}

   and   



b


{\displaystyle b}

  are real numbers,   and   



i


{\displaystyle i}

   is   √ -1 



Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part",   where the imaginary part is the number to be multiplied by 



i


{\displaystyle i}

.



Task

 Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.) 
 Print the results for each operation tested.
 Optional: Show complex conjugation.


By definition, the   complex conjugate   of




a
+
b
i


{\displaystyle a+bi}


is




a
−
b
i


{\displaystyle a-bi}





Some languages have complex number libraries available.   If your language does, show the operations.   If your language does not, also show the definition of this type.




TYPE complex
        real AS DOUBLE
        imag AS DOUBLE
END TYPE
 
DECLARE SUB suma (a AS complex, b AS complex, c AS complex)
DECLARE SUB rest (a AS complex, b AS complex, c AS complex)
DECLARE SUB mult (a AS complex, b AS complex, c AS complex)
DECLARE SUB divi (a AS complex, b AS complex, c AS complex)
DECLARE SUB neg (a AS complex, b AS complex)
DECLARE SUB inv (a AS complex, b AS complex)
DECLARE SUB conj (a AS complex, b AS complex)
 
CLS
DIM x AS complex
DIM y AS complex
DIM z AS complex
x.real = 1
x.imag = 1
y.real = 2
y.imag = 2
 
PRINT "Siendo x = "; x.real; "+"; x.imag; "i"
PRINT "     e y = "; y.real; "+"; y.imag; "i"
PRINT
CALL suma(x, y, z)
PRINT "x + y = "; z.real; "+"; z.imag; "i"
CALL rest(x, y, z)
PRINT "x - y = "; z.real; "+"; z.imag; "i"
CALL mult(x, y, z)
PRINT "x * y = "; z.real; "+"; z.imag; "i"
CALL divi(x, y, z)
PRINT "x / y = "; z.real; "+"; z.imag; "i"
CALL neg(x, z)
PRINT "   -x = "; z.real; "+"; z.imag; "i"
CALL inv(x, z)
PRINT "1 / x = "; z.real; "+"; z.imag; "i"
CALL conj(x, z)
PRINT "   x* = "; z.real; "+"; z.imag; "i"
END
 
SUB suma (a AS complex, b AS complex, c AS complex)
        c.real = a.real + b.real
        c.imag = a.imag + b.imag
END SUB
 
SUB inv (a AS complex, b AS complex)
        denom = a.real ^ 2 + a.imag ^ 2
        b.real = a.real / denom
        b.imag = -a.imag / denom
END SUB
 
SUB mult (a AS complex, b AS complex, c AS complex)
        c.real = a.real * b.real - a.imag * b.imag
        c.imag = a.real * b.imag + a.imag * b.real
END SUB
 
SUB neg (a AS complex, b AS complex)
        b.real = -a.real
        b.imag = -a.imag
END SUB
 
SUB conj (a AS complex, b AS complex)
    b.real =  a.real
    b.imag = -a.imag
END SUB
 
SUB divi (a AS complex, b AS complex, c AS complex)
    c.real = ((a.real * b.real + b.imag * a.imag) / (b.real ^ 2 + b.imag ^ 2))
    c.imag = ((a.imag * b.real - a.real * b.imag) / (b.real ^ 2 + b.imag ^ 2))
END SUB
 
SUB rest (a AS complex, b AS complex, c AS complex)
    c.real = a.real - b.real
    c.imag = a.imag - b.imag
END SUB
 