unit

package module
v1.11.9 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 8 Imported by: 0

README

Unit Go

Go SDK for modeling physical units and conversion between them.

Installation

go get go.einride.tech/unit

Example

angle := 3 * unit.Radian
fmt.Println(angle.Degrees())
// Output: 171.88733853924697

Documentation

Overview

Package unit provides primitives for modeling and converting between with physical units.

Index

Constants

View Source
const (
	Kilo  = 1e3  // k
	Centi = 1e-2 // c
	Milli = 1e-3 // m
	Micro = 1e-6 // µ
	Nano  = 1e-9 // n
)

SI prefixes.

View Source
const Degree = Radian / 180 * math.Pi

Degree is a measurement of a plane angle in which one full rotation is 360 degrees.

View Source
const DegreePerSecond = RadianPerSecond / 180 * math.Pi

DegreePerSecond is angular speed measured in degrees per second.

View Source
const Kilogram = Kilo * Gram

Kilogram is the SI unit for measuring Mass.

View Source
const KilometerPerHour = MeterPerSecond / 3.6

KilometerPerHour is a unit for measuring speed using Kilometer for distance and Hour for time.

View Source
const Mile = 1 / 0.621371192 * Kilo * Meter

Mile is a British imperial unit and US customary unit for measuring distance.

View Source
const RPM = RadianPerSecond * (2 * math.Pi) / 60

RPM is the number of revolutions in one minute.

View Source
const Tonne = 1e6 * Gram

Tonne is a thousand of the SI unit for measuring mass, the Kilogram.

Variables

This section is empty.

Functions

func GetFloatingPointCmpOpts added in v1.6.1

func GetFloatingPointCmpOpts() []cmp.Option

GetFloatingPointCmpOpts compares float unit types with floating point error tolerance. The function is useful when testing with conversions from unit types to/from float32 where precision is lost.

Example usage: ``` import "gotest.tools/v3/assert" [...] assert.DeepEqual(t, source, truncated, GetFloatingPointCmpOpts()...) ```.

Types

type Acceleration

type Acceleration float64

Acceleration is the rate of change of the velocity of an object with respect to time.

const MeterPerSecondSquared Acceleration = 1

MeterPerSecondSquared is SI unit for measuring Acceleration.

func (Acceleration) Get

func (a Acceleration) Get(as Acceleration) float64

Get returns a with the unit of as.

func (Acceleration) MetersPerSecondSquared

func (a Acceleration) MetersPerSecondSquared() float64

MetersPerSecondSquared returns a with the unit of m/s².

func (Acceleration) String

func (a Acceleration) String() string

String implements fmt.Stringer.

func (*Acceleration) UnmarshalString

func (a *Acceleration) UnmarshalString(s string) error

UnmarshalString sets *a from s.

func (*Acceleration) UnmarshalText

func (a *Acceleration) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Angle

type Angle float64

Angle is the measure of a plane angle or rotation.

const Radian Angle = 1.0

Radian is the SI unit for measuring an Angle.

func FromDegrees added in v1.10.0

func FromDegrees(a float64) Angle

FromDegrees returns an Angle from degrees as float64.

func FromRadians added in v1.4.1

func FromRadians(a float64) Angle

FromRadians returns an Angle from radians as float64.

func (Angle) Degrees added in v1.4.1

func (a Angle) Degrees() float64

Degrees returns the angle with the unit of degrees.

func (Angle) Get

func (a Angle) Get(as Angle) float64

Get returns a with the unit of as.

func (*Angle) MarshalJSON added in v1.11.0

func (a *Angle) MarshalJSON() ([]byte, error)

func (Angle) Radians

func (a Angle) Radians() float64

Radians returns the angle with the unit of radians.

func (Angle) String

func (a Angle) String() string

String implements fmt.Stringer.

func (*Angle) UnmarshalJSON added in v1.11.0

func (a *Angle) UnmarshalJSON(data []byte) error

UnmarshalJSON implements JSON unmarshalling for the Angle type. The type is represented as radians in JSON.

func (*Angle) UnmarshalString

func (a *Angle) UnmarshalString(s string) error

UnmarshalString sets *a from s.

func (*Angle) UnmarshalText

func (a *Angle) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Angle) WrapMinusPiPi

func (a Angle) WrapMinusPiPi() Angle

WrapMinusPiPi wraps the current angle in the interval [-pi, pi[.

func (Angle) WrapZeroTwoPi added in v1.10.0

func (a Angle) WrapZeroTwoPi() Angle

WrapZeroTwoPi wraps the current angle in the interval [0, 2*pi[.

type AngularSpeed

type AngularSpeed float64

AngularSpeed is a measure of rotation rate.

const RadianPerSecond AngularSpeed = 1.0

RadianPerSecond is the SI unit for measuring AngularSpeed.

func (AngularSpeed) DegreesPerSecond added in v1.9.0

func (a AngularSpeed) DegreesPerSecond() float64

DegreesPerSecond returns a with the unit of °/s.

func (AngularSpeed) Get

func (a AngularSpeed) Get(as AngularSpeed) float64

Get returns a with the unit of as.

func (AngularSpeed) RadiansPerSecond

func (a AngularSpeed) RadiansPerSecond() float64

RadiansPerSecond returns a with the unit of rad/s.

func (AngularSpeed) String

func (a AngularSpeed) String() string

String implements fmt.Stringer.

func (*AngularSpeed) UnmarshalString

func (a *AngularSpeed) UnmarshalString(s string) error

UnmarshalString sets *a from s.

func (*AngularSpeed) UnmarshalText

func (a *AngularSpeed) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Curvature added in v1.5.1

type Curvature float64

Curvature is a numerical measurement of inverse physical length.

const PerMeter Curvature = 1

PerMeter is the SI unit for measuring Curvature.

func (Curvature) Get added in v1.5.1

func (c Curvature) Get(as Curvature) float64

Get returns c with the unit of as.

func (Curvature) PerMeters added in v1.5.1

func (c Curvature) PerMeters() float64

PerMeters returns c with the unit of 1/m.

func (Curvature) String added in v1.5.1

func (c Curvature) String() string

String implements fmt.Stringer.

func (*Curvature) UnmarshalString added in v1.5.1

func (c *Curvature) UnmarshalString(s string) error

UnmarshalString sets *c from s.

func (*Curvature) UnmarshalText added in v1.5.1

func (c *Curvature) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Distance

type Distance float64

Distance is a numerical measurement of physical length.

const Meter Distance = 1

Meter is the SI unit for measuring Distance.

func (Distance) Get

func (d Distance) Get(as Distance) float64

Get returns d with the unit of as.

func (Distance) Meters

func (d Distance) Meters() float64

Meters returns d with the unit of m.

func (Distance) String

func (d Distance) String() string

String implements fmt.Stringer.

func (*Distance) UnmarshalString

func (d *Distance) UnmarshalString(s string) error

UnmarshalString sets *d from s.

func (*Distance) UnmarshalText

func (d *Distance) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Force

type Force float64

Force is an influence that can change the motion of an object.

const Newton Force = 1.0

Newton is the SI unit for measuring Force.

func (Force) Get

func (f Force) Get(as Force) float64

Get returns f with the unit of as.

func (Force) Newtons

func (f Force) Newtons() float64

Newtons returns f with the unit of N.

func (Force) String

func (f Force) String() string

String implements fmt.Stringer.

func (*Force) UnmarshalString

func (f *Force) UnmarshalString(s string) error

UnmarshalString sets *f from s.

func (*Force) UnmarshalText

func (f *Force) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Frequency

type Frequency float64

Frequency is the number of occurrences of a repeating event per unit of time.

const Hertz Frequency = 1.0

Hertz is the SI unit for measuring Frequency.

func (Frequency) Get

func (f Frequency) Get(as Frequency) float64

Get returns f with the unit of as.

func (Frequency) Hertz

func (f Frequency) Hertz() float64

Hertz returns f with the unit of Hz.

func (Frequency) String

func (f Frequency) String() string

String implements fmt.Stringer.

func (*Frequency) UnmarshalString

func (f *Frequency) UnmarshalString(s string) error

UnmarshalString sets *f from s.

func (*Frequency) UnmarshalText

func (f *Frequency) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Jerk

type Jerk float64

Jerk is the rate at which an object's acceleration changes with respect to time.

const MeterPerSecondCubed Jerk = 1.0

MeterPerSecondCubed is the SI unit for measuring Jerk.

func (Jerk) Get

func (j Jerk) Get(as Jerk) float64

Get returns j with the unit of as.

func (Jerk) MetersPerSecondCubed

func (j Jerk) MetersPerSecondCubed() float64

MetersPerSecondCubed returns j with the unit of m/s³.

func (Jerk) String

func (j Jerk) String() string

String implements fmt.Stringer.

func (*Jerk) UnmarshalString

func (j *Jerk) UnmarshalString(s string) error

UnmarshalString sets *j from s.

func (*Jerk) UnmarshalText

func (j *Jerk) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Mass

type Mass float64

Mass is the quantity of matter in a physical body.

const Gram Mass = 1.0

Gram is one thousandth of the SI unit for measuring mass, the Kilogram.

func (Mass) Get

func (m Mass) Get(as Mass) float64

Get returns m with the unit of as.

func (Mass) Kilograms

func (m Mass) Kilograms() float64

Kilograms returns m with the unit of kg.

func (Mass) String

func (m Mass) String() string

String implements fmt.Stringer.

func (*Mass) UnmarshalString

func (m *Mass) UnmarshalString(s string) error

UnmarshalString sets *m from s.

func (*Mass) UnmarshalText

func (m *Mass) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Pressure added in v1.8.0

type Pressure float64

Pressure is the force applied perpendicular to the surface of an object per unit area over which that force is distributed.

const Bar Pressure = 1e5 * Pascal

Bar is 100'000 times the SI unit Pascal.

const KiloPascal Pressure = 1000 * Pascal

KiloPascal is 1000 times the SI unit Pascal.

const Pascal Pressure = 1

Pascal is the SI unit for measuring Pressure.

func (Pressure) Bar added in v1.8.0

func (p Pressure) Bar() float64

Bar returns p with the unit of 1 bar.

func (Pressure) Get added in v1.8.0

func (p Pressure) Get(as Pressure) float64

Get returns p with the unit of as.

func (Pressure) KiloPascal added in v1.8.0

func (p Pressure) KiloPascal() float64

KiloPascal returns p with the unit of kPa.

func (Pressure) String added in v1.8.0

func (p Pressure) String() string

String implements fmt.Stringer.

func (*Pressure) UnmarshalString added in v1.8.0

func (p *Pressure) UnmarshalString(s string) error

UnmarshalString sets *p from s.

func (*Pressure) UnmarshalText added in v1.8.0

func (p *Pressure) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Sharpness added in v1.7.3

type Sharpness float64

Sharpness is a numerical measurement of angular change per physical length. And example is the sharpness of a curve.

const RadianPerMeter Sharpness = 1

RadianPerMeter is the SI unit for measuring Sharpness.

func (Sharpness) Get added in v1.7.3

func (s Sharpness) Get(as Sharpness) float64

Get returns the sharpness with the unit of as.

func (Sharpness) RadianPerMeter added in v1.7.3

func (s Sharpness) RadianPerMeter() float64

RadianPerMeter returns sharpness as a float64 with the unit of rad/m.

func (Sharpness) String added in v1.7.3

func (s Sharpness) String() string

String implements fmt.Stringer.

func (*Sharpness) UnmarshalString added in v1.7.3

func (s *Sharpness) UnmarshalString(str string) error

UnmarshalString sets *c from s.

func (*Sharpness) UnmarshalText added in v1.7.3

func (s *Sharpness) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Speed

type Speed float64

Speed is the rate of change of an object of its position with time.

const MeterPerSecond Speed = 1.0

MeterPerSecond is the SI unit for measuring Speed.

func (Speed) Get

func (s Speed) Get(as Speed) float64

Get returns s with the unit of as.

func (Speed) MetersPerSecond

func (s Speed) MetersPerSecond() float64

MetersPerSecond returns s with the unit of m/s.

func (Speed) String

func (s Speed) String() string

String implements fmt.Stringer.

func (*Speed) UnmarshalString

func (s *Speed) UnmarshalString(str string) error

UnmarshalString sets *s from str.

func (*Speed) UnmarshalText

func (s *Speed) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Torque

type Torque float64

Torque is the rotational equivalent of linear Force.

const NewtonMeter Torque = 1.0

NewtonMeter is the SI unit for measuring Torque.

func (Torque) Get

func (t Torque) Get(as Torque) float64

Get returns t with the unit of as.

func (Torque) NewtonMeters

func (t Torque) NewtonMeters() float64

NewtonMeters returns t with the unit of Nm.

func (Torque) String

func (t Torque) String() string

String implements fmt.Stringer.

func (*Torque) UnmarshalString

func (t *Torque) UnmarshalString(s string) error

UnmarshalString sets *t from s.

func (*Torque) UnmarshalText

func (t *Torque) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

Jump to

Keyboard shortcuts

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