circular

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package circular implements "circular numbers". This is a number that can be increased (or decreased) indefinitely while only using up a limited amount of memory. This feature comes with the limitiation in how distant two such numbers can be. Circular numbers have a maximum. The maximum distance is half the maximum value. If a number that has the maximum value is increased by 1, it becomes 0. If a number that has the value of 0 is decreased by 1, it becomes the maximum value. By comparing two circular numbers it is not possible to tell how often they wrapped. Therefore these two numbers must come from the same domain in order to make sense of the camparison.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Number

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

Number represents a "circular number". A Number is immutable. All modification to a Number will result in a new instance of a Number.

func New

func New(x, max uint32) Number

New returns a new circular number with the value of x and the maximum of max.

func (Number) Add

func (a Number) Add(b uint32) Number

Add returns a new circular number with a value that is increased by b.

func (Number) Dec

func (a Number) Dec() Number

Dec returns a new circular number with a value that is decreased by 1.

func (Number) Distance

func (a Number) Distance(b Number) uint32

Distance returns the distance of two circular numbers.

func (Number) Equals

func (a Number) Equals(b Number) bool

Equals returns whether two circular numbers have the same value.

func (Number) Gt

func (a Number) Gt(b Number) bool

Gt returns whether the circular number is greather than the circular number b.

func (Number) Gte

func (a Number) Gte(b Number) bool

Gte returns whether the circular number is greather than or equal to the circular number b.

func (Number) Inc

func (a Number) Inc() Number

Inc returns a new circular number with a value that is increased by 1.

Example
a := New(42, max)
b := a.Inc()

fmt.Println(b.Val())
Output:

43

func (Number) Lt

func (a Number) Lt(b Number) bool

Lt returns whether the circular number is lower than the circular number b.

func (Number) Lte

func (a Number) Lte(b Number) bool

Lte returns whether the circular number is lower than or equal to the circular number b.

func (Number) Sub

func (a Number) Sub(b uint32) Number

Sub returns a new circular number with a value that is decreased by b.

func (Number) Val

func (a Number) Val() uint32

Val returns the current value of the number.

Jump to

Keyboard shortcuts

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