Oxygen Basic
Programming => Example Code => General => Topic started by: Charles Pegge on April 29, 2013, 03:18:41 AM
-
'COMPLEX OPERATIONS
=============
class Complex
=============
double x,y
method set(double*bx,*by)
x=bx
y=by
end method
method set(complex*b)
x=b.x
y=b.y
end method
method get(double*bx,*by)
bx=x
by=y
end method
method get(complex*b)
b.x=x
b.y=y
end method
method add(complex*b)
x+=b.x
y+=b.y
end method
method sub(complex*b)
x-=b.x
y-=b.y
end method
method mul(complex*b)
double d
d=x
x = x * b.x - y * b.y
y = y * b.x + d * b.y
end method
method div(complex*b)
double d,v
v=1/(b.x * b.x + b.y * b.y)
d=x
x = (x * b.x + y * b.y) * v
y = (y * b.x - d * b.y) * v
end method
method power(double n)
'Using DeMoivre theorem
double r,an,mg
r = hypot(x,y)
mg = r^n
if x=0 then
an=.5*pi '90 degrees
if y<0 then an=-an
else
an = atan(y,x)
end if
an *= n
x = mg * cos(an)
y = mg * sin(an)
end method
method show() as string
return str(x,14) ", " str(y,14)
end method
end class
complex ca,cb,cc
cc.set 1,2
cc.add ca
cc.mul cb
-
where is the "+= ,+ ,*= ,......" symbol operator for that class so that we can do the following.
complex ca,cb,cc
cc.set 1,2
cc += ca this is the same as cc.add ca i mean with operator symbol we will call the same method
cc *= cb this the same as cc.mul cb
-
This code was rescued and adapted from OOP/operator (overloading). They are now plain methods.
They are in single operand format but you can rearrange or extend them as required. Multiplication, Division and Power are the tricky ones, which are hard to remember.