Arithmetic/Complex: Difference between revisions

→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 2,384:
 
=={{header|Kotlin}}==
<lang scala>class Complex(private val real: Double, private val imag: Double) {
<lang scala>// version 1.0.5-2
operator fun plus(other: Complex) return= Complex(this.real *+ other.real, - this.imag *+ other.imag,)
 
operator fun times(other: Complex): Complex= {Complex(
class Complex(val real: Double, val imag: Double) {
operator fun plus( real * other:.real Complex):- Compleximag {* other.imag,
return Complex(this.real +* other.real, this.imag + imag * other.imag)real
})
 
operator fun times(other: Complex): Complex {
return Complex(this.real * other.real - this.imag * other.imag,
this.real * other.imag + this.imag * other.real)
}
 
fun inv(): Complex {
val denom : Double = this.real * this.real + this.imag * this.imag
return Complex(this.real / denom, -this.imag / denom)
}
 
operator fun unaryMinus() := Complex(-real, {-imag)
return Complex(-this.real, -this.imag)
}
 
operator fun minus(other: Complex): Complex= {this + (-other)
return this + (-other)
}
 
operator fun div(other: Complex): Complex= {this * other.inv()
return this * other.inv()
}
 
fun conj(): = Complex(real, {-imag)
return Complex(this.real, -this.imag)
}
 
override fun toString(): String {=
if (this.imag >= 0.0) "$real + ${imag}i"
returnelse "${this.real} +- ${this.-imag}i"
else
return "${this.real} - ${-this.imag}i"
}
}
 
fun main(args: Array<String>) {
val x = Complex(1.0, 3.0)