github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/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 if haveArchMax { 37 return archMax(x, y) 38 } 39 return max(x, y) 40 } 41 42 func max(x, y float64) float64 { 43 // special cases 44 switch { 45 case IsInf(x, 1) || IsInf(y, 1): 46 return Inf(1) 47 case IsNaN(x) || IsNaN(y): 48 return NaN() 49 case x == 0 && x == y: 50 if Signbit(x) { 51 return y 52 } 53 return x 54 } 55 if x > y { 56 return x 57 } 58 return y 59 } 60 61 // Min returns the smaller of x or y. 62 // 63 // Special cases are: 64 // Min(x, -Inf) = Min(-Inf, x) = -Inf 65 // Min(x, NaN) = Min(NaN, x) = NaN 66 // Min(-0, ±0) = Min(±0, -0) = -0 67 func Min(x, y float64) float64 { 68 if haveArchMin { 69 return archMin(x, y) 70 } 71 return min(x, y) 72 } 73 74 func min(x, y float64) float64 { 75 // special cases 76 switch { 77 case IsInf(x, -1) || IsInf(y, -1): 78 return Inf(-1) 79 case IsNaN(x) || IsNaN(y): 80 return NaN() 81 case x == 0 && x == y: 82 if Signbit(x) { 83 return x 84 } 85 return y 86 } 87 if x < y { 88 return x 89 } 90 return y 91 }