For working with complex numbers, this is how I've been doing it:
import Foundation
class Complex {
var real: Float
var imaginary: Float
init(re: Float, im: Float) {
self.imaginary = im
self.real = re
}
func abs() -> Float {
return sqrtf(powf(self.real, 2) + powf(self.imaginary, 2))
}
func string() -> String {
if (ceilf(self.real) == self.real) && (ceilf(self.imaginary) == self.imaginary){
return "\(Int(self.real))+\(Int(self.imaginary))i"
}
return "\(self.real)+\(self.imaginary)i"
}
func arg() -> Float {
return atan2f(self.imaginary, self.real)
}
}
var someComplex = Complex(re: 2, im: 3)
var someComplexString = someComplex.string() //"2+3i"
var someComplexAbsolute = someComplex.abs() // 3.60...
var someComplexArgument = someComplex.arg() //0.98...
Now I was wondering, if there was any way to define a custom type of variable that would let me write it as someComplex: Complex = 3i for example. Is it possible to create a new type "from the ground up"?
valueas anAnyObjectorAnytype.SomeClass, and variables, even in initializers, start with a lowercase letter, i.e.init(stringValue: String)SomeClass.self