num

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package num provides utility functions for number manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(n float64) float64

Abs returns the absolute value of a number. Example: Abs(-5) -> 5

func Ceil

func Ceil(n float64, precision ...int) float64

Ceil rounds a number up to the nearest integer or to the specified precision. Example: Ceil(4.3) -> 5, Ceil(4.78, 1) -> 4.8

func Clamp

func Clamp(n, lower, upper float64) float64

Clamp clamps a number between lower and upper bounds. Example: Clamp(10, 0, 5) -> 5

func Floor

func Floor(n float64, precision ...int) float64

Floor rounds a number down to the nearest integer or to the specified precision. Example: Floor(4.7) -> 4, Floor(4.78, 1) -> 4.7

func InRange

func InRange(n, start, end float64) bool

InRange checks if a number is between start and end (inclusive). Example: InRange(3, 2, 4) -> true

func Max

func Max(numbers ...float64) float64

Max returns the maximum of the given numbers. Example: Max(1, 2, 3) -> 3

func MaxBy

func MaxBy[T any](collection []T, iteratee func(T) float64) T

MaxBy returns the maximum value of a slice using the provided iteratee function. Example: MaxBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * n) }) -> 3

func Mean

func Mean(numbers ...float64) float64

Mean returns the arithmetic mean of the given numbers. Example: Mean(1, 2, 3) -> 2

func MeanBy

func MeanBy[T any](collection []T, iteratee func(T) float64) float64

MeanBy returns the mean of values in a slice after applying the iteratee function. Example: MeanBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * 2) }) -> 4

func Min

func Min(numbers ...float64) float64

Min returns the minimum of the given numbers. Example: Min(1, 2, 3) -> 1

func MinBy

func MinBy[T any](collection []T, iteratee func(T) float64) T

MinBy returns the minimum value of a slice using the provided iteratee function. Example: MinBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * n) }) -> 1

func Pow

func Pow(base, exponent float64) float64

Pow returns the base raised to the exponent power. Example: Pow(2, 3) -> 8

func Random

func Random(min, max int) int

Random returns a random number between min and max (inclusive). Example: Random(1, 10) -> a random number between 1 and 10

func Round

func Round(n float64, precision ...int) float64

Round rounds a number to the nearest integer or to the specified precision. Example: Round(4.7) -> 5, Round(4.7, 1) -> 4.7

func Sqrt

func Sqrt(n float64) float64

Sqrt returns the square root of a number. Example: Sqrt(9) -> 3

func Sum

func Sum(numbers ...float64) float64

Sum returns the sum of the given numbers. Example: Sum(1, 2, 3) -> 6

func SumBy

func SumBy[T any](collection []T, iteratee func(T) float64) float64

SumBy returns the sum of values in a slice after applying the iteratee function. Example: SumBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * 2) }) -> 12

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL