Cos()

Published Jun 20, 2023
Contribute to Docs

The Cos() function returns the cosine of the given angle (in radians). The math library must be imported in order to use this function.

Syntax

result := math.Cos(angle)

Where result is the cosine value of angle, returned as a float, except under the following circumstances:

  • The result of Cos(-Inf) is NaN
  • The result of Cos(+Inf) is NaN
  • The result of Cos(NaN) is NaN

Example

The following calculates the cosine of angle and prints out the result:

package main
import (
"fmt"
"math"
)
func main() {
angle := math.Pi / 6
cosine := math.Cos(angle)
fmt.Printf("%.1f\n", cosine)
}

The output will be:

0.9

Codebyte Example

The following example is runnable and shows how the Cos() function handles infinite values.

Code
Output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy