num

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: BSD-3-Clause Imports: 0 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Signed | Float](x T) T

Abs returns the absolute value of the given value.

func Convert

func Convert[Tr Number, Ts Number](src Ts) Tr

Convert converts any number to any other, using generics, with the return number specified by the first type argument, and the source number by the second. Typically the source type can be inferred but the return cannot. See SetNumber for a version that uses a pointer to the destination which avoids the need to specify the type parameter.

func FromBool

func FromBool[T Number](v bool) T

FromBool returns a 1 if the bool is true and a 0 for false. Typically the type parameter cannot be inferred and must be provided. See SetFromBool for a version that uses a pointer to the destination which avoids the need to specify the type parameter.

func SetFromBool

func SetFromBool[T Number](dst *T, b bool)

SetFromBool converts a bool into a number, using generics, setting the pointer to the dst destination value to a 1 if bool is true, and 0 otherwise. This version of FromBool does not require type parameters typically.

func SetNumber

func SetNumber[Td Number, Ts Number](dst *Td, src Ts)

SetNumber converts any number to any other, using generics, setting the pointer to the dst destination value to the src value. This version of Convert does not require type parameters typically.

func ToBool

func ToBool[T Number](v T) bool

ToBool returns a bool true if the given number is not zero, and false if it is zero, providing a direct way to convert numbers to bools as is done automatically in C and other languages.

Types

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type. If future releases of Go add new predeclared complex numeric types, this constraint will be modified to include them.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.

type Number

type Number interface {
	Integer | Float
}

Number is a constraint that permits any single-valued numerical type, i.e., Integer or Float. It excludes complex numbers.

type Ordered

type Ordered interface {
	Integer | Float | ~string
}

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is a constraint that permits any signed integer type. If future releases of Go add new predeclared signed integer types, this constraint will be modified to include them.

type Unsigned

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

Unsigned is a constraint that permits any unsigned integer type. If future releases of Go add new predeclared unsigned integer types, this constraint will be modified to include them.

Jump to

Keyboard shortcuts

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