Go 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)isNaN - The result of
Cos(+Inf)isNaN - The result of
Cos(NaN)isNaN
Example
The following calculates the cosine of angle and prints out the result:
package mainimport ("fmt""math")func main() {angle := math.Pi / 6cosine := 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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
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