×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Max() Function with Examples

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

math.Max()

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

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

Syntax

func Max(x, y float64) float64

Parameters

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

Return Value

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

Special Cases

  • Max(x, +Inf) = Max(+Inf, x) = +Inf
    If any of the parameter is positive infinity, the function returns +Inf.
  • Max(x, NaN) = Max(NaN, x) = NaN
    If any of the parameter is NaN, the function returns NaN.
  • Max(+0, ±0) = Max(±0, +0) = +0
    If any of the parameter is ±0, the function returns +0.
  • Max(-0, -0) = -0
    If both of the parameters are -0, the function returns -0.

Example 1

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

package main

import (
	"fmt"
	"math"
)

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

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

Output:

20
20
10
20
10
-10
10.5
+Inf
NaN
0

Example 2

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

package main

import (
	"fmt"
	"math"
)

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

	x = 10
	y = 20
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = 20
	y = 10
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = -10
	y = -20
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = -1.289
	y = -2.120
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = 10
	y = math.Inf(-1)
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = 10
	y = math.Inf(1)
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)

	x = math.NaN()
	y = math.NaN()
	MaxXY = math.Max(x, y)
	fmt.Println("Larger value of", x, "or", y, "is", MaxXY)
}

Output:

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