Skip to content

Commit

Permalink
physic: move constants close to the types
Browse files Browse the repository at this point in the history
This changes the godoc output to create sections, which is easier to read. In
practice what I'd like is to have the constants close to each type in godoc but
this can't be done.

No functional change.
  • Loading branch information
maruel committed Jun 26, 2018
1 parent 3e5b764 commit de66911
Showing 1 changed file with 145 additions and 115 deletions.
260 changes: 145 additions & 115 deletions conn/physic/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ import (
"time"
)

// Common constants.
// Distance is a measurement of length stored as an int64 nano metre.
//
// This is one of the base unit in the International System of Units.
//
// The highest representable value is 9.2Gm.
type Distance int64

// String returns the distance formatted as a string in metre.
func (d Distance) String() string {
return nanoAsString(int64(d)) + "m"
}

// Distance constants.
const (
NanoMetre Distance = 1
MicroMetre = 1000 * NanoMetre
Expand All @@ -24,122 +36,8 @@ const (
Foot = 12 * Inch
Yard = 3 * Foot
Mile = 1760 * Yard

NanoAmpere ElectricCurrent = 1
MicroAmpere = 1000 * NanoAmpere
MilliAmpere = 1000 * MicroAmpere
Ampere = 1000 * MilliAmpere

// Volt is W/A, kg⋅m²/s³/A
NanoVolt ElectricPotential = 1
MicroVolt = 1000 * NanoVolt
MilliVolt = 1000 * MicroVolt
Volt = 1000 * MilliVolt
KiloVolt = 1000 * Volt

// Ohm is V/A, kg⋅m²/s³/A².
NanoOhm ElectricResistance = 1
MicroOhm = 1000 * NanoOhm
MilliOhm = 1000 * MicroOhm
Ohm = 1000 * MilliOhm
KiloOhm = 1000 * Ohm
MegaOhm = 1000 * KiloOhm

// Newton is kg⋅m/s²
NanoNewton Force = 1
MicroNewton = 1000 * NanoNewton
MilliNewton = 1000 * MicroNewton
Newton = 1000 * MilliNewton
KiloNewton = 1000 * Newton
MegaNewton = 1000 * KiloNewton

EarthGravity = 9806650 * MicroNewton

// Conversion between Newton and imperial units.
// Pound is both a unit of mass and weight (force). The suffix Mass is added
// to disambiguate the measurement it represents.
PoundForce = 4448221615261 * NanoNewton

// Hertz is 1/s.
MicroHertz Frequency = 1
MilliHertz = 1000 * MicroHertz
Hertz = 1000 * MilliHertz
KiloHertz = 1000 * Hertz
MegaHertz = 1000 * KiloHertz
GigaHertz = 1000 * MegaHertz

NanoGram Mass = 1
MicroGram = 1000 * NanoGram
MilliGram = 1000 * MicroGram
Gram = 1000 * MilliGram
KiloGram = 1000 * Gram
MegaGram = 1000 * KiloGram
Tonne = MegaGram

// Conversion between Gram and imperial units.
// Ounce is both a unit of mass, weight (force) or volume depending on
// context. The suffix Mass is added to disambiguate the measurement it
// represents.
OunceMass = 28349523125 * NanoGram
// Pound is both a unit of mass and weight (force). The suffix Mass is added
// to disambiguate the measurement it represents.
PoundMass = 16 * OunceMass
Slug = 14593903 * MilliGram

// Pascal is N/m², kg/m/s².
NanoPascal Pressure = 1
MicroPascal = 1000 * NanoPascal
MilliPascal = 1000 * MicroPascal
Pascal = 1000 * MilliPascal
KiloPascal = 1000 * Pascal

TenthMicroRH RelativeHumidity = 1 // 0.00001%rH
MicroRH = 10 * TenthMicroRH // 0.0001%rH
MilliRH = 1000 * MicroRH // 0.1%rH
PercentRH = 10 * MilliRH // 1%rH

NanoKelvin Temperature = 1
MicroKelvin = 1000 * NanoKelvin
MilliKelvin = 1000 * MicroKelvin
Kelvin = 1000 * MilliKelvin

// Conversion between Kelvin and Celsius.
ZeroCelsius = 273150 * MilliKelvin
MilliCelsius = MilliKelvin
Celsius = Kelvin

// Conversion between Kelvin and Fahrenheit.
ZeroFahrenheit = 255372 * MilliKelvin
MilliFahrenheit = 555555 * NanoKelvin
Fahrenheit = 555555555 * NanoKelvin

// MetrePerSecond is m/s.
NanoMetrePerSecond Speed = 1
MicroMetrePerSecond = 1000 * NanoMetrePerSecond
MilliMetrePerSecond = 1000 * MicroMetrePerSecond
MetrePerSecond = 1000 * MilliMetrePerSecond
KiloMetrePerSecond = 1000 * MetrePerSecond
MegaMetrePerSecond = 1000 * KiloMetrePerSecond

LightSpeed = 299792458 * MetrePerSecond

KilometrePerHour = 3600 * MilliMetrePerSecond
MilePerHour = 447040 * MicroMetrePerSecond
FootPerSecond = 304800 * MicroMetrePerSecond
)

// Distance is a measurement of length stored as an int64 nano metre.
//
// This is one of the base unit in the International System of Units.
//
// The highest representable value is 9.2Gm.
type Distance int64

// String returns the distance formatted as a string in metre.
func (d Distance) String() string {
return nanoAsString(int64(d)) + "m"
}

// ElectricCurrent is a measurement of a flow of electric charge stored as an
// int64 nano Ampere.
//
Expand All @@ -153,6 +51,14 @@ func (e ElectricCurrent) String() string {
return nanoAsString(int64(e)) + "A"
}

// ElectricCurrent constants.
const (
NanoAmpere ElectricCurrent = 1
MicroAmpere = 1000 * NanoAmpere
MilliAmpere = 1000 * MicroAmpere
Ampere = 1000 * MilliAmpere
)

// ElectricPotential is a measurement of electric potential stored as an int64
// nano Volt.
//
Expand All @@ -164,6 +70,16 @@ func (e ElectricPotential) String() string {
return nanoAsString(int64(e)) + "V"
}

// ElectricPotential constants.
const (
// Volt is W/A, kg⋅m²/s³/A.
NanoVolt ElectricPotential = 1
MicroVolt = 1000 * NanoVolt
MilliVolt = 1000 * MicroVolt
Volt = 1000 * MilliVolt
KiloVolt = 1000 * Volt
)

// ElectricResistance is a measurement of the difficulty to pass an electric
// current through a conductor stored as an int64 nano Ohm.
//
Expand All @@ -175,6 +91,17 @@ func (e ElectricResistance) String() string {
return nanoAsString(int64(e)) + "Ω"
}

// ElectricResistance constants.
const (
// Ohm is V/A, kg⋅m²/s³/A².
NanoOhm ElectricResistance = 1
MicroOhm = 1000 * NanoOhm
MilliOhm = 1000 * MicroOhm
Ohm = 1000 * MilliOhm
KiloOhm = 1000 * Ohm
MegaOhm = 1000 * KiloOhm
)

// Force is a measurement of interaction that will change the motion of an
// object stored as an int64 nano Newton.
//
Expand All @@ -190,6 +117,24 @@ func (f Force) String() string {
return nanoAsString(int64(f)) + "N"
}

// Force constants.
const (
// Newton is kg⋅m/s².
NanoNewton Force = 1
MicroNewton = 1000 * NanoNewton
MilliNewton = 1000 * MicroNewton
Newton = 1000 * MilliNewton
KiloNewton = 1000 * Newton
MegaNewton = 1000 * KiloNewton

EarthGravity = 9806650 * MicroNewton

// Conversion between Newton and imperial units.
// Pound is both a unit of mass and weight (force). The suffix Mass is added
// to disambiguate the measurement it represents.
PoundForce = 4448221615261 * NanoNewton
)

// Frequency is a measurement of cycle per second, stored as an int32 micro
// Hertz.
//
Expand All @@ -211,6 +156,17 @@ func PeriodToFrequency(t time.Duration) Frequency {
return Frequency(time.Second) * Hertz / Frequency(t)
}

// Frequency constants.
const (
// Hertz is 1/s.
MicroHertz Frequency = 1
MilliHertz = 1000 * MicroHertz
Hertz = 1000 * MilliHertz
KiloHertz = 1000 * Hertz
MegaHertz = 1000 * KiloHertz
GigaHertz = 1000 * MegaHertz
)

// Mass is a measurement of mass stored as an int64 nano gram.
//
// This is one of the base unit in the International System of Units.
Expand All @@ -223,6 +179,27 @@ func (m Mass) String() string {
return nanoAsString(int64(m)) + "g"
}

// Mass constants.
const (
NanoGram Mass = 1
MicroGram = 1000 * NanoGram
MilliGram = 1000 * MicroGram
Gram = 1000 * MilliGram
KiloGram = 1000 * Gram
MegaGram = 1000 * KiloGram
Tonne = MegaGram

// Conversion between Gram and imperial units.
// Ounce is both a unit of mass, weight (force) or volume depending on
// context. The suffix Mass is added to disambiguate the measurement it
// represents.
OunceMass = 28349523125 * NanoGram
// Pound is both a unit of mass and weight (force). The suffix Mass is added
// to disambiguate the measurement it represents.
PoundMass = 16 * OunceMass
Slug = 14593903 * MilliGram
)

// Pressure is a measurement of force applied to a surface per unit
// area (stress) stored as an int64 nano Pascal.
//
Expand All @@ -234,6 +211,16 @@ func (p Pressure) String() string {
return nanoAsString(int64(p)) + "Pa"
}

// Pressure constants.
const (
// Pascal is N/m², kg/m/s².
NanoPascal Pressure = 1
MicroPascal = 1000 * NanoPascal
MilliPascal = 1000 * MicroPascal
Pascal = 1000 * MilliPascal
KiloPascal = 1000 * Pascal
)

// RelativeHumidity is a humidity level measurement stored as an int32 fixed
// point integer at a precision of 0.00001%rH.
//
Expand All @@ -253,6 +240,14 @@ func (r RelativeHumidity) String() string {
return strconv.Itoa(int(r)/10) + "." + strconv.Itoa(frac) + "%rH"
}

// RelativeHumidity constants.
const (
TenthMicroRH RelativeHumidity = 1 // 0.00001%rH
MicroRH = 10 * TenthMicroRH // 0.0001%rH
MilliRH = 1000 * MicroRH // 0.1%rH
PercentRH = 10 * MilliRH // 1%rH
)

// Speed is a measurement of magnitude of velocity stored as an int64 nano
// Metre per Second.
//
Expand All @@ -264,6 +259,23 @@ func (s Speed) String() string {
return nanoAsString(int64(s)) + "m/s"
}

// Speed constants.
const (
// MetrePerSecond is m/s.
NanoMetrePerSecond Speed = 1
MicroMetrePerSecond = 1000 * NanoMetrePerSecond
MilliMetrePerSecond = 1000 * MicroMetrePerSecond
MetrePerSecond = 1000 * MilliMetrePerSecond
KiloMetrePerSecond = 1000 * MetrePerSecond
MegaMetrePerSecond = 1000 * KiloMetrePerSecond

LightSpeed = 299792458 * MetrePerSecond

KilometrePerHour = 3600 * MilliMetrePerSecond
MilePerHour = 447040 * MicroMetrePerSecond
FootPerSecond = 304800 * MicroMetrePerSecond
)

// Temperature is a measurement of hotness stored as a nano kelvin.
//
// Negative values are invalid.
Expand All @@ -276,6 +288,24 @@ func (t Temperature) String() string {
return nanoAsString(int64(t-ZeroCelsius)) + "°C"
}

// Temperature constants.
const (
NanoKelvin Temperature = 1
MicroKelvin = 1000 * NanoKelvin
MilliKelvin = 1000 * MicroKelvin
Kelvin = 1000 * MilliKelvin

// Conversion between Kelvin and Celsius.
ZeroCelsius = 273150 * MilliKelvin
MilliCelsius = MilliKelvin
Celsius = Kelvin

// Conversion between Kelvin and Fahrenheit.
ZeroFahrenheit = 255372 * MilliKelvin
MilliFahrenheit = 555555 * NanoKelvin
Fahrenheit = 555555555 * NanoKelvin
)

//

func prefixZeros(digits, v int) string {
Expand Down

0 comments on commit de66911

Please sign in to comment.