math

package
v0.31.10 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package math contains number utilities that are not included in the Go math library, but that are useful. Feel free to add to it.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsInt

func AbsInt[M ints](a M) M

AbsInt returns the absolute value of the given int.

Example
fmt.Print(AbsInt(-5), AbsInt(5))
Output:

5 5

func CubeInt

func CubeInt[M ints](a M) M

CubeInt returns the cube of the given integer

Example
fmt.Print(CubeInt(2))
Output:

8

func DiffInts

func DiffInts[M ints](values ...M) (diffs []M)

DiffInts returns the difference between each item, and the next item in the list. The last diff is between the last item and the first item. Returns the differences as a slice. If there is only one item, it will return just the one item, and not diff it with itself.

Example
diffs := DiffInts(50, 13, 9, 200, 7, 23)

fmt.Print(diffs)
Output:

[37 4 -191 193 -16 -27]

func MaxInt

func MaxInt[M ints](values ...M) (index int, value M)

MaxInt returns the maximum value from a slice of ints, and the index of that value. The index will be -1 if no items are given.

Example
i, v := MaxInt(50, 13, 9, 200, 7, 23)

fmt.Printf("%d,%d", i, v)
Output:

3,200

func MinInt

func MinInt[M ints](values ...M) (index int, value M)

MinInt returns the minimum value from a slice of ints, and the zero-based index of that value. The index will be -1 if there are no values given.

Example
i, v := MinInt(50, 13, 9, 200, 7, 23)

fmt.Printf("%d,%d", i, v)
Output:

4,7

func PowerInt

func PowerInt[M ints](base, power M) M

PowerInt returns the given base integer raised to the given power.

If your base integer is zero, the result will be zero regardless of the power. Fractions will return as zero.

Example
fmt.Print(PowerInt(3, 3))
Output:

27

func RoundFloat

func RoundFloat(f float64, digits int) float64

RoundFloat rounds a float to another float with the given number of digits. Can be used to fix precision errors.

func RoundInt

func RoundInt(f float64) int

RoundInt rounds a float to an integer.

func SqSqInt

func SqSqInt[M ints](a M) M

SqSqInt returns the given integer to its fourth power

Example
fmt.Print(SqSqInt(2))
Output:

16

func SquareInt

func SquareInt[M ints](a M) M

SquareInt returns the square of the given integer

Example
fmt.Print(SquareInt(2))
Output:

4

func SumInts

func SumInts[M ints](values ...M) (sums []M)

SumInts returns the sum between each item, and the next item in the slice. The last sum is between the last item and the first item. If there is only one item, it will just return the one item, and not sum it with itself.

Example
sums := SumInts(50, 13, -9, 200, 7, 23)

fmt.Print(sums)
Output:

[63 4 191 207 30 73]

Types

This section is empty.

Jump to

Keyboard shortcuts

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