github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/math/dim.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  // Dim returns the maximum of x-y or 0.
     8  //
     9  // Special cases are:
    10  //	Dim(+Inf, +Inf) = NaN
    11  //	Dim(-Inf, -Inf) = NaN
    12  //	Dim(x, NaN) = Dim(NaN, x) = NaN
    13  func Dim(x, y float64) float64 {
    14  	// The special cases result in NaN after the subtraction:
    15  	//      +Inf - +Inf = NaN
    16  	//      -Inf - -Inf = NaN
    17  	//       NaN - y    = NaN
    18  	//         x - NaN  = NaN
    19  	v := x - y
    20  	if v <= 0 {
    21  		// v is negative or 0
    22  		return 0
    23  	}
    24  	// v is positive or NaN
    25  	return v
    26  }
    27  
    28  // Max returns the larger of x or y.
    29  //
    30  // Special cases are:
    31  //	Max(x, +Inf) = Max(+Inf, x) = +Inf
    32  //	Max(x, NaN) = Max(NaN, x) = NaN
    33  //	Max(+0, ±0) = Max(±0, +0) = +0
    34  //	Max(-0, -0) = -0
    35  func Max(x, y float64) float64
    36  
    37  func max(x, y float64) float64 {
    38  	// special cases
    39  	switch {
    40  	case IsInf(x, 1) || IsInf(y, 1):
    41  		return Inf(1)
    42  	case IsNaN(x) || IsNaN(y):
    43  		return NaN()
    44  	case x == 0 && x == y:
    45  		if Signbit(x) {
    46  			return y
    47  		}
    48  		return x
    49  	}
    50  	if x > y {
    51  		return x
    52  	}
    53  	return y
    54  }
    55  
    56  // Min returns the smaller of x or y.
    57  //
    58  // Special cases are:
    59  //	Min(x, -Inf) = Min(-Inf, x) = -Inf
    60  //	Min(x, NaN) = Min(NaN, x) = NaN
    61  //	Min(-0, ±0) = Min(±0, -0) = -0
    62  func Min(x, y float64) float64
    63  
    64  func min(x, y float64) float64 {
    65  	// special cases
    66  	switch {
    67  	case IsInf(x, -1) || IsInf(y, -1):
    68  		return Inf(-1)
    69  	case IsNaN(x) || IsNaN(y):
    70  		return NaN()
    71  	case x == 0 && x == y:
    72  		if Signbit(x) {
    73  			return x
    74  		}
    75  		return y
    76  	}
    77  	if x < y {
    78  		return x
    79  	}
    80  	return y
    81  }