Menu Close

Go – How to get the maximum of difference of two number being first number as positive

Here, We will learn how to get the maximum of difference of two number being first number as positive in go. We can get it by using Dim() function in math package in go golang.

Function prototype:

func Dim(x, y float64) float64

Return value:

Dim() function in math package returns
maximum of x-y. If difference of x and y
or x is negative then it return 0.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Dim(5, -2)
  fmt.Printf("%.2f\n", no)

  no = math.Dim(-5, -2)
  fmt.Printf("%.2f\n", no)

  no = math.Dim(-5, 2)
  fmt.Printf("%.2f\n", no)

  no = math.Dim(5, -5)
  fmt.Printf("%.2f\n", no)

  no = math.Dim(-2, 5)
  fmt.Printf("%.2f\n", no)
	
}

Output:

7.00
0.00
0.00
10.00
0.00

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *