take the arccosine of a number in go

To take the arccosine (inverse cosine) of a number in Go, you can use the math package's Acos function.

Here's an example code snippet:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    x := 0.5 // some number between -1 and 1
    result := math.Acos(x)

    // the result is in radians, so you may want to convert to degrees
    degrees := result * 180 / math.Pi

    fmt.Printf("The arccosine of %v is %.2f radians or %.2f degrees.", x, result, degrees)
}
337 chars
17 lines

The output of this code would be:

main.go
The arccosine of 0.5 is 1.05 radians or 60.00 degrees.
55 chars
2 lines

Note that the argument to Acos must be between -1 and 1, or else it will return NaN (not-a-number).

gistlibby LogSnag