How to find the Arc Cosine of a given value in Golang?


In this tutorial, we will learn how to find the Arc Cosine of a given 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 Arc Cosine of a given 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.

Arc Cosine

Definition

Arc Cosine is a function similar to trigonometry functions. Arc Cosine is equal to the inverse of value x. The formula for Arc Cosine is written below and the value of Arc Cosine is at different angles.

Syntax

X = cos^-1(Ө)

Graph


Value of Arc Cosine at different angles

  • cos^-1(1) = 0

  • cos^-1(√3/2) = ㄫ / 6

  • cos^-1(1/√2) = ㄫ / 4

  • cos^-1(1/2) = ㄫ / 3

  • cos^-1(0) = ㄫ / 2

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 value.

Step 3 − Call the function of Arc Cosine and pass the 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 Arc 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 \value and answer
   var value, answer float64
   fmt.Println("Program to find the Arc Cosine of a given value in the Golang programming language using a math package.")
   
   // initializing the value of the variable value
   value = 0
   
   // finding Arc Cosine for the given value
   answer = math.Acos(value)
   
   // printing the result 
   fmt.Println("The Arc Cosine value with the value of", value, "is", answer)
} 

Output

Program to find the Arc Cosine of a given value in the Golang programming language using a math package.
The Arc Cosine value with the value of 0 is 1.5707963267948966

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 value.

Step 3 − Call the function of Arc Cosine defined by us and pass the 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 Arc 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 ArcCosine(angle float64) float64 {
   
   // returning the Arc Cosine of the angle
   return math.Acos(angle)
}
func main() {
   
   // declaring the variables to store the value of value and answer 
   var value, answer float64
   fmt.Println("Program to find the Arc Cosine of a given value in the Golang programming language using a separate function in the same program.")
   
   // initializing the value of the value
   value = 0
   
   // finding Arc Cosine for the given value in a separate function
   answer = ArcCosine(value)
   
   // printing the result
   fmt.Println("The Arc Cosine value with the value of", value, "is", answer)
}

Output

Program to find the Arc Cosine of a given value in the Golang programming language using a separate function in the same program.
The Arc Cosine value with the value of 0 is 1.5707963267948966

Conclusion

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

Updated on: 29-Nov-2022

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements