math/cmplx

Guided tour · Math · pkg.go.dev →

Elementary functions on the complex128 type: Abs, Exp, Log, Sqrt, Phase, trig.

Usage

Euler's identity in three lines

c := cmplx.Exp(1i * math.Pi)
fmt.Println(c)                    // ~(-1+0i)
fmt.Println(cmplx.Abs(c))         // 1

Polar form

z := complex(3, 4)
fmt.Println(cmplx.Abs(z), cmplx.Phase(z))   // 5, atan2(4,3)