×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Tan() Function with Examples

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

math.Tan()

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

It accepts a parameter (x) and returns the tangent of the radian argument x.

Syntax

func Tan(x float64) float64

Parameters

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

Return Value

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

Special Cases

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

Example 1

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

package main

import (
	"fmt"
	"math"
)

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

Output:

1.557407724654902
0.23414336235146527
-1.557407724654902
-0.23414336235146527
-2.819815734268152
2.819815734268152
-1.2246467991473515e-16
0
NaN
NaN
NaN

Example 2

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var TanX float64

	x = 0
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)

	x = 0.23
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)

	x = 1
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)

	x = -0.23
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)

	x = -2
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)

	x = math.Pi
	TanX = math.Tan(x)
	fmt.Printf("Tangent of %.2f is %.2f\n", x, TanX)
}

Output:

Tangent of 0.00 is 0.00
Tangent of 0.23 is 0.23
Tangent of 1.00 is 1.56
Tangent of -0.23 is -0.23
Tangent of -2.00 is 2.19
Tangent of 3.14 is -0.00

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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