Documentation ¶
Overview ¶
Package sci implements a system for performing calculations on physical quantities. The math API is inspired by the big package of the go standard library: The receiver of a .
The core
Index ¶
- Constants
- Variables
- type BaseUnit
- type BaseUnitAlreadyDefinedError
- type Converter
- type DerivedUnit
- type DivUnit
- type ExpToBigError
- type MagnitudeError
- type Measure
- type MulUnit
- type NilUnit
- type NormalizedUnit
- type Prefix
- type System
- func (sys *System) DefineBaseUnit(name string, m Measure) (*BaseUnit, error)
- func (sys *System) DefineUnit(name string, valstr string) (Unit, error)
- func (sys *System) LookupUnit(name string) (Unit, error)
- func (sys *System) MustDefineBaseUnit(name string, m Measure) *BaseUnit
- func (sys *System) MustDefineUnit(name string, val string) Unit
- func (sys *System) MustParse(val string) *Value
- func (sys *System) Nil() *NilUnit
- func (sys *System) Parse(val string) (*Value, error)
- func (sys *System) ParseUnit(unitstr string) (Unit, error)
- type Unit
- type UnitAlreadyDefinedError
- type UnitNotDefinedError
- type Value
- Bugs
Constants ¶
const ( // MaxExp represents the largest absolute value that an exponent is allowed to // have within this library. It's a conservative cap for now, as I (scott) am // not very sure what sort of issues will arise if left higher or unbounded. MaxExp = 4 )
Variables ¶
var ( // ErrIncompatibleTypes is returned when attempted to perform an operation // (such as addition) on two incompatible types. ErrIncompatibleTypes = errors.New("incompatible types") )
Functions ¶
This section is empty.
Types ¶
type BaseUnit ¶
BaseUnit represents the a base unit of a given measure against which other units can be defined. For example, a meter is a base unit of length.
func (*BaseUnit) PopulateNormalizedUnit ¶
func (bu *BaseUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inverted bool)
PopulateNormalizedUnit implements Unit
type BaseUnitAlreadyDefinedError ¶
type BaseUnitAlreadyDefinedError struct {
Existing *BaseUnit
}
BaseUnitAlreadyDefinedError is an error that occurs when attempting to redefine the base unit used for some measure in a system of units. A system of units may only have one base unit per measure to ensure that we can define any value belonging to a given measure in relation to a single base unit.
func (*BaseUnitAlreadyDefinedError) Error ¶
func (berr *BaseUnitAlreadyDefinedError) Error() string
Error implements the error interface
type DerivedUnit ¶
type DerivedUnit struct {
Value *Value
}
DerivedUnit represents a unit expressed in relation to some base unit.
func (*DerivedUnit) PopulateNormalizedUnit ¶
func (u *DerivedUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)
PopulateNormalizedUnit implements Unit
type DivUnit ¶
DivUnit represents a compound unit such as "feet / hour"
func (*DivUnit) PopulateNormalizedUnit ¶
func (u *DivUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)
PopulateNormalizedUnit implements Unit
type ExpToBigError ¶
type ExpToBigError struct {
Exp int64
}
ExpToBigError is the error that is returned when a string that is being parsed into a unit has an exponent that is too large.
func (*ExpToBigError) Error ¶
func (experr *ExpToBigError) Error() string
Error implements the error interface
type MagnitudeError ¶
type MagnitudeError struct {
M string
}
MagnitudeError represents the error produces when trying to operate on a value whose magnitude (the M field) is invalid.
func (*MagnitudeError) Error ¶
func (merr *MagnitudeError) Error() string
Error implements the error interface
type Measure ¶
type Measure string
Measure represents a domain of measurement, such as length, time, or mass.
type MulUnit ¶
type MulUnit []Unit
MulUnit represents a compound unit such as "foot*pound"
func (*MulUnit) PopulateNormalizedUnit ¶
func (u *MulUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)
PopulateNormalizedUnit implements Unit
type NilUnit ¶
type NilUnit struct {
// contains filtered or unexported fields
}
NilUnit represents "no unit". Values of it represents "a mignitude without a unit". NilUnit is also used to represent inverse units, such as hz (1 / s) when combind using DivUnit.
func (*NilUnit) PopulateNormalizedUnit ¶
func (u *NilUnit) PopulateNormalizedUnit(nu *NormalizedUnit, inv bool)
PopulateNormalizedUnit implements Unit
type NormalizedUnit ¶
type NormalizedUnit struct { Components map[*BaseUnit]int // contains filtered or unexported fields }
NormalizedUnit represents the non-aliased form of a unit, expressed completely in terms of base units. derived units are expanded into base units and then contribute themselves to either the numerator by increasing the count by one, or to the denominator by decreasing the count by one. This will be a recursive process.
func (*NormalizedUnit) Add ¶
func (nu *NormalizedUnit) Add(bu *BaseUnit, exp int)
Add contributes the provided base unit at the provided exponent (negative numbers representing to the negative power) to the normalized unit.
func (*NormalizedUnit) Invert ¶
func (nu *NormalizedUnit) Invert() *NormalizedUnit
Invert returns a copy of the normalized unit, inverted.
type System ¶
type System struct { // Name is an optional name for a system Name string // BaseUnits represents the collection of defined base units BaseUnits map[Measure]*BaseUnit // contains filtered or unexported fields }
System represents a system of measurement.
func (*System) DefineBaseUnit ¶
DefineBaseUnit defines a base unit of measure in the unit system. Only one base unit per measure is allowed, and every other unit in a given system must be defined in terms of the base units of the system.
func (*System) DefineUnit ¶
DefineUnit adds a new DerivedUnit to the system using val as the definition. val must be expressed in terms of units previously defined within the system.
func (*System) LookupUnit ¶
LookupUnit finds a unit by name or alias in the system of units. An error of type *UnitNotFoundError will be returned if a unit with the given name has not been previously defined.
func (*System) MustDefineBaseUnit ¶
MustDefineBaseUnit is the panicking version of define base unit
func (*System) MustDefineUnit ¶
MustDefineUnit is the panicking version of DefineUnit
type Unit ¶
type Unit interface { fmt.Stringer System() *System PopulateNormalizedUnit(nu *NormalizedUnit, inverted bool) }
Unit represents any unit of measure
type UnitAlreadyDefinedError ¶
UnitAlreadyDefinedError is an error that occurs when attempting to redefine the a named unit. A given system may not have multiple units that have the same name dfined within it.
func (*UnitAlreadyDefinedError) Error ¶
func (uerr *UnitAlreadyDefinedError) Error() string
Error implements the error interface
type UnitNotDefinedError ¶
type UnitNotDefinedError struct { // Name is the name for the unit attempted to be found Name string }
UnitNotDefinedError is an error that occurs when attempting to lookup a unit in the system.
func (*UnitNotDefinedError) Error ¶
func (uerr *UnitNotDefinedError) Error() string
Error implements the error interface
type Value ¶
type Value struct { // M is the magnitude or the multitude of the value (depending upon whether or // not the unit is collective or non-collective), expressed as a string // (parsing rules TBD) M string // U is the unit of the value. U Unit }
Value represents a value. Examples include "3 mm" or "10 m/s"
func (*Value) Add ¶
Add adds `l` and `r` together and stores the result in `v`, providing that `l` and `r` can be added together.
Notes ¶
Bugs ¶
we don't have unit conversions yet, so the following adoption of l.U for the return value is broken.