Golang math.Sin() Function with Examples

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

math.Sin()

The Sin() function is an inbuilt function of the math package which is used to get the sine of the given radian value.

It accepts a parameter (x) and returns the sine of the radian value x.

Syntax:

func Sin(x float64) float64

Parameter(s):

  • x : The radian value whose sine value is to be found.

Return Value:

The return type of Sin() function is a float64, it returns the sine of the given radian value.

Special cases:

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

Example 1:

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

package main

import (
	"fmt"
	"math"
)

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

Output:

0.8414709848078965
0.2279775235351884
-0.8414709848078965
-0.2279775235351884
-0.9424888019316975
0.9424888019316975
1.2246467991473515e-16
0
NaN
NaN
NaN

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var SinX float64

	x = 0
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)

	x = 0.23
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)

	x = 1
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)

	x = -0.23
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)

	x = -2
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)

	x = math.Pi
	SinX = math.Sin(x)
	fmt.Println("Sine value of", x, "is", SinX)
}

Output:

Sine value of 0 is 0
Sine value of 0.23 is 0.2279775235351884
Sine value of 1 is 0.8414709848078965
Sine value of -0.23 is -0.2279775235351884
Sine value of -2 is -0.9092974268256816
Sine value of 3.141592653589793 is 1.2246467991473515e-16

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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