Arithmetic/Complex: Difference between revisions

Added Wren
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(Added Wren)
Line 4,884:
1 / (4 - 3i) = 0.16 + 0.12i
(29 - 3i) / (4 - 3i) = 5 + 3i</pre>
 
=={{header|Wren}}==
<lang ecmascript>class Complex {
construct new(real, imag) {
_real = real
_imag = imag
}
 
real { _real }
imag { _imag }
 
- { Complex.new(-_real, -_imag) }
 
+ (other) { Complex.new(_real + other.real, _imag + other.imag) }
 
- (other) { this + (-other) }
 
* (other) {
return Complex.new(
_real * other.real - _imag * other.imag,
_real * other.imag + _imag * other.real
)
}
inv {
var denom = _real * _real + _imag * _imag
return Complex.new(_real/denom, -_imag/denom)
}
 
/ (other) { this * other.inv }
 
conj { Complex.new(_real, -_imag) }
 
toString { (_imag >= 0) ? "%(_real) + %(_imag)i" : "%(_real) - %(-_imag)i" }
}
 
var x = Complex.new(1, 3)
var y = Complex.new(5, 2)
System.print("x = %(x)")
System.print("y = %(y)")
System.print("x + y = %(x + y)")
System.print("x - y = %(x - y)")
System.print("x * y = %(x * y)")
System.print("x / y = %(x / y)")
System.print("-x = %(-x)")
System.print("1 / x = %(x.inv)")
System.print("x* = %(x.conj)")</lang>
 
{{out}}
<pre>
x = 1 + 3i
y = 5 + 2i
x + y = 6 + 5i
x - y = -4 + 1i
x * y = -1 + 17i
x / y = 0.37931034482759 + 0.44827586206897i
-x = -1 - 3i
1 / x = 0.1 - 0.3i
x* = 1 - 3i
</pre>
 
=={{header|XPL0}}==
9,485

edits