Search code examples
complex-numbersswift

Complex numbers in Swift?


Does Apple's Swift language support complex numbers out of the box?

I couldn't find any mention in the docs any equivalent for C++ std::complex nor could I find one when playing with it.


Solution

  • I wrote the following:

    https://github.com/dankogai/swift-complex

    Just add complex.swift to your project and you can go like:

    let z = 1-1.i
    

    It has all functions and operators that of C++11 covers.

    Unlike C++11 complex.swift is not generic -- z.real and z.imag are always Double.

    But the necessity for complex integer is very moot and IMHO it should be treated in different type (GaussianInteger, maybe).