Arithmetic/Complex: Difference between revisions

→‎{{header|Wren}}: Now uses new Wren-complex module.
(→‎{{header|Wren}}: Now uses new Wren-complex module.)
Line 5,030:
 
=={{header|Wren}}==
{{libheader|Wren-complex}}
<lang ecmascript>classimport Complex"/complex" {for 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)
Line 5,073 ⟶ 5,042:
System.print("x / y = %(x / y)")
System.print("-x = %(-x)")
System.print("1 / x = %(x.invinverse)")
System.print("x* = %(x.conj)")</lang>
 
9,482

edits