Search code examples
gonumberscomplextype

What's the difference between "real" and "imaginary" parts of a complex numer in Go?


I was reading Go's documentation for the complex128 and complex64 data types when I came across something odd:

"complex128 is the set of all complex numbers with float64 real and imaginary parts."

And:

"complex64 is the set of all complex numbers with float32 real and imaginary parts."

More specifically:

"real and imaginary parts."

What's meant by this? How can a number be "real" or "imaginary"?


Solution

  • The question isn't dedicated to GoLang, to be honest.

    Complex numbers are a mathematical concept.

    Here is an example:

    import (
      "fmt"
      "math/cmplx"
    )
    func main() {
      fmt.Println(cmplx.Sqrt(-1))
    }
    

    Expected output:

    (0+1i)
    

    There is a package named "cmplx" to work with complex numbers. So Sqrt of cmplx is similar to math one, but it returns a complex number instead.

    As you see, and output consists of 0 and 1i, and the last one is an imaginary part as we are not able to get a square root of "-1".