codekingpro/portable-devtools
114k
1// Copyright 2010 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5package math6 7// Dim returns the maximum of x-y or 0.8//9// Special cases are:10//11// Dim(+Inf, +Inf) = NaN12// Dim(-Inf, -Inf) = NaN13// Dim(x, NaN) = Dim(NaN, x) = NaN14func Dim(x, y float64) float64 {15 // The special cases result in NaN after the subtraction:16 // +Inf - +Inf = NaN17 // -Inf - -Inf = NaN18 // NaN - y = NaN19 // x - NaN = NaN20 v := x - y21 if v <= 0 {22 // v is negative or 023 return 024 }25 // v is positive or NaN26 return v27}28 29// Max returns the larger of x or y.30//31// Special cases are:32//33// Max(x, +Inf) = Max(+Inf, x) = +Inf34// Max(x, NaN) = Max(NaN, x) = NaN35// Max(+0, ±0) = Max(±0, +0) = +036// Max(-0, -0) = -037//38// Note that this differs from the built-in function max when called39// with NaN and +Inf.40func Max(x, y float64) float64 {41 if haveArchMax {42 return archMax(x, y)43 }44 return max(x, y)45}46 47func max(x, y float64) float64 {48 // special cases49 switch {50 case IsInf(x, 1) || IsInf(y, 1):51 return Inf(1)52 case IsNaN(x) || IsNaN(y):53 return NaN()54 case x == 0 && x == y:55 if Signbit(x) {56 return y57 }58 return x59 }60 if x > y {61 return x62 }63 return y64}65 66// Min returns the smaller of x or y.67//68// Special cases are:69//70// Min(x, -Inf) = Min(-Inf, x) = -Inf71// Min(x, NaN) = Min(NaN, x) = NaN72// Min(-0, ±0) = Min(±0, -0) = -073//74// Note that this differs from the built-in function min when called75// with NaN and -Inf.76func Min(x, y float64) float64 {77 if haveArchMin {78 return archMin(x, y)79 }80 return min(x, y)81}82 83func min(x, y float64) float64 {84 // special cases85 switch {86 case IsInf(x, -1) || IsInf(y, -1):87 return Inf(-1)88 case IsNaN(x) || IsNaN(y):89 return NaN()90 case x == 0 && x == y:91 if Signbit(x) {92 return x93 }94 return y95 }96 if x < y {97 return x98 }99 return y100}101 