What is the Cos function in Golang?

The Cos function in the Go programming language is used to find the cosine value of a number.

To use this function, you must import the math package in your file and access the Cos function within it using the . notation (math.Cos). Here, Cos is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Cos function inside the math package is:

Parameters

The Cos function takes a single argument of type float64. This argument represents the angle whose cosine value you want to find.

Return value

The Cos function returns a single value of type float64. This value represents the cosine value of the argument. The possible range of all return values is between -1 and 1 (both inclusive).

An exception to the above statements is when you pass something that is +infinity, - infinity, or NAN as an argument. In these cases, the Cos function returns NAN.

Examples

Following is a simple example where we find out the cosine value of 25:

package main
import (
"fmt"
"math"
)
func main() {
x := 25.0
y := math.Cos(x)
fmt.Print(y)
}

The following example shows how passing an infinite number as an argument makes the Cos function return NAN:

package main
import (
"fmt"
"math"
)
func main() {
y := math.Cos(math.Inf(1))
fmt.Print(y)
}
Copyright ©2024 Educative, Inc. All rights reserved