Arithmetic/Complex: Difference between revisions

Add XPL0
(Add XPL0)
Line 2,923:
3.785e+00-1.969e+00j,
8.066e-02+2.793e-01j></pre>
 
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes;
 
func real CAdd(A, B, C); \Return complex sum of two complex numbers
real A, B, C;
[C(0):= A(0) + B(0);
C(1):= A(1) + B(1);
return C;
];
 
func real CMul(A, B, C); \Return complex product of two complex numbers
real A, B, C;
[C(0):= A(0)*B(0) - A(1)*B(1);
C(1):= A(1)*B(0) + A(0)*B(1);
return C;
];
 
func real CNeg(A, C); \Return negative of a complex number
real A, C;
[C(0):= -A(0);
C(1):= -A(1);
return C;
];
 
func real CInv(A, C); \Return inversion (reciprical) of complex number
real A, C;
real D;
[D:= sq(A(0)) + sq(A(1));
C(0):= A(0)/D;
C(1):=-A(1)/D;
return C;
];
 
func real Conj(A, C); \Return conjugate of a complex number
real A, C;
[C(0):= A(0);
C(1):=-A(1);
return C;
];
 
proc COut(D, A); \Output a complex number to specified device
int D; real A;
[RlOut(D, A(0));
Text(D, if A(1)>=0.0 then " +" else " -");
RlOut(D, abs(A(1)));
ChOut(D, ^i);
];
 
real U, V, W(2);
[Format(2,2);
U:= [1.0, 1.0];
V:= [3.14, 1.2];
COut(0, CAdd(U,V,W)); CrLf(0);
COut(0, CMul(U,V,W)); CrLf(0);
COut(0, CNeg(U,W)); CrLf(0);
COut(0, CInv(U,W)); CrLf(0);
COut(0, Conj(U,W)); CrLf(0);
]</lang>
 
Output:
<pre>
4.14 + 2.20i
1.94 + 4.34i
-1.00 - 1.00i
0.50 - 0.50i
1.00 - 1.00i
</pre>
 
{{omit from|M4}}
772

edits