How to find the cosine of a given radian value in Golang?



In this tutorial, we will learn how to find the cosine of a given radian value in the Golang programming Language. 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 cosine 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.

Cosine

Definition

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


If we define cosine with the help of the above diagram. The angle Ө with function cosine is equal to the ratio of adjacent and hypotenuse.

cosӨ = adjacent / hypotenuse

Value of cosine at different angles

  • cos(0) = 1

  • cos(30) = √3 / 2

  • cos(45) = 1 / √2

  • cos(60) = 1 / 2

  • cos(90) = 0

  • cos(120) = -1 / 2

  • cos(135) = - 1 / √2

  • cos(150) = -√3 / 2

  • cos(180) = - 1

Graph

Now we will see the graph for the cosine function and observe the above values on the graph. For angle zero the value is one then till the angle becomes 90 degrees the value is zero. 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 cosine and pass the radian value.

Step 4 - Printing the result.

Example

In this example, we will write a Golang program in which we will import a math package and call the cosine 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 cosine of a given radian value in the Golang programming language using a math package.")
   
   // initializing the value of radian value
   radianValue = 4.5
   
   // finding cosine for the given radian value
   answer = math.Cos(radianValue)
   
   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

Output

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

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 cosine defined by us and pass the radian value as a parameter.

Step 4 - Printing the result.

Example

In this example, we will write a Golang program in which we will import a math package and call the cosine 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 Cosine(angle float64) float64 {
   
   // returning the cosine of the angle
   return math.Cos(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 cosine 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 cosine for the given radian value in separate function
   answer = Cosine(radianValue)
   
   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

Output

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

Conclusion

These are two ways to find the cosine by using the function in the math package and passing the radian value as a parameter. The second approach will provide abstraction in the program. To learn more about Golang you can explore these tutorials.


Advertisements