fxp

package
v5.20.4 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: MPL-2.0 Imports: 11 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EvalOperators = eval.FixedOperators[DP](true)
	EvalFuncs     = eval.FixedFunctions[DP]()
)

The evaluator operators and functions that will be used when calling NewEvaluator().

View Source
var (
	Min                 = Int(f64.Min)
	NegPointEight       = FromStringForced("-0.8")
	Twentieth           = FromStringForced("0.05")
	PointZeroSix        = FromStringForced("0.06")
	PointZeroSeven      = FromStringForced("0.07")
	PointZeroEight      = FromStringForced("0.08")
	PointZeroNine       = FromStringForced("0.09")
	Tenth               = FromStringForced("0.1")
	PointOneTwo         = FromStringForced("0.12")
	Eighth              = FromStringForced("0.125")
	PointOneFive        = FromStringForced("0.15")
	Fifth               = FromStringForced("0.2")
	Quarter             = FromStringForced("0.25")
	ThreeTenths         = FromStringForced("0.3")
	TwoFifths           = FromStringForced("0.4")
	Half                = FromStringForced("0.5")
	ThreeFifths         = FromStringForced("0.6")
	SevenTenths         = FromStringForced("0.7")
	ThreeQuarters       = FromStringForced("0.75")
	FourFifths          = FromStringForced("0.8")
	One                 = From(1)
	OnePointOne         = FromStringForced("1.1")
	OnePointTwo         = FromStringForced("1.2")
	OneAndAQuarter      = FromStringForced("1.25")
	OneAndAHalf         = FromStringForced("1.5")
	Two                 = From(2)
	TwoAndAHalf         = FromStringForced("2.5")
	Three               = From(3)
	ThreeAndAHalf       = FromStringForced("3.5")
	Four                = From(4)
	Five                = From(5)
	Six                 = From(6)
	Seven               = From(7)
	Eight               = From(8)
	Nine                = From(9)
	Ten                 = From(10)
	Eleven              = From(11)
	Twelve              = From(12)
	Thirteen            = From(13)
	Fifteen             = From(15)
	Sixteen             = From(16)
	Nineteen            = From(19)
	Twenty              = From(20)
	TwentyFour          = From(24)
	TwentyFive          = From(25)
	ThirtySix           = From(36)
	Thirty              = From(30)
	Forty               = From(40)
	Fifty               = From(50)
	Sixty               = From(60)
	Seventy             = From(70)
	Eighty              = From(80)
	NinetyNine          = From(99)
	Hundred             = From(100)
	OneHundredFifty     = From(150)
	FiveHundred         = From(500)
	ThousandMinusOne    = From(999)
	Thousand            = From(1000)
	TwoThousand         = From(2000)
	ThirtySixHundred    = From(3600)
	TenThousandMinusOne = From(9999)
	ThirtySixThousand   = From(36000)
	MileInInches        = From(63360)
	MillionMinusOne     = From(999999)
	TenMillionMinusOne  = From(9999999)
	BillionMinusOne     = From(999999999)
	MaxBasePoints       = MillionMinusOne
	Max                 = Int(f64.Max)
)

Common values that can be reused.

View Source
var DebugVariableResolver = false

DebugVariableResolver produces debug output for the variable resolver when enabled.

LengthUnits holds all possible values.

View Source
var WeightUnits = []WeightUnit{
	Pound,
	PoundAlt,
	Ounce,
	Ton,
	TonAlt,
	Kilogram,
	Gram,
}

WeightUnits holds all possible values.

Functions

func As

func As[T xmath.Numeric](value Int) T

As returns the equivalent value in the destination type.

func NewEvaluator

func NewEvaluator(resolver eval.VariableResolver) *eval.Evaluator

NewEvaluator creates a new evaluator whose number type is an Int.

func ResetIfOutOfRange

func ResetIfOutOfRange[T xmath.Numeric | Int](value, minValue, maxValue, defValue T) T

ResetIfOutOfRange checks the value and if it is lower than minValue or greater than maxValue, returns defValue, otherwise returns value.

func SecondsToDuration added in v5.20.4

func SecondsToDuration(value Int) time.Duration

SecondsToDuration converts a fixed-point value in seconds to a time.Duration.

Types

type DP

type DP = fixed.D4

DP is an alias for the fixed-point decimal places configuration we are using.

type Fraction

type Fraction = f64.Fraction[DP]

Fraction is an alias for the fixed-point fractional type we are using.

func NewFraction

func NewFraction(str string) Fraction

NewFraction creates a new fractional value from a string.

type Int

type Int = f64.Int[DP]

Int is an alias for the fixed-point type we are using.

func ApplyRounding

func ApplyRounding(value Int, roundDown bool) Int

ApplyRounding rounds in the positive direction of roundDown is false, or in the negative direction if roundDown is true.

func EvaluateToNumber

func EvaluateToNumber(expression string, resolver eval.VariableResolver) Int

EvaluateToNumber evaluates the provided expression and returns a number.

func Extract

func Extract(in string) (value Int, remainder string)

Extract a leading value from a string. If a value is found, it is returned along with the portion of the string that was unused. If a value is not found, then 0 is returned along with the original string.

func From

func From[T xmath.Numeric](value T) Int

From creates an Int from a numeric value.

func FromString

func FromString(value string) (Int, error)

FromString creates an Int from a string.

func FromStringForced

func FromStringForced(value string) Int

FromStringForced creates an Int from a string, ignoring any conversion inaccuracies.

type Length added in v5.16.2

type Length Int

Length contains a fixed-point value in inches. Conversions to/from metric are done using the simplified Length metric conversion of 1 yd = 1 meter. For consistency, all metric lengths are converted to meters, then to yards, rather than the variations at different lengths that the Length rules suggest.

