github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekamath/min_max.go (about)

     1  // Copyright © 2020-2022. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekamath
     7  
     8  import (
     9  	"github.com/qioalice/ekago/v3/ekaext"
    10  )
    11  
    12  func Min[T ekaext.Ordered](a, b T) T {
    13  	return ekaext.If(a < b, a, b)
    14  }
    15  
    16  func Max[T ekaext.Ordered](a, b T) T {
    17  	return ekaext.If(a > b, a, b)
    18  }
    19  
    20  func Clamp[T ekaext.Ordered](v, a, b T) T {
    21  	a, b = Min(a, b), Max(a, b)
    22  	return Min(Max(v, a), b)
    23  }