maths

package module
v0.0.0-...-24dceeb Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

README

go-maths

PkgGoDev

This package provides a generic library for math.

Installation

$ go get github.com/jabolopes/go-maths

You can use go get -u to update the package. If you are using Go modules, you can also just import the package and it will be automatically downloaded on the first compilation.

Examples

math.Abs(2)
math.Abs(3.0)
math.Abs(uint32(4))

math.Neg(2)
math.Neg(3.0)
math.Neg(uint32(4))

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T number](value T) T

Abs returns the absolute of value.

func Clamp

func Clamp[T cmp.Ordered](value, min, max T) T

Clamp clamps value to the range [min, max]. Returns value if value is already in the range [min, max]. Otherwise, it either returns min or max depending on whether value is less than min or greater than max, respectively.

func DownToMultipleOf

func DownToMultipleOf[T constraints.Integer](n, m T) T

Down rounds n down to nearest multiple of m

Example
fmt.Printf("%d", maths.DownToMultipleOf(22, 10))
Output:

20

func Max

func Max[T cmp.Ordered](a, b T) T

func Min

func Min[T cmp.Ordered](a, b T) T

func Neg

func Neg[T number](value T) T

Neg returns the negative of value. It does not negate value. For negating, simply use -value instead.

func UpToMultipleOf

func UpToMultipleOf[T constraints.Integer](n, m T) T

Up rounds n up to nearest multiple of m

This is actually -1 * Down(-n, m)

Example
fmt.Printf("%d", maths.UpToMultipleOf(35, 10))
Output:

40

Types

This section is empty.

Jump to

Keyboard shortcuts

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