Go Cos()

4hbab's avatar
Published Jun 20, 2023

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

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

All contributors

Learn Go on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours