# Golang : Calculations using complex numbers example

Tags : golangcomplexrealimaginary-unit sine cosine conjugate

Alright, you need to calculate the last known position of UFOs and their flying trajectory in Golang or you just want to perform some calculations using complex numbers.

Declaring complex numbers and performing calculations on the numbers is simple in Golang. Pretty much similar to how Python declare complex numbers.

In Python:

```
>>> a = complex(100,8)
>>> a
(100+8j)
>>> a.real
100.0
>>> a.imag
8.0
```

In Golang and some examples on how to perform calculations with `math/cmplx`

package:

```
package main
import (
"fmt"
"math/cmplx"
)
const a = complex(100, 8)
const b = complex(8, 100)
func main() {
fmt.Println("Complex number a : ", a)
fmt.Println("Complex number b : ", b)
fmt.Println("Get the real part of complex number a : ", real(a))
fmt.Println("Get the imaginary part of complex number b : ", imag(a))
conjugate := cmplx.Conj(a)
fmt.Println("Complex number a's conjugate : ", conjugate)
c := a + b
fmt.Println("a + b complex number : ", c)
fmt.Println("Cosine of complex number b : ", cmplx.Cos(b))
// see https://golang.org/pkg/math/cmplx/
// for more functions such as sine, log, exponential
}
```

Happy coding!

**References:**

https://en.wikipedia.org/wiki/Complex_number

https://golang.org/pkg/builtin/#complex

https://golang.org/pkg/math/cmplx/

https://www.socketloop.com/references/golang-builtin-complex-function-example

Tags : golangcomplexrealimaginary-unit sine cosine conjugate

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+2.9k Golang : Get YouTube playlist

+1.4k Golang : Find the shortest line of text example

+2.6k Golang : Gorrila set route name and get the current route name

+3.2k Golang : Load ASN1 encoded DSA public key PEM file example

+2.5k Golang: Prevent over writing file with md5 hash

+3.4k Golang : Natural string sorting example

+4k Use systeminfo to find out installed Windows Hotfix(s) or updates

+5.4k Swift : Convert (cast) Float to String

+18.9k Golang : Move file to another directory

+4.6k Golang : How to check if a file is hidden?

+19.8k Golang : Convert date or time stamp from string to time.Time type

+3.2k Golang : Check to see if *File is a file or directory