modular32

package
v0.0.0-...-e3819b7 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrBadModulo = errors.New("bad modulus")
	ErrBadIndex  = errors.New("bad index")
)

Error types

Functions

This section is empty.

Types

type Indexer

type Indexer struct {
	Modulus
	// contains filtered or unexported fields
}

Indexer provides a fast method for mapping a floating point modulus to a range of integers.

Example
package main

import (
	"fmt"

	"github.com/stewi1014/modular/modular32"
)

func main() {
	shifts := []string{
		"morning",
		"day",
		"evening",
	}

	// Errors can be ignored so long as we don't feed bad numbers
	indexer, _ := modular32.NewIndexer(24, len(shifts))

	for i := float32(0); i < 100; i += 13 {
		shift := shifts[indexer.Index(i)]
		fmt.Printf("It will be the %v shift in %v hours\n", shift, i)
	}

}
Output:

It will be the morning shift in 0 hours
It will be the day shift in 13 hours
It will be the morning shift in 26 hours
It will be the day shift in 39 hours
It will be the morning shift in 52 hours
It will be the evening shift in 65 hours
It will be the morning shift in 78 hours
It will be the evening shift in 91 hours

func NewIndexer

func NewIndexer(modulus float32, index int) (Indexer, error)

NewIndexer creates a new Indexer

index must not be larger than 2**16, and modulus must be a normalised float

Special cases:

NewIndexer(m, 0) = panic(integer divide by zero)
NewIndexer(m, i > 2**16) = ErrBadIndex
NewIndexer(0, i) = ErrBadModulo
NewIndexer(±Inf, i) = ErrBadModulo
NewIndexer(NaN, i) = ErrBadModulo
NewIndexer(m, i) = ErrBadModulo for |m| < 2**-126

func (Indexer) Index

func (i Indexer) Index(n float32) int

Index indexes n.

If n is NaN or ±Inf, it returns the index. Otherwise, it always satisfies 0 <= num < index

Special cases:

Index(NaN) = index
Index(±Inf) = index

type Modulus

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

Modulus defines a modulus. It offers greater performance than traditional floating point modulo calculations by pre-computing the inverse of the modulus's fractional component, and pre-computing a lookup table for different exponents in the given modulus, allowing direct computation of n mod m - no iteration or recursion is used. This obviously adds overhead to the creation of a new Modulus, but quickly breaks even after a few calls to Congruent.

func NewModulus

func NewModulus(modulus float32) Modulus

NewModulus creates a new Modulus. An Infinite modulus has no effect other than to waste CPU time

Special cases:

NewModulus(0) = panic(integer divide by zero)

func (Modulus) Congruent

func (m Modulus) Congruent(n float32) float32

Congruent returns n mod m.

Special cases:

Modulus{NaN}.Congruent(n) = NaN
Modulus{±Inf}.Congruent(n>=0) = n
Modulus{±Inf}.Congruent(n<0) = +Inf
Modulus{m}.Congruent(±Inf) = NaN
Modulus{m}.Congruent(NaN) = NaN

func (Modulus) Dist

func (m Modulus) Dist(n1, n2 float32) float32

Dist returns the distance and direction of n1 to n2.

func (Modulus) GetCongruent

func (m Modulus) GetCongruent(n1, n2 float32) float32

GetCongruent returns the closest number to n1 that is congruent to n2.

func (Modulus) Mod

func (m Modulus) Mod() float32

Mod returns the modulus

func (Modulus) NewIndexer

func (m Modulus) NewIndexer(index int) (Indexer, error)

NewIndexer creates a new indexer from the Modulus.

type Vec2Modulus

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

Vec2Modulus defines a modulus for 2d vectors

func NewVec2Modulus

func NewVec2Modulus(vec mgl.Vec2) Vec2Modulus

NewVec2Modulus creates a new 2d Vector Modulus

func (Vec2Modulus) Congruent

func (m Vec2Modulus) Congruent(vec mgl.Vec2) mgl.Vec2

Congruent performs Congruent() on all axis

func (Vec2Modulus) Dist

func (m Vec2Modulus) Dist(v1, v2 mgl.Vec2) mgl.Vec2

Dist returns the distance and direction of v1 to v2 It picks the shortest distance.

func (Vec2Modulus) GetCongruent

func (m Vec2Modulus) GetCongruent(v1, v2 mgl.Vec2) mgl.Vec2

GetCongruent returns the vector closest to v1 that is congruent to v2

type Vec3Modulus

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

Vec3Modulus defines a modulus for 3d vectors

func NewVec3Modulus

func NewVec3Modulus(vec mgl.Vec3) Vec3Modulus

NewVec3Modulus creates a new 3d Vector Modulus

func (Vec3Modulus) Congruent

func (m Vec3Modulus) Congruent(vec mgl.Vec3) mgl.Vec3

Congruent performs Congruent() on all axis

func (Vec3Modulus) Dist

func (m Vec3Modulus) Dist(v1, v2 mgl.Vec3) mgl.Vec3

Dist returns the distance and direction of v1 to v2 It picks the shortest distance.

func (Vec3Modulus) GetCongruent

func (m Vec3Modulus) GetCongruent(v1, v2 mgl.Vec3) mgl.Vec3

GetCongruent returns the vector closest to v1 that is congruent to v2

type Vec4Modulus

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

Vec4Modulus defines a modulus for 4d vectors

func NewVec4Modulus

func NewVec4Modulus(vec mgl.Vec4) Vec4Modulus

NewVec4Modulus creates a new 4d Vector Modulus

func (Vec4Modulus) Congruent

func (m Vec4Modulus) Congruent(vec mgl.Vec4) mgl.Vec4

Congruent performs Congruent() on all axis

func (Vec4Modulus) Dist

func (m Vec4Modulus) Dist(v1, v2 mgl.Vec4) mgl.Vec4

Dist returns the distance and direction of v1 to v2 It picks the shortest distance.

func (Vec4Modulus) GetCongruent

func (m Vec4Modulus) GetCongruent(v1, v2 mgl.Vec4) mgl.Vec4

GetCongruent returns the vector closest to v1 that is congruent to v2

Jump to

Keyboard shortcuts

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