| 1 | // Copyright ©2013 The Gonum Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:generate go run generate_unit.go |
| 6 | |
| 7 | // Package unit provides a set of types and constants that facilitate |
| 8 | // the use of the International System of Units (SI). |
| 9 | // |
| 10 | // The unit package provides two main functionalities: compile-time type-safe |
| 11 | // base SI units and common derived units; and a system for dynamically |
| 12 | // extensible user-defined units. |
| 13 | // |
| 14 | // # Static SI units |
| 15 | // |
| 16 | // This package provides a number of types representing either an SI base |
| 17 | // unit or a common combination of base units, named for the physical quantity |
| 18 | // it represents (Length, Mass, Pressure, etc.). Each type is defined from |
| 19 | // float64. The value of the float64 represents the quantity of that unit as |
| 20 | // expressed in SI base units (kilogram, metre, Pascal, etc.). For example, |
| 21 | // |
| 22 | // height := 1.6 * unit.Metre |
| 23 | // acc := unit.Acceleration(9.8) |
| 24 | // |
| 25 | // creates a variable named 'height' with a value of 1.6 metres, and |
| 26 | // a variable named 'acc' with a value of 9.8 metres per second squared. |
| 27 | // These types can be used to add compile-time safety to code. For |
| 28 | // example, |
| 29 | // |
| 30 | // func unitVolume(t unit.Temperature, p unit.Pressure) unit.Volume { |
| 31 | // ... |
| 32 | // } |
| 33 | // |
| 34 | // func main(){ |
| 35 | // t := 300 * unit.Kelvin |
| 36 | // p := 500 * unit.Kilo * unit.Pascal |
| 37 | // v := unitVolume(p, t) // compile-time error |
| 38 | // } |
| 39 | // |
| 40 | // gives a compile-time error (temperature type does not match pressure type) |
| 41 | // while the corresponding code using float64 runs without error. |
| 42 | // |
| 43 | // func float64Volume(temperature, pressure float64) float64 { |
| 44 | // ... |
| 45 | // } |
| 46 | // |
| 47 | // func main(){ |
| 48 | // t := 300.0 // Kelvin |
| 49 | // p := 500000.0 // Pascals |
| 50 | // v := float64Volume(p, t) // no error |
| 51 | // } |
| 52 | // |
| 53 | // Many types have constants defined representing named SI units (Metre, |
| 54 | // Kilogram, etc. ) or SI derived units (Pascal, Hz, etc.). The unit package |
| 55 | // additionally provides untyped constants for SI prefixes, so the following |
| 56 | // are all equivalent. |
| 57 | // |
| 58 | // l := 0.001 * unit.Metre |
| 59 | // k := 1 * unit.Milli * unit.Metre |
| 60 | // j := unit.Length(0.001) |
| 61 | // |
| 62 | // Additional SI-derived static units can also be defined by adding types that |
| 63 | // satisfy the Uniter interface described below. |
| 64 | // |
| 65 | // # Dynamic user-extensible unit system |
| 66 | // |
| 67 | // The unit package also provides the Unit type, a representation of a general |
| 68 | // dimensional value. Unit can be used to help prevent errors of dimensionality |
| 69 | // when multiplying or dividing dimensional numbers defined at run time. New |
| 70 | // variables of type Unit can be created with the New function and the |
| 71 | // Dimensions map. For example, the code |
| 72 | // |
| 73 | // rate := unit.New(1 * unit.Milli, Dimensions{MoleDim: 1, TimeDim: -1}) |
| 74 | // |
| 75 | // creates a variable "rate" which has a value of 1e-3 mol/s. Methods of |
| 76 | // unit can be used to modify this value, for example: |
| 77 | // |
| 78 | // rate.Mul(1 * unit.Centi * unit.Metre).Div(1 * unit.Milli * unit.Volt) |
| 79 | // |
| 80 | // To convert the unit back into a typed float64 value, the From methods |
| 81 | // of the dimensional types should be used. From will return an error if the |
| 82 | // dimensions do not match. |
| 83 | // |
| 84 | // var energy unit.Energy |
| 85 | // err := energy.From(acc) |
| 86 | // |
| 87 | // Domain-specific problems may need custom dimensions, and for this purpose |
| 88 | // NewDimension should be used to help avoid accidental overlap between |
| 89 | // packages. For example, results from a blood test may be measured in |
| 90 | // "White blood cells per slide". In this case, NewDimension should be |
| 91 | // used to create a 'WhiteBloodCell' dimension. NewDimension takes in a |
| 92 | // string which will be used for printing that dimension, and will return |
| 93 | // a unique dimension number. |
| 94 | // |
| 95 | // wbc := unit.NewDimension("WhiteBloodCell") |
| 96 | // |
| 97 | // NewDimension should not be used, however, to create the unit of 'Slide', |
| 98 | // because in this case slide is just a measurement of liquid volume. Instead, |
| 99 | // a constant could be defined. |
| 100 | // |
| 101 | // const Slide unit.Volume = 0.1 * unit.Micro * unit.Litre |
| 102 | // |
| 103 | // Note that unit cannot catch all errors related to dimensionality. |
| 104 | // Different physical ideas are sometimes expressed with the same dimensions |
| 105 | // and unit is incapable of catching these mismatches. For example, energy and |
| 106 | // torque are both expressed as force times distance (Newton-metres in SI), |
| 107 | // but it is wrong to say that a torque of 10 N·m is the same as 10 J, even |
| 108 | // though the dimensions agree. Despite this, using the defined types to |
| 109 | // represent units can help to catch errors at compile-time. For example, |
| 110 | // using unit.Torque allows you to define a statically typed function like so |
| 111 | // |
| 112 | // func LeverLength(apply unit.Force, want unit.Torque) unit.Length { |
| 113 | // return unit.Length(float64(want)/float64(apply)) |
| 114 | // } |
| 115 | // |
| 116 | // This will prevent an energy value being provided to LeverLength in place |
| 117 | // of a torque value. |
| 118 | package unit // import "gonum.org/v1/gonum/unit" |