The Dim
function in the Go programming language is used to find the maximum between the difference of two provided arguments and 0.
To use this function, you must import the math
package in your file and access the Dim
function within it using the .
notation (math.Dim
). Here, Dim
is the actual function, while math
is the Go package that stores the definition of this function.
The definition of the Dim
function inside the math
package is:
The Dim
function takes two arguments of type float64
. These arguments represent the two numbers whose difference we need to find.
The Dim
function returns a single value of type float64
. This value represents the difference between the two arguments (x-y
) and 0. If the difference is greater than 0, it is returned; else, 0 is returned.
Following are two types of return values only used by the function under certain circumstances:
NAN
: Not a number or NAN
is returned in two cases:
Either one or both input arguments are of undefined value.
Both arguments are infinity with the same sign.
(±)Inf
: The Dim
function returns +Inf
if the difference between the two arguments (x-y
) is infinite in the positive direction. However, if it is infinite in the negative direction, -Inf
is returned.
This can result if one of the arguments is of infinite value or if both are of infinite value but with opposite signs.
Giving an empty argument or an argument that is not a numeric value results in an error.
Following is a simple example where we use the Dim
function on two decimal values:
Now, we make both arguments negative, which means that the result of x-y
is now also negative. Since negative numbers are smaller than 0, the value of the returned float64
is 0:
package mainimport("fmt""math")func main() {x := math.Dim(-3, -1)fmt.Println(x)}
The following example shows how the function handles an infinite difference. Since the x
here is 1 and y
is -Inf
, x-y
becomes +Inf
:
package mainimport("fmt""math")func main() {x := math.Dim(1, math.Inf(-1))fmt.Println(x)}
The first argument (i.e. zero/zero) passed to the Dim
function is an undefined value and is classified as a NAN
. In the following example, we can see how a single NAN
argument results in the return value also becoming NAN
:
package mainimport("fmt""math")func main() {zero := 0.0x := math.Dim(zero/zero, 3)fmt.Println(x)}