num

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package num implements various utility functions regarding numeric types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Real](x T) T

Abs returns the absolute value of x.

func ByteSizeT added in v0.3.0

func ByteSizeT[T Integer]() int

ByteSizeT returns the bytes required to express value of type T in int.

func IsPowerOfTwo

func IsPowerOfTwo[T Integer](x T) bool

IsPowerOfTwo returns whether x is a power of two. If x < 0, it always returns false.

func IsSigned

func IsSigned[T Real]() bool

IsSigned returns true if type T is a signed type.

func Log2

func Log2[T Integer](x T) int

Log2 returns floor(log2(x)). Panics if x <= 0.

func Max

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

Max returns the larger value between x and y.

func MaxN

func MaxN[T Real](x ...T) T

MaxN returns the largest number of x. If x is empty, it returns the zero value of T.

func MaxT

func MaxT[T Integer]() uint64

MaxT returns the maximum possible value of type T in uint64.

func Min

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

Min returns the smaller value between x and y.

func MinN

func MinN[T Real](x ...T) T

MinN returns the smallest number of x. If x is empty, it returns the zero value of T.

func MinT

func MinT[T Integer]() int64

MinT returns the minimum possible value of type T in int64.

func RoundRatio

func RoundRatio[T Integer](x, y T) T

RoundRatio returns round(x/y).

func RoundRatioBits

func RoundRatioBits[T Integer](x T, bits int) T

RoundRatioBits is a bit-optimzed version of RoundRatio: it returns round(x/2^bits).

If bits <= 0, it panics.

func SizeT

func SizeT[T Integer]() int

SizeT returns the bits required to express value of type T in int.

func Sqrt

func Sqrt[T Integer](x T) T

Sqrt returns floor(sqrt2(x)). Panics if x < 0.

Types

type Integer

type Integer interface {
	Unsigned | int | int8 | int16 | int32 | int64
}

Integer represents the Integer type.

type Number

type Number interface {
	Real | complex64 | complex128
}

Number represents Integer, Float, and Complex type.

type Real

type Real interface {
	Integer | float32 | float64
}

Real represents the Integer and Float type.

type Unsigned

type Unsigned interface {
	uint | uint8 | uint16 | uint32 | uint64 | uintptr
}

Unsigned represents the unsigned Integer type.

Jump to

Keyboard shortcuts

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