max Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/max/ Sat, 28 Mar 2020 13:46:21 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 max Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/max/ 32 32 159787465 Max of two numbers in Go (Golang) https://vikasboss.github.io/max-of-two-numbers-go/ https://vikasboss.github.io/max-of-two-numbers-go/#respond Sat, 28 Mar 2020 13:46:11 +0000 https://vikasboss.github.io/?p=1882 Overview math package of GO provides a Max method that can be used to get the maximum of two numbers. Below is the signature of the function. It takes input two float...

The post Max of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Max method that can be used to get the maximum of two numbers.

Below is the signature of the function. It takes input two float numbers and returns a float.

func Max(x, y float64) float64

Also some special cases of Max function are

  • Max(x, +Inf) = Max(+Inf, x) = +Inf
  • Max(x, NaN) = Max(NaN, x) = NaN
  • Max(+0, ±0) = Max(±0, +0) = +0
  • Max(-0, -0) = -0

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    max := math.Max(2, 3)
    fmt.Println(max)

    max = math.Max(-2.1, -3.3)
    fmt.Println(max)
}

Output:

3
-2.1

The post Max of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/max-of-two-numbers-go/feed/ 0 1882