github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/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  	return dim(x, y)
    15  }
    16  
    17  func dim(x, y float64) float64 {
    18  	return max(x-y, 0)
    19  }
    20  
    21  // Max returns the larger of x or y.
    22  //
    23  // Special cases are:
    24  //	Max(x, +Inf) = Max(+Inf, x) = +Inf
    25  //	Max(x, NaN) = Max(NaN, x) = NaN
    26  //	Max(+0, ±0) = Max(±0, +0) = +0
    27  //	Max(-0, -0) = -0
    28  func Max(x, y float64) float64 {
    29  	return max(x, y)
    30  }
    31  
    32  func max(x, y float64) float64 {
    33  	// special cases
    34  	switch {
    35  	case IsInf(x, 1) || IsInf(y, 1):
    36  		return Inf(1)
    37  	case IsNaN(x) || IsNaN(y):
    38  		return NaN()
    39  	case x == 0 && x == y:
    40  		if Signbit(x) {
    41  			return y
    42  		}
    43  		return x
    44  	}
    45  	if x > y {
    46  		return x
    47  	}
    48  	return y
    49  }
    50  
    51  // Min returns the smaller of x or y.
    52  //
    53  // Special cases are:
    54  //	Min(x, -Inf) = Min(-Inf, x) = -Inf
    55  //	Min(x, NaN) = Min(NaN, x) = NaN
    56  //	Min(-0, ±0) = Min(±0, -0) = -0
    57  func Min(x, y float64) float64 {
    58  	return min(x, y)
    59  }
    60  
    61  func min(x, y float64) float64 {
    62  	// special cases
    63  	switch {
    64  	case IsInf(x, -1) || IsInf(y, -1):
    65  		return Inf(-1)
    66  	case IsNaN(x) || IsNaN(y):
    67  		return NaN()
    68  	case x == 0 && x == y:
    69  		if Signbit(x) {
    70  			return x
    71  		}
    72  		return y
    73  	}
    74  	if x < y {
    75  		return x
    76  	}
    77  	return y
    78  }