quantities.base

This module defines the base types for unit and quantity handling.

Each quantity can be represented as the product of a number and a set of dimensions, and the struct Quantity has this role. The number is stored internally as a member of type N, which is enforced to be a built-in numeric type (isNumeric!N is true). The dimensions are stored as template parameter list (Dim) in the form of a sequence of string symbols and integral powers. Dimensionless quantities have an empty Dim. For instance length and speed quantities can be stored as:

alias Length = Quantity!(double, "L", 1);
alias Speed  = Quantity!(double, "L", 1, "T", -1);

where "L" is the symbol for the length dimension, "T" is the symbol of the time dimensions, and 1 and -1 are the powers of those dimensions in the representation of the quantity.

The main quantities compliant with the international system of units (SI) are predefined in the module quantities.si. In the same way, units are just instances of a Quantity struct where the number is 1 and the dimensions only contain one symbol, with the power 1. For instance, the meter unit is predefined as something equivalent to:

enum meter = Quantity!(double, "L", 1)(1.0);

(note that the constructor used here has the package access protection: new units should be defined with the unit template of this module).

Any quantity can be expressed as the product of a number (n) and a unit of the right dimensions (U). For instance:

auto size = 9.5 * meter;
auto time = 120 * milli(second);

The unit U is not actually stored along with the number in a Quantity struct, only the dimensions are. This is because the same quantity can be expressed in an infinity of different units. The value of n is stored as if the quantity was expressed in the base units of the same dimemsions. In the example above, n = 9.5 for the variable size and n = 0.120 for the variable time.

The value method can be used to extract the number n as if it was expressed in any possible unit. The user must pass this unit to the method. This way, the user makes it clear in which unit the value was expressed.

auto size = 9.5 * meter;
auto valueMeter      = size.value(meter);        // valueMeter == 9.5
auto valueCentimeter = size.value(centi(meter)); // valueCentimeter == 950

Arithmetic operators (+ - * /), as well as assignment and comparison operators, are defined when the operations are dimensionally consistent, otherwise an error occurs at compile-time:

auto time = 2 * hour + 17 * minute;
auto frequency = time / second;
time = time + 2 * meter; // Compilation error

Any kind of quantities and units can be defined with this module, not just those from the SI. The SI quantities and units are in fact defined in the module quantities.si. When a quantity that is not predefined has to be used, instead of instantiating the Quantity template first, it is preferable to start defining a new base unit (with only one dimension) using the unit template, and then the quantity type with the typeof operator:

enum euro = unit!"C"; // C for currency
alias Currency = typeof(euro);
assert(is(Currency == Quantity!(double, "C", 1)));

This means that all currencies will be defined with respect to euro.

Members

Classes

DimensionException
class DimensionException

Exception thrown when operating on two units that are not interconvertible.

Functions

dimstr
string dimstr(int[string] dim)
Undocumented in source. Be warned that the author may not have intended to support it.
dimstr
string dimstr()
Undocumented in source. Be warned that the author may not have intended to support it.
store
auto store(Q quantity, T delegate(Q.valueType) convertDelegate)

Returns a new quantity where the value is stored in a field of type T.

toAA
int[string] toAA()
Undocumented in source. Be warned that the author may not have intended to support it.

Structs

Quantity
struct Quantity(N, Dim...)

A quantity that can be expressed as the product of a number and a set of dimensions.

Templates

AreConsistent
template AreConsistent(Q1, Q2)

Check that two quantity types are dimensionally consistent.

Filter
template Filter(string s, Dim...)
Undocumented in source.
FilterOut
template FilterOut(string s, Dim...)
Undocumented in source.
FilterPred
template FilterPred(alias pred, Dim...)
Undocumented in source.
Invert
template Invert(Dim...)
Undocumented in source.
Is
template Is(T...)
Undocumented in source.
IsDim
template IsDim(string d1, int p1)
Undocumented in source.
OpBinary
template OpBinary(Dim...)
Undocumented in source.
Pow
template Pow(int n, Dim...)
Undocumented in source.
PowInverse
template PowInverse(int n, Dim...)
Undocumented in source.
Reduce
template Reduce(int seed, Dim...)
Undocumented in source.
RemoveNull
template RemoveNull(Dim...)
Undocumented in source.
Simplify
template Simplify(Dim...)
Undocumented in source.
Sort
template Sort(Dim...)
Undocumented in source.
Store
template Store(Q, N)

Creates a new quantity type where the payload is stored as another numeric type.

isNumberLike
template isNumberLike(N)
Undocumented in source.
isQuantity
template isQuantity(T)

Tests whether T is a quantity type

prefix
template prefix(alias factor)

Creates a new prefix function that mutlpy a Quantity by _factor factor.

unit
template unit(N, string symbol)

Creates a new monodimensional unit.

Meta

Authors

Nicolas Sicard