×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

Parameters

  • 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 »




Comments and Discussions!

Load comments ↻





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