Arithmetic/Complex: Difference between revisions

Added Wortel
No edit summary
(Added Wortel)
Line 3,327:
3.785e+00-1.969e+00j,
8.066e-02+2.793e-01j></pre>
 
=={{header|Wortel}}==
<lang wortel>@class Complex[] {
&[r i] @: {
^r || r 0
^i || i 0
^m +@sq^r @sq^i
}
add &o @new Complex[+ ^r o.r + ^i o.i]
mul &o @new Complex[-* ^r o.r * ^i o.i +* ^r o.i * ^i o.r]
neg &^ @new Complex[@-^r @-^i]
inv &^ @new Complex[/ ^r ^m / @-^i ^m]
toString &^?{
=^i 0 "{^r}"
=^r 0 "{^i}i"
>^i 0 "{^r} + {^i}i"
"{^r} - {@-^i}i"
}
}
 
@vars {
a @new Complex[5 3]
b @new Complex[4 3N]
}
@each &x !console.log x [
"({a}) + ({b}) = {!a.add b}"
"({a}) * ({b}) = {!a.mul b}"
"-1 * ({b}) = {b.neg.}"
"({a}) - ({b}) = {!a.add b.neg.}"
"1 / ({b}) = {b.inv.}"
"({!a.mul b}) / ({b}) = {`!.mul b.inv. !a.mul b}"
]</lang>
Output:
<pre>(5 + 3i) + (4 - 3i) = 9
(5 + 3i) * (4 - 3i) = 29 - 3i
-1 * (4 - 3i) = -4 + 3i
(5 + 3i) - (4 - 3i) = 1 + 6i
1 / (4 - 3i) = 0.16 + 0.12i
(29 - 3i) / (4 - 3i) = 5 + 3i</pre>
 
=={{header|XPL0}}==
Anonymous user