×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Sincos() Function with Examples

Golang | math.Sincos() Function: Here, we are going to learn about the Sincos() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 03, 2021

math.Sincos()

The Sincos() function is an inbuilt function of math package which is used to get the Sin(x), Cos(x). Where x is the given value.

It accepts a parameter (x) and returns two values: sine of x (Sin(x)) and cosine of x (Cos(x)).

Syntax

func Sincos(x float64) (sin, cos float64)

Parameters

  • x : The value whose sine and cosine values are to be found.

Return Value

The return type of Sincos() function is a (float64, float64), it returns the sine of x (Sin(x)) and cosine of x (Cos(x)).

Special Cases

  • Sincos(±0) = ±0, 1
    If the parameter is ±0, the function returns ±0, 1.
  • Sincos(±Inf) = NaN, NaN
    If the parameter is ±Inf, the function returns NaN, NaN.
  • Sincos(NaN) = NaN, NaN
    If the parameter is NaN, the function returns NaN, NaN.

Example 1

// Golang program to demonstrate the
// example of math.Sincos() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Sincos(1))
	fmt.Println(math.Sincos(0.23))
	fmt.Println(math.Sincos(-1))
	fmt.Println(math.Sincos(-0.23))
	fmt.Println(math.Sincos(-1.23))
	fmt.Println(math.Sincos(1.23))
	fmt.Println(math.Sincos(math.Pi))
	fmt.Println(math.Sincos(0))
	fmt.Println(math.Sincos(math.Inf(1)))
	fmt.Println(math.Sincos(math.Inf(-1)))
	fmt.Println(math.Sincos(math.NaN()))
}

Output:

0.8414709848078965 0.5403023058681398
0.2279775235351884 0.9736663950053749
-0.8414709848078965 0.5403023058681398
-0.2279775235351884 0.9736663950053749
-0.9424888019316975 0.3342377271245026
0.9424888019316975 0.3342377271245026
1.2246467991473515e-16 -1
0 1
NaN NaN
NaN NaN
NaN NaN

Example 2

// Golang program to demonstrate the
// example of math.Sincos() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var SinX, CosX float64

	x = 0
	SinX, CosX = math.Sincos(x)

	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)

	x = 0.23
	SinX, CosX = math.Sincos(x)
	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)

	x = 1
	SinX, CosX = math.Sincos(x)
	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)

	x = -0.23
	SinX, CosX = math.Sincos(x)
	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)

	x = -2
	SinX, CosX = math.Sincos(x)
	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)

	x = math.Pi
	SinX, CosX = math.Sincos(x)
	fmt.Printf("Sin(%.2f) = %.2f, Cos(%.2f) = %.2f\n", x, SinX, x, CosX)
}

Output:

Sin(0.00) = 0.00, Cos(0.00) = 1.00
Sin(0.23) = 0.23, Cos(0.23) = 0.97
Sin(1.00) = 0.84, Cos(1.00) = 0.54
Sin(-0.23) = -0.23, Cos(-0.23) = 0.97
Sin(-2.00) = -0.91, Cos(-2.00) = -0.42
Sin(3.14) = 0.00, Cos(3.14) = -1.00

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.