# COMPLEX # # Complex arithmetic # # Ralph E. Griswold # # Last modified 7/27/83 # record complex(rpart,ipart) procedure strcpx(s) i := upto('+-',s,2) return complex(+s[1:i],+s[i:-1]) end procedure cpxstr(x) if x.ipart < 0 then return x.rpart || x.ipart || "i" else return x.rpart || "+" || x.ipart || "i" end procedure cpxadd(x1,x2) return complex(x1.rpart + x2.rpart,x1.ipart + x2.ipart) end procedure cpxsub(x1,x2) return complex(x1.rpart - x2.rpart,x1.ipart - x2.ipart) end procedure cpxmul(x1,x2) return complex(x1.rpart * x2.rpart - x1.ipart * x2.ipart, x1.rpart * x2.ipart + x1.ipart * x2.rpart) end procedure cpxdiv(x1,x2) denom := x2.rpart ^ 2 + x2.ipart ^ 2 return complex((x1.rpart * x2.rpart + x1.ipart * x2.ipart) / denom,(x1.ipart * x2.rpart - x1.rpart * x2.ipart) / denom) end