How to Find the Sin and Cos Value of a Number in Golang?
Last Updated :
21 Apr, 2020
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find the value of sin and cos of the specified number with the help of Sincos() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Sincos() function.
Syntax:
func Sincos(a float64) (sin, cos float64)
- If you pass -Inf or +Inf in this function like Sincos(-Inf) or Sincos(+Inf), then this function will return NaN, NaN.
- If you pass -0 or +0 in this function like Sincos(-0) or Sincos(+0), then this function will return -0 or +0, 1.
- If you pass NaN in this function like Sincos(NaN), then this function will return NaN, NaN.
Example 1:
package main
import (
"fmt"
"math"
)
func main() {
res_1, res_2 := math.Sincos(math.Pi / 3)
res_3, res_4 := math.Sincos(0)
res_5, res_6 := math.Sincos(math.Inf(-1))
res_7, res_8 := math.Sincos(math.NaN())
res_9, res_10 := math.Sincos(math.Pi)
fmt.Printf( "Result 1: %.1f, %.1f" , res_1, res_2)
fmt.Printf( "\nResult 2: %.1f, %.1f" , res_3, res_4)
fmt.Printf( "\nResult 3: %.1f, %.1f" , res_5, res_6)
fmt.Printf( "\nResult 4: %.1f, %.1f" , res_7, res_8)
fmt.Printf( "\nResult 5: %.1f, %.1f" , res_9, res_10)
}
|
Output:
Result 1: 0.9, 0.5
Result 2: 0.0, 1.0
Result 3: NaN, NaN
Result 4: NaN, NaN
Result 5: 0.0, -1.0
Example 2:
package main
import (
"fmt"
"math"
)
func main() {
nvalue_1, nvalue_2 := math.Sincos(math.Pi / 2)
nvalue_3, nvalue_4 := math.Sincos(math.Pi / 3)
res1 := nvalue_1 + nvalue_3
fmt.Println( "Sum of Sin Values:" )
fmt.Printf( "%.2f + %.2f = %.2f\n" ,
nvalue_1, nvalue_3, res1)
res2 := nvalue_2 + nvalue_4
fmt.Println( "Sum of Cos Values:" )
fmt.Printf( "%.2f + %.2f = %.2f" ,
nvalue_2, nvalue_4, res2)
}
|
Output:
Sum of Sin Values:
1.00 + 0.87 = 1.87
Sum of Cos Values:
0.00 + 0.50 = 0.50
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...