Arithmetic/Complex: Difference between revisions

Content added Content deleted
(Added Arturo implementation)
(Arithmetic/Complex en BASIC - añadidas la conjugación compleja, resta y división)
Line 284: Line 284:
imag AS DOUBLE
imag AS DOUBLE
END TYPE
END TYPE

DECLARE SUB add (a AS complex, b AS complex, c AS complex)
DECLARE SUB suma (a AS complex, b AS complex, c AS complex)
DECLARE SUB rest (a AS complex, b AS complex, c AS complex)
DECLARE SUB mult (a AS complex, b AS complex, c AS complex)
DECLARE SUB mult (a AS complex, b AS complex, c AS complex)
DECLARE SUB inv (a AS complex, b AS complex)
DECLARE SUB divi (a AS complex, b AS complex, c AS complex)
DECLARE SUB neg (a AS complex, b AS complex)
DECLARE SUB neg (a AS complex, b AS complex)
DECLARE SUB inv (a AS complex, b AS complex)
DECLARE SUB conj (a AS complex, b AS complex)

CLS
CLS
DIM x AS complex
DIM x AS complex
Line 296: Line 301:
y.real = 2
y.real = 2
y.imag = 2
y.imag = 2

CALL add(x, y, z)
PRINT z.real; "+"; z.imag; "i"
PRINT "Siendo x = "; x.real; "+"; x.imag; "i"
PRINT " e y = "; y.real; "+"; y.imag; "i"
PRINT
CALL suma(x, y, z)
PRINT "x + y = "; z.real; "+"; z.imag; "i"
CALL rest(x, y, z)
PRINT "x - y = "; z.real; "+"; z.imag; "i"
CALL mult(x, y, z)
CALL mult(x, y, z)
PRINT z.real; "+"; z.imag; "i"
PRINT "x * y = "; z.real; "+"; z.imag; "i"
CALL inv(x, z)
CALL divi(x, y, z)
PRINT z.real; "+"; z.imag; "i"
PRINT "x / y = "; z.real; "+"; z.imag; "i"
CALL neg(x, z)
CALL neg(x, z)
PRINT z.real; "+"; z.imag; "i"
PRINT " -x = "; z.real; "+"; z.imag; "i"
CALL inv(x, z)

PRINT "1 / x = "; z.real; "+"; z.imag; "i"
CALL conj(x, z)
PRINT " x* = "; z.real; "+"; z.imag; "i"
END


SUB add (a AS complex, b AS complex, c AS complex)
SUB suma (a AS complex, b AS complex, c AS complex)
c.real = a.real + b.real
c.real = a.real + b.real
c.imag = a.imag + b.imag
c.imag = a.imag + b.imag
Line 325: Line 340:
b.real = -a.real
b.real = -a.real
b.imag = -a.imag
b.imag = -a.imag
END SUB</lang>
END SUB

SUB conj (a AS complex, b AS complex)
b.real = a.real
b.imag = -a.imag
END SUB

SUB divi (a AS complex, b AS complex, c AS complex)
c.real = ((a.real * b.real + b.imag * a.imag) / (b.real ^ 2 + b.imag ^ 2))
c.imag = ((a.imag * b.real - a.real * b.imag) / (b.real ^ 2 + b.imag ^ 2))
END SUB

SUB rest (a AS complex, b AS complex, c AS complex)
c.real = a.real - b.real
c.imag = a.imag - b.imag
END SUB
</lang>
{{out}}
{{out}}
<pre> 3 + 3 i
<pre>Siendo x = 1+ 3i
e y = 5+ 2i
0 + 4 i

.5 +-.5 i
x + y = 6 + 5 i
-1 +-1 i</pre>
x - y = -4 + 1 i
x * y = -1 + 17 i
x / y = .3793103448275862 + .4482758620689655 i
-x = -1 +-3 i
1 / x = .1 +-.3 i
x* = 1 +-3 i</pre>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==