RosettaCodeData/Task/Arithmetic-Complex/XPL0/arithmetic-complex.xpl0

58 lines
1.1 KiB
Plaintext

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);
]