This repository has been archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
doc.go
96 lines (95 loc) · 4.3 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright ©2013 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This repository is no longer maintained.
// Development has moved to https://github.com/gonum/gonum.
//
// Package unit provides a set of types and constants that facilitate
// the use of the International System of Units (SI).
//
// Unit provides two main functionalities.
//
// 1)
// It provides a number of types representing either an SI base unit
// or a common combination of base units, named for the unit it
// represents (Length, Mass, Pressure, etc.). Each type has
// a float64 as the underlying unit, and its value represents the
// number of that underlying unit (Kilogram, Meter, Pascal, etc.).
// For example,
// height := 1.6 * unit.Meter
// acc := unit.Acceleration(9.8)
// creates a variable named 'height' with a value of 1.6 meters, and
// a variable named 'acc' with a value of 9.8 meters per second squared.
// These types can be used to add compile-time safety to code. For
// example,
// func UnitDensity(t unit.Temperature, pressure unit.Pressure) (unit.Density){
// ...
// }
// func main(){
// t := 300 * unit.Kelvin
// p := 5 * unit.Bar
// rho := UnitDensity(p, t) // gives 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 Float64Density(temperature, pressure float64) (float64){
// ...
// }
// func main(){
// t := 300.0 // degrees kelvin
// p := 50000.0 // Pascals
// rho := Float64Density(p, t) // no error
// }
// Many types have constants defined representing named SI units (Meter,
// Kilogram, etc. ) or SI derived units (Bar, Milligram, etc.). These are
// all defined as multiples of the base unit, so, for example, the following
// are euqivalent
// l := 0.001 * unit.Meter
// k := 1 * unit.Millimeter
// j := unit.Length(0.001)
//
// 2)
// Unit provides the type "Unit", meant to represent a general dimensional
// value. unit.Unit can be used to help prevent errors of dimensionality
// when multiplying or dividing dimensional numbers. This package also
// provides the "Uniter" interface which is satisfied by any type which can
// be converted to a unit. New varibles of type Unit can be created with
// the New function and the Dimensions map. For example, the code
// acc := New(9.81, Dimensions{LengthDim:1, TimeDim: -2})
// creates a variable "acc" which has a value of 9.81 m/s^2. Methods of
// unit can be used to modify this value, for example:
// acc.Mul(1.0 * unit.Kilogram).Mul(1 * unit.Meter)
// 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. NewDimension should not be
// used, however, to create the unit of 'Slide', because in this case slide
// is just a measurement of area. Instead, a constant could be defined.
// const Slide unit.Area = 0.001875 // m^2
// var WhiteBloodCellDim unit.Dimension
// func init(){
// WhiteBloodCellDim = unit.NewDimension("wbc")
// }
// type WbcPerArea float64
// func (w WbcPerArea) Unit() *Unit{
// return unit.New(float64(w), unit.Dimensions{WhiteBloodCellDim: 1, unit.LengthDim: -2})
// }
// func main(){
// v := WbcPerArea(15)
// fmt.Println(v.Unit()) // prints: 1.5e+01 m^-2 wbc
// }
// Please 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-meters 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.
package unit