quantities.runtime

This module defines dimensionally variant quantities, mainly for use at run-time.

The dimensions are stored in a field, along with the numerical value of the quantity. Operations and function calls fail if they are not dimensionally consistent, by throwing a DimensionException.

Members

Classes

DimensionException
class DimensionException

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

Enums

isQVariantOrQuantity
eponymoustemplate isQVariantOrQuantity(T)
Undocumented in source.

Functions

abs
Q abs(Q quantity)
cbrt
auto cbrt(Q quantity)
cubic
auto cubic(Q quantity)
nthRoot
auto nthRoot(Q quantity, Rational r)

Basic math functions that work with QVariant.

nthRoot
auto nthRoot(Q quantity, I n)
Undocumented in source. Be warned that the author may not have intended to support it.
pow
auto pow(Q quantity, Rational r)

Basic math functions that work with QVariant.

pow
auto pow(Q quantity, I n)
Undocumented in source. Be warned that the author may not have intended to support it.
qVariant
auto qVariant(Q qty)

Turns a Quantity into a QVariant

qVariant
auto qVariant(N scalar)

Turns a scalar into a dimensionless QVariant

sqrt
auto sqrt(Q quantity)

Basic math functions that work with QVariant.

square
auto square(Q quantity)

Basic math functions that work with QVariant.

unit
QVariant!N unit(string dimSymbol, size_t rank)

Creates a new monodimensional unit as a QVariant.

Structs

QVariant
struct QVariant(N)

A dimensionnaly variant quantity.

Templates

isQVariant
template isQVariant(T)
Undocumented in source.

Examples

1 import quantities.runtime;
2 import quantities.si;
3 import std.format : format;
4 import std.math : approxEqual;
5 
6 // Note: the types of the predefined SI units (gram, mole, liter...)
7 // are Quantity instances, not QVariant instance.
8 
9 // Introductory example
10 {
11     // I have to make a new solution at the concentration of 5 mmol/L
12     QVariant!double concentration = 5.0 * milli(mole) / liter;
13 
14     // The final volume is 100 ml.
15     QVariant!double volume = 100.0 * milli(liter);
16 
17     // The molar mass of my compound is 118.9 g/mol
18     QVariant!double molarMass = 118.9 * gram / mole;
19 
20     // What mass should I weigh?
21     QVariant!double mass = concentration * volume * molarMass;
22     assert(format("%s", mass) == "5.945e-05 [M]");
23     // Wait! That's not really useful!
24     assert(siFormat!"%.1f mg"(mass) == "59.5 mg");
25 }
26 
27 // Working with predefined units
28 {
29     QVariant!double distance = 384_400 * kilo(meter); // From Earth to Moon
30     QVariant!double speed = 299_792_458 * meter / second; // Speed of light
31     QVariant!double time = distance / speed;
32     assert(time.siFormat!"%.3f s" == "1.282 s");
33 }
34 
35 // Dimensional correctness
36 {
37     import std.exception : assertThrown;
38 
39     QVariant!double mass = 4 * kilogram;
40     assertThrown!DimensionException(mass + meter);
41     assertThrown!DimensionException(mass == 1.2);
42 }
43 
44 // Create a new unit from the predefined ones
45 {
46     QVariant!double inch = 2.54 * centi(meter);
47     QVariant!double mile = 1609 * meter;
48     assert(mile.value(inch).approxEqual(63_346)); // inches in a mile
49     // NB. Cannot use siFormatter, because inches are not SI units
50 }
51 
52 // Create a new unit with new dimensions
53 {
54     // Create a new base unit of currency
55     QVariant!double euro = unit!double("C"); // C is the chosen dimension symol (for currency...)
56 
57     QVariant!double dollar = euro / 1.35;
58     QVariant!double price = 2000 * dollar;
59     assert(price.value(euro).approxEqual(1481)); // Price in euros
60 }
61 
62 // Run-time parsing
63 {
64     auto data = ["distance-to-the-moon" : "384_400 km", "speed-of-light" : "299_792_458 m/s"];
65     QVariant!double distance = parseSI(data["distance-to-the-moon"]);
66     QVariant!double speed = parseSI(data["speed-of-light"]);
67     QVariant!double time = distance / speed;
68 }

Meta

Authors

Nicolas Sicard