ADVERTISEMENT
ADVERTISEMENT

Golang math.Min() Function with Examples

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

math.Min()

The Min() function is an inbuilt function of the math package which is used to get the smaller value of the given parameters.

It accepts two parameters (x, y), and returns the smaller of x or y.

Syntax:

func Min(x, y float64) float64

Parameter(s):

  • x, y : The values from which we have to find the smaller value.

Return Value:

The return type of Min() function is a float64, it returns the smaller value of the given parameters.

Special cases:

  • Min(x, -Inf) = Min(-Inf, x) = -Inf
    If any of the any parameter is negative infinity, the function returns -Inf.
  • Min(x, NaN) = Min(NaN, x) = NaN
    If any of the parameter is NaN, the function returns NaN.
  • Min(-0, ±0) = Min(±0, -0) = -0
    If one parameter is ±0 and other is -0, the function returns -0.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Min(10, 20))
	fmt.Println(math.Min(20, 10))
	fmt.Println(math.Min(10, 10))
	fmt.Println(math.Min(-10, 20))
	fmt.Println(math.Min(10, -20))
	fmt.Println(math.Min(-10, -10))
	fmt.Println(math.Min(10.50, -20.12))

	fmt.Println(math.Min(math.Inf(1), 10))
	fmt.Println(math.Min(10, math.NaN()))
	fmt.Println(math.Min(0, 0))
}

Output:

10
10
10
-10
-20
-10
-20.12
10
NaN
0

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var MinXY float64

	x = 10
	y = 20
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = 20
	y = 10
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = -10
	y = -20
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = -1.289
	y = -2.120
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = 10
	y = math.Inf(-1)
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = 10
	y = math.Inf(1)
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)

	x = math.NaN()
	y = math.NaN()
	MinXY = math.Min(x, y)
	fmt.Println("Smaller value of", x, "or", y, "is", MinXY)
}

Output:

Smaller value of 10 or 20 is 10
Smaller value of 20 or 10 is 10
Smaller value of -10 or -20 is -20
Smaller value of -1.289 or -2.12 is -2.12
Smaller value of 10 or -Inf is -Inf
Smaller value of 10 or +Inf is 10
Smaller value of NaN or 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.