take the cosine of a number in go

To take the cosine of a number in Go programming language, we use the math.Cos() function. This function takes a float64 type argument and returns its cosine value as a float64 type value.

Here is an example code snippet that shows how to use the math.Cos() function in Go:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    // taking the cosine of pi/2 (90 degrees)
    angle := math.Pi / 2
    cosine := math.Cos(angle)

    fmt.Printf("cos(%.2f) = %.2f", angle, cosine)
}
215 chars
15 lines

Output:

main.go
cos(1.57) = 0.00
17 chars
2 lines

In the above code, we first import the math package to access the Cos() function. Then, we define an angle variable angle and set its value to math.Pi / 2, which is 90 degrees in radians. We then pass this angle value to the math.Cos() function, which returns its cosine value as cosine. Finally, we print the angle and cosine values using fmt.Printf() function.

gistlibby LogSnag