The filter
package includes a set of math functions designed to perform common numerical operations in Go. These functions support a wide range of inputs, including integers, floats, and strings that can be parsed into numbers.
Calculates the absolute value of a given number.
Example:
result, err := filter.Abs(-5)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 5
Ensures that the given number is at least as large as a minimum value.
Example:
result, err := filter.AtLeast(3, 5)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 5
Ensures that the given number is no larger than a maximum value.
Example:
result, err := filter.AtMost(10, 8)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 8
Rounds the input to the specified number of decimal places.
Example:
result, err := filter.Round(3.14159, 2)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 3.14
Rounds the input down to the nearest whole number.
Example:
result, err := filter.Floor(2.9)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 2
Rounds the input up to the nearest whole number.
Example:
result, err := filter.Ceil(2.1)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 3
Adds two numbers.
Example:
result, err := filter.Plus(5, 3)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 8
Subtracts the second value from the first.
Example:
result, err := filter.Minus(10, 4)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 6
Multiplies the first value by the second.
Example:
result, err := filter.Times(6, 7)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 42
Divides the first value by the second. Includes handling for division by zero.
Example:
result, err := filter.Divide(10, 0)
if err != nil {
log.Fatal(err)
fmt.Println(err) // Outputs: division by zero
} else {
fmt.Println(result)
}
Returns the remainder of the division of the first value by the second.
Example:
result, err := filter.Modulo(10, 3)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // Outputs: 1