# Finding the Cosine of Complex Number in Golang

The cosine function is a trigonometric function that is used to determine the ratio of the adjacent and hypotenuse sides of a right triangle. When dealing with complex numbers, the cosine function is used to determine the real part of a complex number.

In Golang, the math/cmplx package provides various mathematical functions for complex numbers, including the cosine function. In this article, we will explore how to find the cosine of a complex number in Golang.

## Syntax

The syntax to find the cosine of a complex number in Golang is as follows −

cosine := cmplx.Cos(complexNumber)


Where −

• Cosine is the real part of the complex number.

• complexNumber is the complex number whose cosine is to be found.

## Example 1

Let's find the cosine of a complex number (3+4i) using the cmplx.Cos() function in Golang.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
complexNumber := complex(3, 4)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}


## Output

The cosine of (3+4i) is (-27.034945603074224-3.851153334811777i)


## Example 2

Now let's find the cosine of a different complex number (1+2i) using the cmplx.Cos() function.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
complexNumber := complex(1, 2)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}


## Output

The cosine of (1+2i) is (2.0327230070196656-3.0518977991518i)


## Example 3

Let's find the cosine of another complex number (-2+3i) using the cmplx.Cos() function.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
complexNumber := complex(-2, 3)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}


## Output

The cosine of (-2+3i) is (-4.189625690968807+9.109227893755337i)


## Conclusion

The cosine of a complex number is the real part of that complex number. In Golang, we can use the cmplx.Cos() function to find the cosine of a complex number. The cmplx.Cos() function takes a complex number as input and returns the real part of the corresponding cosine value. We can use the cmplx package to perform various mathematical operations on complex numbers in Golang.

Updated on: 12-Apr-2023

66 Views