src/pkg/math/dim.go - The Go Programming Language

Golang

Source file src/pkg/math/dim.go

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