Library contents

Go math.Cos

In Go, math.Cos takes an angle in radians and returns its cosine. In a right triangle, cosine is the adjacent side divided by the hypotenuse.

A simple example

math.Cos

  1. Start with 0 radians
  2. Do this Find the horizontal component
  3. Result 1
At angle zero a unit line points horizontally. Its full length lies on the horizontal axis.
main.go
package main

import "fmt"
import "math"

func main() {
    fmt.Println(math.Cos(0.0))
}
Output
1

Syntax

Go syntax
math.Cos(angle)
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Find the horizontal reach of an awning

A designer is positioning an awning over a window. Relate the length of its arm to the horizontal distance it covers so a drawing can show the reach at a chosen angle.

From the programangle := 1.0471975511965976
Result0.500
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    angle := 1.0471975511965976

    fmt.Printf("%.3f\n", math.Cos(angle))
}
Output
0.500

At 60 degrees the horizontal reach is half the length of the arm.

Advertisements

Check the reach of a level awning

A builder sketches an awning with its arm completely level. Use that position as a reference when checking the horizontal projection produced by the drawing calculation.

From the programangle := 0.0
Result1.000
main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    angle := 0.0

    fmt.Printf("%.3f\n", math.Cos(angle))
}
Output
1.000

A horizontal arm has a horizontal reach equal to its full length.

Find a sign arm’s horizontal reach

A scale model for a shop sign has a 2 m arm raised 30° from horizontal. The artist wants the distance between its tip and the wall along the ground.

GivenLength 2 m · angle 30°
Worked result1.73 m

Cosine gives the adjacent side as a fraction of the arm length.

What to watch for

The angle is measured from the horizontal and supplied in radians.

Documentation for math.Cos