File:  [CSRG BSD Unix] / 43BSD / contrib / icon / src / lib / complex.icn
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:55 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43
BSD 4.3

#	COMPLEX(2)
#
#	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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.