func LengthFromInteger added in v5.16.2

func LengthFromInteger[T constraints.Integer](value T, unit LengthUnit) Length

LengthFromInteger creates a new Length.

func LengthFromString added in v5.16.2

func LengthFromString(text string, defaultUnits LengthUnit) (Length, error)

LengthFromString creates a new Length. May have any of the known Units suffixes, a feet and inches format (e.g. 6'2"), or no notation at all, in which case defaultUnits is used.

func LengthFromStringForced added in v5.16.2

func LengthFromStringForced(text string, defaultUnits LengthUnit) Length

LengthFromStringForced creates a new Length. May have any of the known Units suffixes, a feet and inches format (e.g. 6'2"), or no notation at all, in which case defaultUnits is used.

func (Length) MarshalJSON added in v5.16.2

func (l Length) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Length) String added in v5.16.2

func (l Length) String() string

func (*Length) UnmarshalJSON added in v5.16.2

func (l *Length) UnmarshalJSON(in []byte) error

UnmarshalJSON implements json.Unmarshaler.

type LengthUnit added in v5.19.0

type LengthUnit byte

LengthUnit holds the length unit type. Note that conversions to/from metric are done using the simplified GURPS metric conversion of 1 yd = 1 meter. For consistency, all metric lengths are converted to meters, then to yards, rather than the variations at different lengths that the GURPS rules suggest.

const (
	FeetAndInches LengthUnit = iota
	Inch
	Feet
	Yard
	Mile
	Centimeter
	Kilometer
	Meter
)

Possible values.

const LastLengthUnit LengthUnit = Meter

LastLengthUnit is the last valid value.

func ExtractLengthUnit added in v5.19.0

func ExtractLengthUnit(str string) LengthUnit

ExtractLengthUnit extracts the value from a string.

func (LengthUnit) EnsureValid added in v5.19.0

func (enum LengthUnit) EnsureValid() LengthUnit

EnsureValid ensures this is of a known value.

func (LengthUnit) Format added in v5.19.0

func (enum LengthUnit) Format(length Length) string

Format the length for this LengthUnit.

func (LengthUnit) Key added in v5.19.0

func (enum LengthUnit) Key() string

Key returns the key used in serialization.

func (LengthUnit) MarshalText added in v5.19.0

func (enum LengthUnit) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (LengthUnit) String added in v5.19.0

func (enum LengthUnit) String() string

String implements fmt.Stringer.

func (LengthUnit) ToInches added in v5.19.0

func (enum LengthUnit) ToInches(length Int) Int

ToInches converts the length in this LengthUnit to inches.

func (*LengthUnit) UnmarshalText added in v5.19.0

func (enum *LengthUnit) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Weight added in v5.16.2

type Weight Int

Weight contains a fixed-point value in pounds.

func WeightFromInteger added in v5.16.2

func WeightFromInteger[T constraints.Integer](value T, unit WeightUnit) Weight

WeightFromInteger creates a new Weight.

func WeightFromString added in v5.16.2

func WeightFromString(text string, defaultUnits WeightUnit) (Weight, error)

WeightFromString creates a new Weight. May have any of the known Weight suffixes or no notation at all, in which case defaultUnits is used.

func WeightFromStringForced added in v5.16.2

func WeightFromStringForced(text string, defaultUnits WeightUnit) Weight

WeightFromStringForced creates a new Weight. May have any of the known Weight suffixes or no notation at all, in which case defaultUnits is used.

func (Weight) MarshalJSON added in v5.16.2

func (w Weight) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Weight) String added in v5.16.2

func (w Weight) String() string

func (*Weight) UnmarshalJSON added in v5.16.2

func (w *Weight) UnmarshalJSON(in []byte) error

UnmarshalJSON implements json.Unmarshaler.

type WeightUnit added in v5.19.0

type WeightUnit byte

WeightUnit holds the weight unit type. Note that conversions to/from metric are done using the simplified GURPS metric conversion of 1 lb = 0.5kg. For consistency, all metric weights are converted to kilograms, then to pounds, rather than the variations at different weights that the GURPS rules suggest.

const (
	Pound WeightUnit = iota
	PoundAlt
	Ounce
	Ton
	TonAlt
	Kilogram
	Gram
)

Possible values.

const LastWeightUnit WeightUnit = Gram

LastWeightUnit is the last valid value.

func ExtractWeightUnit added in v5.19.0

func ExtractWeightUnit(str string) WeightUnit

ExtractWeightUnit extracts the value from a string.

func TrailingWeightUnitFromString added in v5.19.0

func TrailingWeightUnitFromString(s string, defUnits WeightUnit) WeightUnit

TrailingWeightUnitFromString extracts a trailing WeightUnit from a string.

func (WeightUnit) EnsureValid added in v5.19.0

func (enum WeightUnit) EnsureValid() WeightUnit

EnsureValid ensures this is of a known value.

func (WeightUnit) Format added in v5.19.0

func (enum WeightUnit) Format(weight Weight) string

Format the weight for this WeightUnit.

func (WeightUnit) Key added in v5.19.0

func (enum WeightUnit) Key() string

Key returns the key used in serialization.

func (WeightUnit) MarshalText added in v5.19.0

func (enum WeightUnit) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (WeightUnit) String added in v5.19.0

func (enum WeightUnit) String() string

String implements fmt.Stringer.

func (WeightUnit) ToPounds added in v5.19.0

func (enum WeightUnit) ToPounds(weight Int) Int

ToPounds the weight for this WeightUnit.

func (*WeightUnit) UnmarshalText added in v5.19.0

func (enum *WeightUnit) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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