simplemath

package
v2.2.8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package simplemath implements math operations used by the algorithms in the coollex package.

Additionally, it includes simple, naive implementations of math operations that are designed to facilitate writing tests. These functions return an error upon numeric overflow: Add, Mul, etc. They are used exclusively in tests and not in the Cool-lex algorithm implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b uint) (uint, error)

Add returns the sum of a and b if no numeric overflow occurs, or an error otherwise.

func BitEq32

func BitEq32(a, b int32) int32

BitEq32 implements the bitwise-equivalence operation, that is, NOT(XOR(a,b)).

func BitEq64

func BitEq64(a, b int64) int64

BitEq64 implements the bitwise-equivalence operation, that is, NOT(XOR(a,b)).

func Doz32

func Doz32(a, b int32) int32

Doz32 implements the difference-or-zero function, in a branchless fashion. See `simplemath.DozB64` for details.

func Doz64

func Doz64(a, b int64) int64

Doz64 implements the difference-or-zero function, in a branchless fashion. See `simplemath.DozB64` for details.

func DozB32

func DozB32(a, b int32) int32

DozB32 implements the difference-or-zero function. See `simplemath.DozB64` for details.

func DozB64

func DozB64(a, b int64) int64

DozB64 implements the difference-or-zero function. DozB64(a,b) is a - b if a >= b, and is 0 if a < b.

It is named "saturated subtraction" in The Coolest Way to Generate Combinations paper by Frank Ruskey and Aaron Williams, see 3.3. Implementation in Computer words, page 10.

See also: `simplemath.Doz64`.

func Factorial

func Factorial(n uint) (uint, error)

Factorial returns the factorial of n, or 1 for n=0. Error is reported if numeric overflow occurs.

func Mul

func Mul(a, b uint) (uint, error)

Mul returns the product of a and b, or an error if numeric overflow occurs.

func MulRange

func MulRange(n2, n1 uint) (r uint, err error)

MulRange calculates the product of all integers within the inclusive interval [n1, n2] (or [n2, n1] if n2 < n1).

It returns the product if no numeric overflow occurs. Otherwise, it returns an error, and the partial product calculated before the overflow is stored in 'r'.

func NumComb

func NumComb(n, k uint) (uint, error)

NumComb calculates the number of combinations for the specified k and n. In other words, it calculates the binomial coefficient: C(n,k)=n!/k!(n-k)!.

n: number of elements to combine; n>=k must hold. k: number of elements in a combination.

Error is reported if numeric overflow occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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