×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Dim() Function with Examples

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

math.Dim()

The Dim() function is an inbuilt function of the math package which is used to get the maximum of x-y or 0. Where x is the first parameter and y is the second parameter.

It accepts two parameters (x, y) and returns the maximum of x-y or 0.

Syntax

func Dim(x, y float64) float64

Parameters

  • x, y : The values to be used to get the maximum of x-y or 0.

Return Value

The return type of Dim() function is a float64, it returns the maximum of x-y or 0.

Special cases are:

  • Dim(+Inf, +Inf) = NaN
    If both parameters are positive infinity, the function returns the Inf.
  • Dim(-Inf, -Inf) = NaN
    If both parameters are negative infinity, the function returns the Inf.
  • Dim(x, NaN) = Dim(NaN, x) = NaN
    If any of the parameters is NaN (Not-A-Number), the function returns the NaN.

Example 1

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Dim(10, -5))
	fmt.Println(math.Dim(-10, 5))
	fmt.Println(math.Dim(-10, -5))
	fmt.Println(math.Dim(10, 5))
	fmt.Println(math.Dim(1.49, -2))

	fmt.Println(math.Dim(math.Inf(-1), math.Inf(-1)))
	fmt.Println(math.Dim(math.Inf(1), math.Inf(1)))
	fmt.Println(math.Dim(math.Inf(-1), math.Inf(1)))
	fmt.Println(math.Dim(math.Inf(1), math.Inf(-1)))

	fmt.Println(math.Dim(math.NaN(), math.NaN()))
	fmt.Println(math.Dim(10, math.NaN()))
	fmt.Println(math.Dim(math.NaN(), 10))
}

Output:

15
0
0
5
3.49
NaN
NaN
0
+Inf
NaN
NaN
NaN

Example 2

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

package main

import (
	"fmt"
	"math"
)

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

	x = 10
	y = -5
	DimXY = math.Dim(x, y)
	fmt.Println("Dim(", x, ",", y, ") = ", DimXY)

	x = -10
	y = 5
	DimXY = math.Dim(x, y)
	fmt.Println("Dim(", x, ",", y, ") = ", DimXY)

	x = -10
	y = -5
	DimXY = math.Dim(x, y)
	fmt.Println("Dim(", x, ",", y, ") = ", DimXY)

	x = 10
	y = 5
	DimXY = math.Dim(x, y)
	fmt.Println("Dim(", x, ",", y, ") = ", DimXY)
}

Output:

Dim( 10 , -5 ) =  15
Dim( -10 , 5 ) =  0
Dim( -10 , -5 ) =  0
Dim( 10 , 5 ) =  5

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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