ADVERTISEMENT
ADVERTISEMENT

Golang math.Cos() Function with Examples

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

math.Cos()

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

It accepts one parameter and returns the cosine of the radian value.

Syntax:

func Cos(x float64) float64

Parameter(s):

  • x : The value whose cosine is to be found.

Return Value:

The return type of Cos() function is a float64, it returns the cosine value of the given value.

Special cases are:

  • Cos(±Inf) = NaN
    If the parameter is ±Inf, it returns the NaN.
  • Cos(NaN) = NaN
    If the parameter is NaN, it returns the NaN.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Cos(1))
	fmt.Println(math.Cos(10))

	fmt.Println(math.Cos(-1))
	fmt.Println(math.Cos(-10))

	fmt.Println(math.Cos(0.5))
	fmt.Println(math.Cos(-0.5))

	fmt.Println(math.Cos(math.Inf(1)))
	fmt.Println(math.Cos(math.Inf(-1)))
	fmt.Println(math.Cos(math.NaN()))
}

Output:

0.5403023058681398
-0.8390715290764524
0.5403023058681398
-0.8390715290764524
0.8775825618903728
0.8775825618903728
NaN
NaN
NaN

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var CosX float64

	x = 0
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = 0.23
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = 1
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = -0.23
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = -2
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = math.Inf(1)
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = math.Inf(-1)
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)

	x = math.NaN()
	CosX = math.Cos(x)
	fmt.Println("Cosine value of", x, "is", CosX)
}

Output:

Cosine value of 0 is 1
Cosine value of 0.23 is 0.9736663950053749
Cosine value of 1 is 0.5403023058681398
Cosine value of -0.23 is 0.9736663950053749
Cosine value of -2 is -0.4161468365471424
Cosine value of +Inf is NaN
Cosine value of -Inf is NaN
Cosine value of NaN is NaN

Golang math Package Constants and Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.