Finding the Tangent of Complex Number in Golang

The tangent of a complex number is defined as the ratio of the sine of the complex number to the cosine of the complex number. In Golang, the cmplx package provides a built-in function called Tanh to find the tangent of a complex number. In this article, we will explore how to find the tangent of a complex number in Golang with the help of examples.

What is a Complex Number?

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit. The imaginary unit i is defined as the square root of -1. A complex number can be represented as a point in a two-dimensional plane known as the complex plane.

What is the Tangent of a Complex Number?

The tangent of a complex number z is defined as the ratio of the sine of z to the cosine of z. It is represented as tan(z) = sin(z) / cos(z).

Finding the Tangent of a Complex Number in Golang

To find the tangent of a complex number in Golang, we can use the Tanh function provided by the cmplx package. The Tanh function takes a complex number as its argument and returns the tangent of that complex number.

Example 1: Finding the Tangent of a Real Number

Let's consider an example of finding the tangent of a real number using Golang. Suppose we want to find the tangent of x = 2. Here's the code snippet to accomplish this −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
x := complex(2, 0)

// Finding the tangent of the complex number
tanX := cmplx.Tanh(x)

// Displaying the result
fmt.Println("Tangent of", x, "is", tanX)
}

Output

Tangent of (2+0i) is (0.9640275800758168+0i)

In this example, we first create a real number x and then use the cmplx.Tanh() function to find its tangent and store the result in tanX.

Example 2: Finding the Tangent of a Purely Imaginary Number

Let's consider an example of finding the tangent of a purely imaginary number using Golang. Suppose we want to find the tangent of y = 2i. Here's the code snippet to accomplish this −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
y := 2i

// Finding the tangent of the complex number
tanY := cmplx.Tanh(y)

// Displaying the result
fmt.Println("Tangent of", y, "is", tanY)
}

Output

Tangent of (0+2i) is (0-2.185039863261519i)

In this example, we first create a purely imaginary number y and then use the cmplx.Tanh() function to find its tangent and store the result in tanY.

Example 3: Finding the Tangent of a Complex Number

Let's consider an example of finding the tangent of a complex number using Golang. Suppose we want to find the tangent of z = 3+4i. Here's the code snippet to accomplish this −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
z := complex(3, 4)

// Finding the tangent of the complex number
tanZ := cmplx.Tan(z)

// Displaying the result
fmt.Println("Tangent of", z, "is", tanZ)
}

Output

Tangent of (3+4i) is (-0.00018734620462947842+0.9993559873814732i)

In this example, we first create a complex number z and then use the cmplx.Tan() function to find its tangent and store the result in tanZ.

Conclusion

In this article, we explored how to find the tangent of a complex number using Golang. We covered examples of finding the tangent of a real number, a purely imaginary number, and a complex number. The "math/cmplx" package provides a wide range of mathematical functions to work with complex numbers in Golang.

Updated on: 17-Apr-2023

70 Views

Get certified by completing the course