github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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 // 11 // Dim(+Inf, +Inf) = NaN 12 // Dim(-Inf, -Inf) = NaN 13 // Dim(x, NaN) = Dim(NaN, x) = NaN 14 func Dim(x, y float64) float64 { 15 // The special cases result in NaN after the subtraction: 16 // +Inf - +Inf = NaN 17 // -Inf - -Inf = NaN 18 // NaN - y = NaN 19 // x - NaN = NaN 20 v := x - y 21 if v <= 0 { 22 // v is negative or 0 23 return 0 24 } 25 // v is positive or NaN 26 return v 27 } 28 29 // Max returns the larger of x or y. 30 // 31 // Special cases are: 32 // 33 // Max(x, +Inf) = Max(+Inf, x) = +Inf 34 // Max(x, NaN) = Max(NaN, x) = NaN 35 // Max(+0, ±0) = Max(±0, +0) = +0 36 // Max(-0, -0) = -0 37 func Max(x, y float64) float64 { 38 if haveArchMax { 39 return archMax(x, y) 40 } 41 return max(x, y) 42 } 43 44 func max(x, y float64) float64 { 45 // special cases 46 switch { 47 case IsInf(x, 1) || IsInf(y, 1): 48 return Inf(1) 49 case IsNaN(x) || IsNaN(y): 50 return NaN() 51 case x == 0 && x == y: 52 if Signbit(x) { 53 return y 54 } 55 return x 56 } 57 if x > y { 58 return x 59 } 60 return y 61 } 62 63 // Min returns the smaller of x or y. 64 // 65 // Special cases are: 66 // 67 // Min(x, -Inf) = Min(-Inf, x) = -Inf 68 // Min(x, NaN) = Min(NaN, x) = NaN 69 // Min(-0, ±0) = Min(±0, -0) = -0 70 func Min(x, y float64) float64 { 71 if haveArchMin { 72 return archMin(x, y) 73 } 74 return min(x, y) 75 } 76 77 func min(x, y float64) float64 { 78 // special cases 79 switch { 80 case IsInf(x, -1) || IsInf(y, -1): 81 return Inf(-1) 82 case IsNaN(x) || IsNaN(y): 83 return NaN() 84 case x == 0 && x == y: 85 if Signbit(x) { 86 return x 87 } 88 return y 89 } 90 if x < y { 91 return x 92 } 93 return y 94 }