Golang math.Inf() Function with Examples

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

math.Inf()

The Inf() function is an inbuilt function of the math package which is used to get the positive infinity or negative infinity. It returns the positive infinity (+Inf) if sign >= 0, the negative infinity (-Inf) if sign < 0. Where the sign is the given parameter.

It accepts a parameter (sign), and returns the positive infinity if sign >= 0, negative infinity if sign < 0.

Syntax:

func Inf(sign int) float64

Parameter(s):

  • sign : The value to be used to get the positive or negative infinity.

Return Value:

The return type of Inf() function is a float64, it returns the positive infinity if sign >= 0, negative infinity if sign < 0.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Inf(0))
	fmt.Println(math.Inf(1))
	fmt.Println(math.Inf(2))

	fmt.Println(math.Inf(-1))
	fmt.Println(math.Inf(-2))
}

Output:

+Inf
+Inf
+Inf
-Inf
-Inf

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x int
	var InfX float64

	x = 0
	InfX = math.Inf(x)
	fmt.Println("Inf(", x, ") = ", InfX)

	x = 1
	InfX = math.Inf(x)
	fmt.Println("Inf(", x, ") = ", InfX)

	x = -1
	InfX = math.Inf(x)
	fmt.Println("Inf(", x, ") = ", InfX)
}

Output:

Inf( 0 ) =  +Inf
Inf( 1 ) =  +Inf
Inf( -1 ) =  -Inf

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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