intish

package
v0.10.9 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package intish provides a collection of strongly type integer arithmetic operations, to make it possible to avoid floating point math for simple operations when desired.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Signed](in T) T

Abs returns the absolute value of the integer.

func AbsBounds added in v0.10.4

func AbsBounds[T Signed](a, b T) (min T, max T)

AbsBounds resolves the absolute values of two numbers and then return the lower (absolute) value followed by the higher absolute value.

func AbsMax

func AbsMax[T Signed](a, b T) T

AbsMax resolves the absolute value of both arguments and returns the larger value.

func AbsMin

func AbsMin[T Signed](a, b T) T

AbsMin resolves the absolute value of both arguments and returns the smaller value.

func Bounds added in v0.10.4

func Bounds[T Numbers](a, b T) (min T, max T)

Bounds returns the two arguments as a (min,max): values less than zero become zero.

func Diff

func Diff[T Numbers](a, b T) T

Diff returns the absolute value of the difference between two values.

func FloatMillis added in v0.10.1

func FloatMillis[T Signed](in T) float64

FloatMillis reverses, though potentially (often) not without some loss of fidelity

func Max

func Max[T Numbers](a, b T) T

Max returns the highest value.

func Millis

func Millis[T Signed](in float64) T

Millis converts a float into a an integer that represents one thousandth of the units of the original.

Millis will panic in the case of an overflow (wraparound).

func Min

func Min[T Numbers](a, b T) T

Min returns the lowest value.

func Range added in v0.10.4

func Range[T Numbers](a, b T) (start T, end T)

Range orders two numbers and returns the pair as (lower, higher).

func RoundToLargestMultiple added in v0.10.1

func RoundToLargestMultiple[T Signed](a, b T) T

RoundToLargestMultiple rounds up to a larger value: The argument with the smaller absolute value is always the "multiple" and the "larger" is always the value that is rounded.

The output value is always *larget* than the input value.

func RoundToMultipleAwayFromZero

func RoundToMultipleAwayFromZero[T Signed](a, b T) T

RoundToMultipleAwayFromZero rounds a value to the nearest multiple of the other number. The argument with the smaller absolute value is always the "multiple" and value with larger absolute value is rounded.

The rounded always has a higher absolute value than the input value.

func RoundToMultipleTowardZero

func RoundToMultipleTowardZero[T Signed](a, b T) T

RoundToMultipleTowardZero rounds a value to the nearest multiple of the other number. The argument with the smaller absolute value is always the "multiple" and value with larger absolute value is rounded.

The rounded always has a lower absolute value than the input value.

func RoundToSmallestMultiple added in v0.10.1

func RoundToSmallestMultiple[T Signed](a, b T) T

RoundToSmallestMultiple rounds to smaller numbers: The argument with the smaller absolute value is always the "multiple" and the "larger" is always the value that is rounded.

The rounded value is always *smaller* than the input value.

Types

type Atomic added in v0.10.3

type Atomic[T Numbers] struct {
	// contains filtered or unexported fields
}

Atomic is a wrapper around sync/atomic.Int64 that provides type-preserving atomic storage for all numeric types.

Atomic shares an interface with the adt.Atomic wrapper types.

func (*Atomic[T]) Add added in v0.10.3

func (a *Atomic[T]) Add(delta T) T

func (*Atomic[T]) CompareAndSwap added in v0.10.3

func (a *Atomic[T]) CompareAndSwap(old, new T) bool

func (*Atomic[T]) Get added in v0.10.3

func (a *Atomic[T]) Get() T

func (*Atomic[T]) Load added in v0.10.3

func (a *Atomic[T]) Load() T

func (*Atomic[T]) Set added in v0.10.3

func (a *Atomic[T]) Set(in T)

func (*Atomic[T]) Store added in v0.10.3

func (a *Atomic[T]) Store(in T)

func (*Atomic[T]) Swap added in v0.10.3

func (a *Atomic[T]) Swap(new T) T

type AtomicFloat64 added in v0.10.3

type AtomicFloat64 struct {
	// contains filtered or unexported fields
}

AtomicFloat64 provides full-fidelity atomic storage for float64 values (by converting them) as bits to int64 and storing them using sync/atomic.Int64 values. The Add() method is correct, but must spin, unlike for integers which rely on an optimized underlying instruction.

AtomicFloat64 shares an interface with the adt.Atomic wrapper types.

func (*AtomicFloat64) Add added in v0.10.3

func (a *AtomicFloat64) Add(delta float64) float64

func (*AtomicFloat64) CompareAndSwap added in v0.10.3

func (a *AtomicFloat64) CompareAndSwap(old, new float64) bool

func (*AtomicFloat64) Get added in v0.10.3

func (a *AtomicFloat64) Get() float64

func (*AtomicFloat64) Load added in v0.10.3

func (a *AtomicFloat64) Load() float64

func (*AtomicFloat64) Set added in v0.10.3

func (a *AtomicFloat64) Set(in float64)

func (*AtomicFloat64) Store added in v0.10.3

func (a *AtomicFloat64) Store(value float64)

func (*AtomicFloat64) Swap added in v0.10.3

func (a *AtomicFloat64) Swap(new float64) float64

type Numbers

type Numbers interface {
	Signed | Unsigned
}

Numbers are the set of singed and unsinged integers, used by this package.

type Signed

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

Signed are all of the primitive signed integer types in go.

type Unsigned

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

Unsigned are all of the primitive signed integer types in go.

Jump to

Keyboard shortcuts

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