gitee.com/quant1x/num@v0.3.2/math32/dim.go (about)

     1  package math32
     2  
     3  // Dim returns the maximum of x-y or 0.
     4  //
     5  // Special cases are:
     6  //
     7  //	Dim(+Inf, +Inf) = NaN
     8  //	Dim(-Inf, -Inf) = NaN
     9  //	Dim(x, NaN) = Dim(NaN, x) = NaN
    10  func Dim(x, y float32) float32 {
    11  	return dim(x, y)
    12  }
    13  
    14  func dim(x, y float32) float32 {
    15  	return max(x-y, 0)
    16  }
    17  
    18  // Max returns the larger of x or y.
    19  //
    20  // Special cases are:
    21  //
    22  //	Max(x, +Inf) = Max(+Inf, x) = +Inf
    23  //	Max(x, NaN) = Max(NaN, x) = NaN
    24  //	Max(+0, ±0) = Max(±0, +0) = +0
    25  //	Max(-0, -0) = -0
    26  func Max(x, y float32) float32 {
    27  	return max(x, y)
    28  }
    29  
    30  func max(x, y float32) float32 {
    31  	// special cases
    32  	switch {
    33  	case IsInf(x, 1) || IsInf(y, 1):
    34  		return Inf(1)
    35  	case IsNaN(x) || IsNaN(y):
    36  		return NaN()
    37  	case x == 0 && x == y:
    38  		if Signbit(x) {
    39  			return y
    40  		}
    41  		return x
    42  	}
    43  	if x > y {
    44  		return x
    45  	}
    46  	return y
    47  }
    48  
    49  // Min returns the smaller of x or y.
    50  //
    51  // Special cases are:
    52  //
    53  //	Min(x, -Inf) = Min(-Inf, x) = -Inf
    54  //	Min(x, NaN) = Min(NaN, x) = NaN
    55  //	Min(-0, ±0) = Min(±0, -0) = -0
    56  func Min(x, y float32) float32 {
    57  	return min(x, y)
    58  }
    59  
    60  func min(x, y float32) float32 {
    61  	// special cases
    62  	switch {
    63  	case IsInf(x, -1) || IsInf(y, -1):
    64  		return Inf(-1)
    65  	case IsNaN(x) || IsNaN(y):
    66  		return NaN()
    67  	case x == 0 && x == y:
    68  		if Signbit(x) {
    69  			return x
    70  		}
    71  		return y
    72  	}
    73  	if x < y {
    74  		return x
    75  	}
    76  	return y
    77  }