si

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2018 License: MIT Imports: 3 Imported by: 0

README

Système international (SI)

GoDoc License Go Report Card

Working with sensors requires some extra thought into how you report measurements. The need for this library arised when several sensors where reporting different units which was confused or hard to distinguish in the backend. E.g. one sensor reporting km/h and another m/s.

Installation

Windows, OS X & Linux:

go get github.com/gurre/si

Usage examples

Simple usage:

// Our sensor reports temperature in millidegrees celcius
mc := si.NewQuantity(si.Milli, si.Temperature)

// Make sure we never forget what unit we are measuring in
temp := si.NewUnit(24062, mc)

fmt.Println(temp)
// 24062 m°C

Combining several quantities:

// NewQuantity takes a prefix and a measure
km := si.NewQuantity(si.Kilo, si.Length)

// Hour is not a SI unit but officially accepted
h := si.NewQuantity(si.Hour, si.Time)

// Combine a value with several SI-units
kmh := si.NewUnit(100, km, h)

parsed := si.Parse(kmh.String())

fmt.Println(kmh, parsed)
// 100.0 km/h 100.0 km/h

More reading

Documentation

Index

Constants

View Source
const (
	// Base is 1 or 1E0
	Base = iota
	// Yotta is 1E24
	Yotta
	// Zetta is 1E21
	Zetta
	// Exa is 1E18
	Exa
	// Peta is 1E15
	Peta
	// Tera is 1E12
	Tera
	// Giga is 1E9
	Giga
	// Mega is 1E6
	Mega
	// Kilo is 1E3
	Kilo
	// Hecto is 1E2
	Hecto
	// Deca is 1E1
	Deca
	// Deci is 1E-1
	Deci
	// Centi is 1E-2
	Centi
	// Milli is 1E-3
	Milli
	// Micro is 1E-6
	Micro
	// Nano is 1E-9
	Nano
	// Pico is 1E-12
	Pico
	// Femto is 1E-15
	Femto
	// Atto is 1E-18
	Atto
	// Zepto is 1E-21
	Zepto
	// Yocto is 1E-24
	Yocto
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Measure

type Measure int
const (
	// None is the empty measure
	None Measure = iota
	// Length is meters
	Length
	// Mass is grams
	Mass
	Time
	ElectricCurrent
	ThermodynamicTemperature
	AmountOfSubstance
	LuminousIntensity
	Angle
	SolidAngle
	Frequency
	Force // or weight
	Pressure
	Energy         // or work, heat
	Power          // or radiant flux
	ElectricCharge // or quantity of electricity
	Voltage        // (electrical potential), emf
	Capacitance
	Impedance // resistance, reactance
	ElectricalConductance
	MagneticFlux
	MagneticFluxDensity
	Inductance
	Temperature // temperature relative to 273.15 K
	LuminousFlux
	Illuminance
	Radioactivity // decays per unit time
	AbsorbedDose
	EquivalentDose
	CatalyticActivity
)

func Parse

func Parse(str string) (measure Measure, err error)

Parse takes a string generated from String() and converts it back to a unit.

func (Measure) Dimension

func (measure Measure) Dimension() string

Dimension return the symbol used in dimensional analysis.

func (Measure) String

func (measure Measure) String() string

String returns the Système international unit symbol

type Prefix

type Prefix int

func (Prefix) Factor

func (prefix Prefix) Factor() (*big.Float, error)

Factor returns the multiplication factor for a prefix. When prefixes are used the SI units are no longer coherent which means you need to do convertions.

func (Prefix) String

func (prefix Prefix) String() string

type Quantity

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

func NewQuantity

func NewQuantity(prefix Prefix, measure Measure) (*Quantity, error)

type Unit

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

Unit is complete according to Système international

func NewUnit

func NewUnit(val float64, quantities ...Quantity) (*Unit, error)

NewUnit returns a complete SI-unit

func (*Unit) BigFloat

func (u *Unit) BigFloat() (unit *big.Float)

BigInt returns the SI unit as a big.Int-number

func (Unit) MarshalJSON

func (u Unit) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface

func (*Unit) UnmarshalJSON

func (u *Unit) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the Unmarshaler interface

func (*Unit) ValueUnit

func (u *Unit) ValueUnit() (float64, *Unit)

ValueUnit returns the underlying value and unit, useful when you don't want a string

Jump to

Keyboard shortcuts

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