Documentation
¶
Index ¶
- func AbsDiff[T basicutils.Numeric](one T, two T) T
- func AbsVal[T basicutils.Numeric](val T) T
- func Avg[T basicutils.Numeric](array []T) T
- func AvgFloat[T basicutils.Numeric](array []T) float64
- func ClosestMatch[T basicutils.Numeric](toMatch T, slice []T) (int, T)
- func Max[T basicutils.Numeric](array []T) T
- func MaxValue[T basicutils.Numeric]() T
- func Med[T basicutils.Numeric](array []T) T
- func Min[T basicutils.Numeric](array []T) T
- func MinMax[T basicutils.Numeric](array []T) (T, T)
- func MinMaxFromMap[K comparable, T basicutils.Numeric](m map[K]T) (T, T)
- func MinMaxInt(array []int) (int, int)
- func RoundUp[T basicutils.Numeric](value T) T
- func RoundWithPrecision[T basicutils.Numeric](value T, precision int) T
- func Sum[T basicutils.Numeric](array []T) T
- func ValOrMin(val int, mn int) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AbsDiff ¶
func AbsDiff[T basicutils.Numeric](one T, two T) T
AbsDiff returns absolute diff result of a numeric type
func AbsVal ¶
func AbsVal[T basicutils.Numeric](val T) T
AbsVal returns absolute value of a numeric type
func AvgFloat ¶
func AvgFloat[T basicutils.Numeric](array []T) float64
AvgFloat returns average value from the array as float64
func ClosestMatch ¶
func ClosestMatch[T basicutils.Numeric](toMatch T, slice []T) (int, T)
ClosestMatch finds the closest match in a slice. This implementation doesn't use Round. Returns index of a found element and the element. If no element was found then -1 as index will be returned.
func MaxValue ¶
func MaxValue[T basicutils.Numeric]() T
func Med ¶
func Med[T basicutils.Numeric](array []T) T
func MinMax ¶
func MinMax[T basicutils.Numeric](array []T) (T, T)
func MinMaxFromMap ¶
func MinMaxFromMap[K comparable, T basicutils.Numeric](m map[K]T) (T, T)
func RoundUp ¶
func RoundUp[T basicutils.Numeric](value T) T
RoundUp rounds the given numeric value up to the nearest integer, regardless of the fractional part. This function is akin to RoundWithPrecision but differs in its rounding strategy: while RoundWithPrecision rounds to a specified number of decimal places based on the usual rounding rules, RoundUp always rounds up to the nearest integer.
This function is generic and can work with any type that satisfies the basicutils.Numeric constraint, which includes integer and floating-point numeric types.
Parameters: - value: The numeric value to be rounded up. This can be any type that satisfies the basicutils.Numeric constraint.
Returns: - The value rounded up to the nearest integer, with the same type as the input `value`.
Example: - RoundUp(0.1) returns 1 (whereas RoundWithPrecision(0.1, 0) would return 0) - RoundUp(1.5) returns 2 (whereas RoundWithPrecision(1.5, 0) would return 2) - RoundUp(-1.5) returns -1 (whereas RoundWithPrecision(-1.5, 0) would return -2)
Note: This function always rounds numbers up to the next highest integer. This is different from RoundWithPrecision, which rounds to the nearest integer based on the fractional part and specified precision. If you need traditional rounding to the nearest value or rounding down, you should use RoundWithPrecision or a similar function.
func RoundWithPrecision ¶
func RoundWithPrecision[T basicutils.Numeric](value T, precision int) T
RoundWithPrecision rounds a numeric value to a specified number of decimal places. This function is generic and can operate on any type that satisfies the basicutils.Numeric constraint, which typically includes integer and floating-point types like int, float32, and float64.
Parameters:
- value: The numeric value to be rounded. This can be any type that satisfies the basicutils.Numeric constraint.
- precision: The number of decimal places to round to. It specifies how many digits should appear after the decimal point. If precision is negative, the function will round off digits to the left of the decimal point.
Returns:
- The rounded value of the same type as the input `value`. The rounding is done according to the standard rounding rules, where numbers halfway between two possible outcomes are rounded to the nearest even number (also known as "bankers rounding").
Behavior and Edge Cases:
- If precision is 0, the function rounds to the nearest integer.
- Positive precision values round to the specified number of digits after the decimal point.
- Negative precision values round to the nearest 10^(-precision) place. For example, a precision of -1 rounds to the nearest 10, -2 rounds to the nearest 100, and so forth, effectively reducing the precision of the input number.
- For floating-point values, the function handles edge cases like rounding at 0.5, where the standard rounding method is applied.
- For integer types, the precision parameter is less meaningful beyond 0, but the function will still operate without error, effectively leaving the integer value unchanged for positive precision values, and rounding to multiples of 10 for negative precision values.
- The function may lose precision for very large numbers or for numbers requiring more precision than the type can represent. For example, rounding a float64 to a very high precision can result in a loss of accuracy due to the inherent limitations of floating-point representation.
- If the type T does not have enough precision to represent the result accurately, the behavior is dependent on the underlying type's precision and range.
Usage: The function is versatile and can be used with various numeric types. It is especially useful in scenarios where the precision of numerical computation needs to be controlled explicitly.
func Sum ¶
func Sum[T basicutils.Numeric](array []T) T
Types ¶
This section is empty.