unit

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: BSD-3-Clause Imports: 7 Imported by: 5

Documentation

Overview

Package unit provides a set of types and constants that facilitate the use of the International System of Units (SI).

The unit package provides two main functionalities: compile-time type-safe base SI units and common derived units; and a system for dynamically extensible user-defined units.

Static SI units

This package provides a number of types representing either an SI base unit or a common combination of base units, named for the physical quantity it represents (Length, Mass, Pressure, etc.). Each type is defined from float64. The value of the float64 represents the quantity of that unit as expressed in SI base units (kilogram, metre, Pascal, etc.). For example,

height := 1.6 * unit.Metre
acc := unit.Acceleration(9.8)

creates a variable named 'height' with a value of 1.6 metres, and a variable named 'acc' with a value of 9.8 metres per second squared. These types can be used to add compile-time safety to code. For example,

func unitVolume(t unit.Temperature, p unit.Pressure) unit.Volume {
	...
}

func main(){
	t := 300 * unit.Kelvin
	p := 500 * unit.Kilo * unit.Pascal
	v := unitVolume(p, t) // compile-time error
}

gives a compile-time error (temperature type does not match pressure type) while the corresponding code using float64 runs without error.

func float64Volume(temperature, pressure float64) float64 {
	...
}

func main(){
	t := 300.0 // Kelvin
	p := 500000.0 // Pascals
	v := float64Volume(p, t) // no error
}

Many types have constants defined representing named SI units (Metre, Kilogram, etc. ) or SI derived units (Pascal, Hz, etc.). The unit package additionally provides untyped constants for SI prefixes, so the following are all equivalent.

l := 0.001 * unit.Metre
k := 1 * unit.Milli * unit.Metre
j := unit.Length(0.001)

Additional SI-derived static units can also be defined by adding types that satisfy the Uniter interface described below.

Dynamic user-extensible unit system

The unit package also provides the Unit type, a representation of a general dimensional value. Unit can be used to help prevent errors of dimensionality when multiplying or dividing dimensional numbers defined at run time. New variables of type Unit can be created with the New function and the Dimensions map. For example, the code

rate := unit.New(1 * unit.Milli, Dimensions{MoleDim: 1, TimeDim: -1})

creates a variable "rate" which has a value of 1e-3 mol/s. Methods of unit can be used to modify this value, for example:

rate.Mul(1 * unit.Centi * unit.Metre).Div(1 * unit.Milli * unit.Volt)

To convert the unit back into a typed float64 value, the From methods of the dimensional types should be used. From will return an error if the dimensions do not match.

var energy unit.Energy
err := energy.From(acc)

Domain-specific problems may need custom dimensions, and for this purpose NewDimension should be used to help avoid accidental overlap between packages. For example, results from a blood test may be measured in "White blood cells per slide". In this case, NewDimension should be used to create a 'WhiteBloodCell' dimension. NewDimension takes in a string which will be used for printing that dimension, and will return a unique dimension number.

wbc := unit.NewDimension("WhiteBloodCell")

NewDimension should not be used, however, to create the unit of 'Slide', because in this case slide is just a measurement of liquid volume. Instead, a constant could be defined.

const Slide unit.Volume =  0.1 * unit.Micro * unit.Litre

Note that unit cannot catch all errors related to dimensionality. Different physical ideas are sometimes expressed with the same dimensions and unit is incapable of catching these mismatches. For example, energy and torque are both expressed as force times distance (Newton-metres in SI), but it is wrong to say that a torque of 10 N·m is the same as 10 J, even though the dimensions agree. Despite this, using the defined types to represent units can help to catch errors at compile-time. For example, using unit.Torque allows you to define a statically typed function like so

func LeverLength(apply unit.Force, want unit.Torque) unit.Length {
	return unit.Length(float64(want)/float64(apply))
}

This will prevent an energy value being provided to LeverLength in place of a torque value.

Example (Horsepower)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/unit"
)

func main() {
	// One mechanical horsepower ≡ 33,000 ft-lbf/min.
	foot := unit.Length(0.3048)
	pound := unit.Mass(0.45359237)

	gravity := unit.Acceleration(9.80665)
	poundforce := pound.Unit().Mul(gravity)

	hp := ((33000 * foot).Unit().Mul(poundforce)).Div(unit.Minute)
	fmt.Println("1 hp =", hp)

	watt := unit.Power(1)
	fmt.Println("W is equivalent to hp?", unit.DimensionsMatch(hp, watt))

}
Output:


1 hp = 745.6998715822701 kg m^2 s^-3
W is equivalent to hp? true

Index

Examples

Constants

View Source
const (
	Yotta = 1e24
	Zetta = 1e21
	Exa   = 1e18
	Peta  = 1e15
	Tera  = 1e12
	Giga  = 1e9
	Mega  = 1e6
	Kilo  = 1e3
	Hecto = 1e2
	Deca  = 1e1

	Deci  = 1e-1
	Centi = 1e-2
	Milli = 1e-3
	Micro = 1e-6
	Nano  = 1e-9
	Pico  = 1e-12
	Femto = 1e-15
	Atto  = 1e-18
	Zepto = 1e-21
	Yocto = 1e-24
)
View Source
const (
	Second Time = 1

	Minute = 60 * Second
	Hour   = 60 * Minute
)

Variables

This section is empty.

Functions

func DimensionsMatch

func DimensionsMatch(a, b Uniter) bool

DimensionsMatch checks if the dimensions of two Uniters are the same.

func SymbolExists

func SymbolExists(symbol string) bool

SymbolExists returns whether the given symbol is already in use.

Types

type AbsorbedRadioactiveDose

type AbsorbedRadioactiveDose float64

AbsorbedRadioactiveDose is a measure of absorbed dose of ionizing radiation in grays.

const Gray AbsorbedRadioactiveDose = 1

func (AbsorbedRadioactiveDose) AbsorbedRadioactiveDose

func (a AbsorbedRadioactiveDose) AbsorbedRadioactiveDose() AbsorbedRadioactiveDose

AbsorbedRadioactiveDose allows AbsorbedRadioactiveDose to implement a AbsorbedRadioactiveDoseer interface.

func (AbsorbedRadioactiveDose) Format

func (a AbsorbedRadioactiveDose) Format(fs fmt.State, c rune)

func (*AbsorbedRadioactiveDose) From

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (AbsorbedRadioactiveDose) Unit

func (a AbsorbedRadioactiveDose) Unit() *Unit

Unit converts the AbsorbedRadioactiveDose to a *Unit.

type Acceleration

type Acceleration float64

Acceleration represents an acceleration in metres per second squared.

func (Acceleration) Acceleration

func (a Acceleration) Acceleration() Acceleration

Acceleration allows Acceleration to implement a Accelerationer interface.

func (Acceleration) Format

func (a Acceleration) Format(fs fmt.State, c rune)

func (*Acceleration) From

func (a *Acceleration) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Acceleration) Unit

func (a Acceleration) Unit() *Unit

Unit converts the Acceleration to a *Unit.

type Angle

type Angle float64

Angle represents an angle in radians.

const Rad Angle = 1

func (Angle) Angle

func (a Angle) Angle() Angle

Angle allows Angle to implement a Angleer interface.

func (Angle) Format

func (a Angle) Format(fs fmt.State, c rune)

func (*Angle) From

func (a *Angle) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Angle) Unit

func (a Angle) Unit() *Unit

Unit converts the Angle to a *Unit.

type Area

type Area float64

Area represents an area in square metres.

func (Area) Area

func (a Area) Area() Area

Area allows Area to implement a Areaer interface.

func (Area) Format

func (a Area) Format(fs fmt.State, c rune)

func (*Area) From

func (a *Area) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Area) Unit

func (a Area) Unit() *Unit

Unit converts the Area to a *Unit.

type Capacitance

type Capacitance float64

Capacitance represents an electrical capacitance in Farads.

const Farad Capacitance = 1

func (Capacitance) Capacitance

func (cp Capacitance) Capacitance() Capacitance

Capacitance allows Capacitance to implement a Capacitancer interface.

func (Capacitance) Format

func (cp Capacitance) Format(fs fmt.State, c rune)

func (*Capacitance) From

func (cp *Capacitance) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Capacitance) Unit

func (cp Capacitance) Unit() *Unit

Unit converts the Capacitance to a *Unit.

type Charge

type Charge float64

Charge represents an electric charge in Coulombs.

const Coulomb Charge = 1

func (Charge) Charge

func (ch Charge) Charge() Charge

Charge allows Charge to implement a Charger interface.

func (Charge) Format

func (ch Charge) Format(fs fmt.State, c rune)

func (*Charge) From

func (ch *Charge) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Charge) Unit

func (ch Charge) Unit() *Unit

Unit converts the Charge to a *Unit.

type Conductance

type Conductance float64

Conductance represents an electrical conductance in Siemens.

const Siemens Conductance = 1

func (Conductance) Conductance

func (co Conductance) Conductance() Conductance

Conductance allows Conductance to implement a Conductancer interface.

func (Conductance) Format

func (co Conductance) Format(fs fmt.State, c rune)

func (*Conductance) From

func (co *Conductance) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Conductance) Unit

func (co Conductance) Unit() *Unit

Unit converts the Conductance to a *Unit.

type Current

type Current float64

Current represents a current in Amperes.

const Ampere Current = 1

func (Current) Current

func (i Current) Current() Current

Current allows Current to implement a Currenter interface.

func (Current) Format

func (i Current) Format(fs fmt.State, c rune)

func (*Current) From

func (i *Current) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Current) Unit

func (i Current) Unit() *Unit

Unit converts the Current to a *Unit.

type Dimension

type Dimension int

Dimension is a type representing an SI base dimension or a distinct orthogonal dimension. Non-SI dimensions can be created using the NewDimension function, typically within an init function.

const (
	CurrentDim Dimension
	LengthDim
	LuminousIntensityDim
	MassDim
	MoleDim
	TemperatureDim
	TimeDim
	// Other common SI Dimensions
	AngleDim // e.g. radians
)

func NewDimension

func NewDimension(symbol string) Dimension

NewDimension creates a new orthogonal dimension with the given symbol, and returns the value of that dimension. The input symbol must not overlap with any of the any of the SI base units or other symbols of common use in SI ("kg", "J", etc.), and must not overlap with any other dimensions created by calls to NewDimension. The SymbolExists function can check if the symbol exists. NewDimension will panic if the input symbol matches an existing symbol.

NewDimension should only be called for unit types that are actually orthogonal to the base dimensions defined in this package. See the package-level documentation for further explanation.

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/unit"
)

func main() {
	// Create a "trees" dimension
	// Typically, this should be used within an init function
	treeDim := unit.NewDimension("tree")
	countPerArea := unit.New(0.1, unit.Dimensions{treeDim: 1, unit.LengthDim: -2})
	fmt.Println(countPerArea)

}
Output:

0.1 tree m^-2

func (Dimension) String

func (d Dimension) String() string

String returns the string for the dimension.

type Dimensions

type Dimensions map[Dimension]int

Dimensions represent the dimensionality of the unit in powers of that dimension. If a key is not present, the power of that dimension is zero. Dimensions is used in conjunction with New.

func (Dimensions) String

func (d Dimensions) String() string

type Dimless

type Dimless float64

Dimless represents a dimensionless constant.

func (Dimless) Dimless

func (d Dimless) Dimless() Dimless

Dimless allows Dimless to implement a Dimlesser interface.

func (Dimless) Format

func (d Dimless) Format(fs fmt.State, c rune)

func (*Dimless) From

func (d *Dimless) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Dimless) Unit

func (d Dimless) Unit() *Unit

Unit converts the Dimless to a *Unit.

type Energy

type Energy float64

Energy represents a quantity of energy in Joules.

const Joule Energy = 1

func (Energy) Energy

func (e Energy) Energy() Energy

Energy allows Energy to implement a Energyer interface.

func (Energy) Format

func (e Energy) Format(fs fmt.State, c rune)

func (*Energy) From

func (e *Energy) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Energy) Unit

func (e Energy) Unit() *Unit

Unit converts the Energy to a *Unit.

type EquivalentRadioactiveDose

type EquivalentRadioactiveDose float64

EquivalentRadioactiveDose is a measure of equivalent dose of ionizing radiation in sieverts.

const Sievert EquivalentRadioactiveDose = 1

func (EquivalentRadioactiveDose) EquivalentRadioactiveDose

func (a EquivalentRadioactiveDose) EquivalentRadioactiveDose() EquivalentRadioactiveDose

EquivalentRadioactiveDose allows EquivalentRadioactiveDose to implement a EquivalentRadioactiveDoseer interface.

func (EquivalentRadioactiveDose) Format

func (a EquivalentRadioactiveDose) Format(fs fmt.State, c rune)

func (*EquivalentRadioactiveDose) From

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (EquivalentRadioactiveDose) Unit

func (a EquivalentRadioactiveDose) Unit() *Unit

Unit converts the EquivalentRadioactiveDose to a *Unit.

type Force

type Force float64

Force represents a force in Newtons.

const Newton Force = 1

func (Force) Force

func (f Force) Force() Force

Force allows Force to implement a Forcer interface.

func (Force) Format

func (f Force) Format(fs fmt.State, c rune)

func (*Force) From

func (f *Force) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Force) Unit

func (f Force) Unit() *Unit

Unit converts the Force to a *Unit.

type Frequency

type Frequency float64

Frequency represents a frequency in Hertz.

const Hertz Frequency = 1

func (Frequency) Format

func (f Frequency) Format(fs fmt.State, c rune)

func (Frequency) Frequency

func (f Frequency) Frequency() Frequency

Frequency allows Frequency to implement a Frequencyer interface.

func (*Frequency) From

func (f *Frequency) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Frequency) Unit

func (f Frequency) Unit() *Unit

Unit converts the Frequency to a *Unit.

type Inductance

type Inductance float64

Inductance represents an electrical inductance in Henry.

const Henry Inductance = 1

func (Inductance) Format

func (i Inductance) Format(fs fmt.State, c rune)

func (*Inductance) From

func (i *Inductance) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Inductance) Inductance

func (i Inductance) Inductance() Inductance

Inductance allows Inductance to implement a Inductancer interface.

func (Inductance) Unit

func (i Inductance) Unit() *Unit

Unit converts the Inductance to a *Unit.

type Length

type Length float64

Length represents a length in metres.

const Metre Length = 1

func (Length) Format

func (l Length) Format(fs fmt.State, c rune)

func (*Length) From

func (l *Length) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Length) Length

func (l Length) Length() Length

Length allows Length to implement a Lengther interface.

func (Length) Unit

func (l Length) Unit() *Unit

Unit converts the Length to a *Unit.

type LuminousIntensity

type LuminousIntensity float64

Candela represents a luminous intensity in candela.

const Candela LuminousIntensity = 1

func (LuminousIntensity) Format

func (j LuminousIntensity) Format(fs fmt.State, c rune)

func (*LuminousIntensity) From

func (j *LuminousIntensity) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (LuminousIntensity) LuminousIntensity

func (j LuminousIntensity) LuminousIntensity() LuminousIntensity

LuminousIntensity allows LuminousIntensity to implement a LuminousIntensityer interface.

func (LuminousIntensity) Unit

func (j LuminousIntensity) Unit() *Unit

Unit converts the LuminousIntensity to a *Unit.

type MagneticFlux

type MagneticFlux float64

MagneticFlux represents a magnetic flux in Weber.

const Weber MagneticFlux = 1

func (MagneticFlux) Format

func (m MagneticFlux) Format(fs fmt.State, c rune)

func (*MagneticFlux) From

func (m *MagneticFlux) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (MagneticFlux) MagneticFlux

func (m MagneticFlux) MagneticFlux() MagneticFlux

MagneticFlux allows MagneticFlux to implement a MagneticFluxer interface.

func (MagneticFlux) Unit

func (m MagneticFlux) Unit() *Unit

Unit converts the MagneticFlux to a *Unit.

type MagneticFluxDensity

type MagneticFluxDensity float64

MagneticFluxDensity represents a magnetic flux density in Tesla.

const Tesla MagneticFluxDensity = 1

func (MagneticFluxDensity) Format

func (m MagneticFluxDensity) Format(fs fmt.State, c rune)

func (*MagneticFluxDensity) From

func (m *MagneticFluxDensity) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (MagneticFluxDensity) MagneticFluxDensity

func (m MagneticFluxDensity) MagneticFluxDensity() MagneticFluxDensity

MagneticFluxDensity allows MagneticFluxDensity to implement a MagneticFluxDensityer interface.

func (MagneticFluxDensity) Unit

func (m MagneticFluxDensity) Unit() *Unit

Unit converts the MagneticFluxDensity to a *Unit.

type Mass

type Mass float64

Mass represents a mass in kilograms.

const (
	Gram Mass = 1e-3

	Kilogram = Kilo * Gram
)

func (Mass) Format

func (m Mass) Format(fs fmt.State, c rune)

func (*Mass) From

func (m *Mass) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Mass) Mass

func (m Mass) Mass() Mass

Mass allows Mass to implement a Masser interface.

func (Mass) Unit

func (m Mass) Unit() *Unit

Unit converts the Mass to a *Unit.

type Mole

type Mole float64

Mole represents an amount in moles.

const Mol Mole = 1

func (Mole) Format

func (n Mole) Format(fs fmt.State, c rune)

func (*Mole) From

func (n *Mole) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Mole) Mole

func (n Mole) Mole() Mole

Mole allows Mole to implement a Moleer interface.

func (Mole) Unit

func (n Mole) Unit() *Unit

Unit converts the Mole to a *Unit.

type Power

type Power float64

Power represents a power in Watts.

const Watt Power = 1

func (Power) Format

func (pw Power) Format(fs fmt.State, c rune)

func (*Power) From

func (pw *Power) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Power) Power

func (pw Power) Power() Power

Power allows Power to implement a Powerer interface.

func (Power) Unit

func (pw Power) Unit() *Unit

Unit converts the Power to a *Unit.

type Pressure

type Pressure float64

Pressure represents a pressure in Pascals.

const Pascal Pressure = 1

func (Pressure) Format

func (pr Pressure) Format(fs fmt.State, c rune)

func (*Pressure) From

func (pr *Pressure) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Pressure) Pressure

func (pr Pressure) Pressure() Pressure

Pressure allows Pressure to implement a Pressurer interface.

func (Pressure) Unit

func (pr Pressure) Unit() *Unit

Unit converts the Pressure to a *Unit.

type Radioactivity

type Radioactivity float64

Radioactivity represents a rate of radioactive decay in becquerels.

const Becquerel Radioactivity = 1

func (Radioactivity) Format

func (r Radioactivity) Format(fs fmt.State, c rune)

func (*Radioactivity) From

func (r *Radioactivity) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Radioactivity) Radioactivity

func (r Radioactivity) Radioactivity() Radioactivity

Radioactivity allows Radioactivity to implement a Radioactivityer interface.

func (Radioactivity) Unit

func (r Radioactivity) Unit() *Unit

Unit converts the Radioactivity to a *Unit.

type Resistance

type Resistance float64

Resistance represents an electrical resistance, impedance or reactance in Ohms.

const Ohm Resistance = 1

func (Resistance) Format

func (r Resistance) Format(fs fmt.State, c rune)

func (*Resistance) From

func (r *Resistance) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Resistance) Resistance

func (r Resistance) Resistance() Resistance

Resistance allows Resistance to implement a Resistancer interface.

func (Resistance) Unit

func (r Resistance) Unit() *Unit

Unit converts the Resistance to a *Unit.

type Temperature

type Temperature float64

Temperature represents a temperature in Kelvin.

const Kelvin Temperature = 1

func (Temperature) Format

func (t Temperature) Format(fs fmt.State, c rune)

func (*Temperature) From

func (t *Temperature) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Temperature) Temperature

func (t Temperature) Temperature() Temperature

Temperature allows Temperature to implement a Temperaturer interface.

func (Temperature) Unit

func (t Temperature) Unit() *Unit

Unit converts the Temperature to a *Unit.

type Time

type Time float64

Time represents a duration in seconds.

func (Time) Format

func (t Time) Format(fs fmt.State, c rune)

func (*Time) From

func (t *Time) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Time) Time

func (t Time) Time() Time

Time allows Time to implement a Timer interface.

func (Time) Unit

func (t Time) Unit() *Unit

Unit converts the Time to a *Unit.

type Torque

type Torque float64

Torque represents a torque in Newton metres.

const Newtonmetre Torque = 1

func (Torque) Format

func (t Torque) Format(fs fmt.State, c rune)

func (*Torque) From

func (t *Torque) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Torque) Torque

func (t Torque) Torque() Torque

Torque allows Torque to implement a Torquer interface.

func (Torque) Unit

func (t Torque) Unit() *Unit

Unit converts the Torque to a *Unit.

type Unit

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

Unit represents a dimensional value. The dimensions will typically be in SI units, but can also include dimensions created with NewDimension. The Unit type is most useful for ensuring dimensional consistency when manipulating types with different units, for example, by multiplying an acceleration with a mass to get a force. See the package documentation for further explanation.

func New

func New(value float64, d Dimensions) *Unit

New creates a new variable of type Unit which has the value and dimensions specified by the inputs. The built-in dimensions are always in SI units (metres, kilograms, etc.).

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/unit"
)

func main() {
	// Create an angular acceleration of 3 rad/s^2
	accel := unit.New(3.0, unit.Dimensions{unit.AngleDim: 1, unit.TimeDim: -2})
	fmt.Println(accel)

}
Output:

3 rad s^-2

func (*Unit) Add

func (u *Unit) Add(uniter Uniter) *Unit

Add adds the function argument to the receiver. Panics if the units of the receiver and the argument don't match.

func (*Unit) Copy added in v0.9.0

func (u *Unit) Copy() *Unit

Copy returns a copy of the Unit that can be mutated without the change being reflected in the original value.

func (*Unit) Dimensions

func (u *Unit) Dimensions() Dimensions

Dimensions returns a copy of the dimensions of the unit.

func (*Unit) Div

func (u *Unit) Div(uniter Uniter) *Unit

Div divides the receiver by the argument changing the dimensions of the receiver as appropriate.

func (*Unit) Format

func (u *Unit) Format(fs fmt.State, c rune)

Format makes Unit satisfy the fmt.Formatter interface. The unit is formatted with dimensions appended. If the power of the dimension is not zero or one, symbol^power is appended, if the power is one, just the symbol is appended and if the power is zero, nothing is appended. Dimensions are appended in order by symbol name with positive powers ahead of negative powers.

func (*Unit) Mul

func (u *Unit) Mul(uniter Uniter) *Unit

Mul multiply the receiver by the input changing the dimensions of the receiver as appropriate. The input is not changed.

func (*Unit) SetValue

func (u *Unit) SetValue(v float64)

SetValue sets the value of the unit.

func (*Unit) Unit

func (u *Unit) Unit() *Unit

Unit implements the Uniter interface, returning the receiver. If a copy of the receiver is required, use the Copy method.

func (*Unit) Value

func (u *Unit) Value() float64

Value return the raw value of the unit as a float64. Use of this method is, in general, not recommended, though it can be useful for printing. Instead, the From method of a specific dimension should be used to guarantee dimension consistency.

type Uniter

type Uniter interface {
	Unit() *Unit
}

Uniter is a type that can be converted to a Unit.

type Velocity

type Velocity float64

Velocity represents a velocity in metres per second.

func (Velocity) Format

func (v Velocity) Format(fs fmt.State, c rune)

func (*Velocity) From

func (v *Velocity) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Velocity) Unit

func (v Velocity) Unit() *Unit

Unit converts the Velocity to a *Unit.

func (Velocity) Velocity

func (v Velocity) Velocity() Velocity

Velocity allows Velocity to implement a Velocityer interface.

type Voltage

type Voltage float64

Voltage represents a voltage in Volts.

const Volt Voltage = 1

func (Voltage) Format

func (v Voltage) Format(fs fmt.State, c rune)

func (*Voltage) From

func (v *Voltage) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Voltage) Unit

func (v Voltage) Unit() *Unit

Unit converts the Voltage to a *Unit.

func (Voltage) Voltage

func (v Voltage) Voltage() Voltage

Voltage allows Voltage to implement a Voltager interface.

type Volume

type Volume float64

Volume represents a volume in cubic metres.

const Litre Volume = 1e-3

func (Volume) Format

func (v Volume) Format(fs fmt.State, c rune)

func (*Volume) From

func (v *Volume) From(u Uniter) error

From converts the unit into the receiver. From returns an error if there is a mismatch in dimension.

func (Volume) Unit

func (v Volume) Unit() *Unit

Unit converts the Volume to a *Unit.

func (Volume) Volume

func (v Volume) Volume() Volume

Volume allows Volume to implement a Volumeer interface.

Directories

Path Synopsis
Package constant provides fundamental constants satisfying unit.Uniter.
Package constant provides fundamental constants satisfying unit.Uniter.

Jump to

Keyboard shortcuts

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