How to find the Tangent of a given Radian Value in Golang?


The ratio of the radian’s adjacent side and the opposite side is known as the tangent of the given radian. Golang language has many packages with predefined functions that the developer can use without writing the complete logic. To perform the mathematical operations and logic we have a math package in Golang. We will use this package only to find the Tangent of a given radian value. We will also see how to import the package and also how to call a function this package consists of by writing a Golang code.

Tangent

Definition

Tangent is a function that is part of trigonometry. To understand Tangent observe the diagram below.

If we define Tangent with the help of the above diagram. The angle $\theta$ with function Tangent is equal to the ratio of opposite and adjacent. 


tan$\theta$ = opposite / adjacent

Value of Tangent at different angles

  • tan(0) = 0

  • tan(30) = 1 / √3

  • tan(45) = 1

  • tan(60) = √3

  • tan(90) = Undefined

  • tan(120) = − √3

  • tan(135) = − 1

  • tan(150) = −1 / √3

  • tan(180) = 0

Graph

Now we will see the graph for the Tangent function and observe the above values on the graph. For angle zero the value is zero then till the angle becomes 90 degrees the value is undefined. Then again till 180 degrees, we will get a mirror in the fourth quadrant.

Algorithm

Step 1 − Declaring the variable to store the value of the guardian and answer of float32 type.

Step 2 − Initializing the variable of a radian.

Step 3 − Call the function of Tangent and pass the radian value.

Step 4 − Print the result.

Example 1

In this example, we will write a Golang program in which we will import a math package and call the Tangent function.

package main

import (
    // fmt package provides the function to print anything
    "fmt"

    // math package provides multiple functions for different
    // mathematical operations
    "math"
)

func main() {
    // declaring the variables to store the value of radian value and answer
    var radianValue, answer float64

    fmt.Println("Program to find the Tangent of a given radian value in the Golang programming language using a math package.")

    // initializing the value of the radian value
    radianValue = 4.5

     // finding tangent for the given radian value 
     answer = math.Tan(radianValue)

    // printing the result
    fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}

Output

Program to find the Tangent of a given radian value in the Golang programming language using a math package.
The Tangent value with the value of radian 4.5 is 4.637332054551185

Example 2

In this example, we will write a Golang program in which we will import a math package and call the Tangent function in a separate function and call that function main.

package main

import (
    // fmt package provides the function to print anything
    "fmt"

    // math package provides multiple functions for different
    // mathematical operations
    "math"
)

// this is a function with a parameter of float64 type and a return type of float64
func Tangent(angle float64) float64 {
    // returning the Tangent of the angle
    return math.Tan(angle)
}

func main() {
    // declaring the variables to store the value of radian value and answer
    var radianValue, answer float64

    fmt.Println("Program to find the Tangent of a given radian value in the Golang programming language using a separate function in the same program.")

    // initializing the value of the radian value
    radianValue = 4.5

    // finding factorial of n
    answer = Tangent(radianValue)

     // finding tangent for the given radian value in the separate function
    fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}

Output

Program to find the Tangent of a given radian value in the Golang programming language using a separate function in the same program.
The Tangent value with the value of radian 4.5 is 4.637332054551185

Conclusion

These are two ways to find the Tangent by using the function in the math package and passing the radian value as a parameter. If we compare both approaches then the second approach of creating a separate function will provide abstraction in the program by creating a separate function and also can be reused at different places. To learn more about Golang you can explore these tutorials.

Updated on: 10-Jul-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements