Documentation ¶
Overview ¶
Package i16math provides some utilities for the int16 type that mirror those found in the built-in math package.
Index ¶
Examples ¶
Constants ¶
const ( MaxValue = math.MaxInt16 MinValue = math.MinInt16 )
int16 limit values.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
Abs returns the absolute value of x, or |x|. If |x| exceeds MaxValue, MaxValue is returned.
Example ¶
fmt.Println(Abs(0)) fmt.Println(Abs(1)) fmt.Println(Abs(-1)) fmt.Println(Abs(MaxValue) == MaxValue) fmt.Println(Abs(MinValue) == MaxValue)
Output: 0 1 1 true true
func CeilToMultiple ¶
CeilToMultiple returns the least multiple of y that is at least x. If x+y exceeds MaxValue, then the result is undefined.
Special cases:
CeilToMultiple(x, 0) = x CeilToMultiple(x, -1) = x
Example ¶
fmt.Println(CeilToMultiple(6, 3)) fmt.Println(CeilToMultiple(7, 3)) fmt.Println(CeilToMultiple(8, 3)) fmt.Println(CeilToMultiple(9, 3)) fmt.Println(CeilToMultiple(-6, 3)) fmt.Println(CeilToMultiple(-7, 3)) fmt.Println(CeilToMultiple(-8, 3)) fmt.Println(CeilToMultiple(-9, 3))
Output: 6 9 9 9 -6 -6 -6 -9
func FloorToMultiple ¶
FloorToMultiple returns the greatest multiple of y that is at most x. If x-y is less than MinValue, then the result is undefined.
Special cases:
FloorToMultiple(x, 0) = x FloorToMultiple(x, -1) = x
Example ¶
fmt.Println(FloorToMultiple(6, 3)) fmt.Println(FloorToMultiple(7, 3)) fmt.Println(FloorToMultiple(8, 3)) fmt.Println(FloorToMultiple(9, 3)) fmt.Println(FloorToMultiple(-6, 3)) fmt.Println(FloorToMultiple(-7, 3)) fmt.Println(FloorToMultiple(-8, 3)) fmt.Println(FloorToMultiple(-9, 3))
Output: 6 6 6 9 -6 -9 -9 -9
func Max ¶
Max returns the larger of x or y.
Example ¶
fmt.Println(Max(0, 0)) fmt.Println(Max(0, 1)) fmt.Println(Max(1, 0)) fmt.Println(Max(MaxValue, 0) == MaxValue) fmt.Println(Max(MinValue, MaxValue) == MaxValue)
Output: 0 1 1 true true
func Min ¶
Min returns the smaller of x or y.
Example ¶
fmt.Println(Min(1, 1)) fmt.Println(Min(0, 1)) fmt.Println(Min(1, 0)) fmt.Println(Min(MinValue, 0) == MinValue) fmt.Println(Min(MinValue, MaxValue) == MinValue)
Output: 1 0 0 true true
func RoundToMultiple ¶
RoundToMultiple returns the multiple of y closest to x, rounding ties away from zero. If x + y/2 exceeds MaxValue, or if x - y/2 is less than MinValue, then the result is undefined.
Special cases:
RoundToMultiple(x, 0) = x RoundToMultiple(x, -1) = x
Example (Even) ¶
fmt.Println(RoundToMultiple(4, 2)) fmt.Println(RoundToMultiple(5, 2)) fmt.Println(RoundToMultiple(6, 2)) fmt.Println(RoundToMultiple(-4, 2)) fmt.Println(RoundToMultiple(-5, 2)) fmt.Println(RoundToMultiple(-6, 2))
Output: 4 6 6 -4 -6 -6
Example (Odd) ¶
fmt.Println(RoundToMultiple(6, 3)) fmt.Println(RoundToMultiple(7, 3)) fmt.Println(RoundToMultiple(8, 3)) fmt.Println(RoundToMultiple(9, 3)) fmt.Println(RoundToMultiple(-6, 3)) fmt.Println(RoundToMultiple(-7, 3)) fmt.Println(RoundToMultiple(-8, 3)) fmt.Println(RoundToMultiple(-9, 3))
Output: 6 6 9 9 -6 -6 -9 -9
Types ¶
This section is empty.