math

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 2 Imported by: 0

README

Math

  • Abs: Returns the absolute value of the given number. Works for both integers and floating-point numbers. If the input is negative, it returns its positive equivalent; otherwise, it returns the number as is.

  • Sign: Determines the sign of a signed number. Returns 1 if the number is positive, -1 if negative, and 0 if the number is zero.

  • Min: Returns the smaller of two numbers. Works for all number types including integers and floating-point numbers.

  • Max: Returns the larger of two numbers. Works for all number types including integers and floating-point numbers.

  • Clamp: Restricts a given value to be within a specified range. If the value is below the minimum, it returns the minimum; if above the maximum, it returns the maximum.

  • IntPow: Calculates base raised to the power of exp. Supports both positive and negative exponents. Returns float64 for fractional results.

  • IsEven: Checks if the given integer is even. Returns true for even numbers and false otherwise.

  • IsOdd: Checks if the given integer is odd. Returns true for odd numbers and false otherwise.

  • Swap: Swaps the values of two variables in place. It uses pointers to modify the original variables.

  • Factorial: Computes the factorial of a non-negative integer. Factorial of n is defined as the product of all integers from 1 to n. For 0 and 1, the result is 1. Factorial returns an error on invalid input.

  • GCD: Finds the greatest common divisor (GCD) of two integers using the Euclidean algorithm. If one of the inputs is 0, the other input is returned.

  • LCM: Finds the least common multiple (LCM) of two integers.

  • Sqrt: Finds the square root of the given number. Works for both integers and floating-point numbers. If the input is negative, it returns an error along with the original negative number.

  • IsPrime: Checks if a number is prime or not. Only works for non-negative integers.

  • PrimeList: Returns a slice of prime numbers up to n.

  • GetDivisors: Returns the divisors of a positive integer as an unordered slice. Returns an empty slice for negative inputs.

  • RoundDecimalPlaces: Rounds a float64 to the specified number of decimal places using math.Round (half away from zero). Negative values for places are clamped to 0 (i.e., rounds to a whole number).

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T number](x T) T

Abs returns the absolute value of a number. For negative inputs, it returns -x; otherwise, it returns x.

func Clamp

func Clamp[T number](min, max, value T) T

Clamp restricts a value within a specified range [min, max]. If value is less than min, it returns min; if greater than max, it returns max.

func Factorial

func Factorial(x int) (int, error)

Factorial calculates the factorial of a non-negative integer x.

func GCD

func GCD(x, y int) int

GCD computes the greatest common divisor (GCD) of two integers x and y using the Euclidean algorithm.

func GetDivisors added in v1.20.0

func GetDivisors(n int) []int

Complexity = sqrt(n)

func IntPow

func IntPow(base, exp int) float64

IntPow calculates base raised to the power of exp. Supports both positive and negative exponents. Returns float64 for fractional results.

func IsEven

func IsEven(x int) bool

IsEven checks if an integer x is even.

func IsOdd

func IsOdd(x int) bool

IsOdd checks if an integer x is odd.

func IsPrime added in v1.19.0

func IsPrime(x int) bool

IsPrime checks if a number is prime. Complexity = O(sqrt(n)).

func LCM

func LCM(x, y int) int

LCM computes the least common multiple (LCM) of two integers x and y

func Max

func Max[T number](x, y T) T

Max returns the larger of two numbers x and y.

func Min

func Min[T number](x, y T) T

Min returns the smaller of two numbers x and y.

func PrimeList added in v1.20.0

func PrimeList(n int) []int

Sieve of Eratosthenes algorithm Complexity = O(n log log n).

func RoundDecimalPlaces added in v1.21.0

func RoundDecimalPlaces(val float64, places int) float64

RoundDecimalPlaces rounds a float64 to the specified number of decimal places. Negative values for places are clamped to 0 (i.e., rounds to a whole number).

func Sign

func Sign[T signedNumber](x T) int

Sign determines the sign of a signed number. It returns 1 if x is positive, -1 if x is negative, and 0 if x is zero.

func Sqrt added in v1.11.0

func Sqrt[T number](x T) (float64, error)

Sqrt computes the square root of a number using the standard library's math.Sqrt. For negative inputs, it returns the original value and an error.

func Swap

func Swap[T any](x, y *T)

Swap exchanges the values of two variables x and y.

Types

This section is empty.

Jump to

Keyboard shortcuts

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