structure Complex : ComplexSig = struct type complex = real * real; fun r X (a,b) : complex = (r*a, r*b) fun (a,b) ++ (c,d): complex = (a+c, b+d) and (a,b) -- (c,d): complex = (a-c, b-d) and (a,b) ** (c,d): complex = (a*c - b*d, b*c + a*d) and (a,b) // (c,d): complex = let val factor = 1.0/(c*c + d*d) in factor X (a,b) ** (c,~d) end and ~~(a,b) : complex = (~a,~b) and (a,b) == (c,d) = (a=c) andalso (b=d) val pi = 4.0 * arctan 1.0; fun descartes{real, imaginary} = (real,imaginary) and argand {modulus, argument} = modulus X (cos argument, sin argument) and realpart (real, _) = real and imagpart (_, imag) = imag and modulus (r, i) = sqrt(r*r + i*i) and argument(r, i) = if r = 0.0 then if i < 0.0 then ~pi/2.0 else pi/2.0 else arctan(i/r) end;