Finding Tangent of Specified Number in Golang


Golang is a programming language that supports various mathematical functions. One of the functions that Golang supports is the tangent function. The tangent function is used to find the ratio of the opposite side to the adjacent side of a right-angled triangle. In this article, we will discuss how to find the tangent of a specified number in Golang.

Syntax

The syntax for finding the tangent of a number in Golang is −

func Tan(x float64) float64

The Tan function takes a float64 value as an argument and returns a float64 value.

Example 1: Finding Tangent of a Number in Golang

The following code snippet demonstrates how to find the tangent of a number in Golang −

package main

import (
   "fmt"
   "math"
)

func main() {
   x := 45.0
   y := math.Tan(x * math.Pi / 180)
   fmt.Println("Tangent of", x, "degrees is:", y)
}

Output

Tangent of 45 degrees is: 1

In this example, we have used the Tan() function provided by the math package to find the tangent of a number. The angle is converted from degrees to radians using the Pi constant provided by the math package.

Example 2: Finding Tangent of Multiple Numbers in Golang

The following code snippet demonstrates how to find the tangent of multiple numbers in Golang −

package main

import (
   "fmt"
   "math"
)

func main() {
   numbers := []float64{30, 45, 60}
   for _, x := range numbers {
      y := math.Tan(x * math.Pi / 180)
      fmt.Printf("Tangent of %v degrees is: %v\n", x, y)
   }
}

Output

Tangent of 30 degrees is: 0.5773502691896257
Tangent of 45 degrees is: 1
Tangent of 60 degrees is: 1.7320508075688767

In this example, we have used a slice of float64 values to find the tangent of multiple numbers. The angle is converted from degrees to radians using the Pi constant provided by the math package.

Conclusion

In conclusion, finding the tangent of a specified number in Golang is easy and can be achieved using the Tan() function provided by the math package. The function takes a float64 value as an argument and returns a float64 value. The angle needs to be converted from degrees to radians using the Pi constant provided by the math package before finding the tangent.

Updated on: 12-Apr-2023

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements