quantities.compiletime

This module defines quantities that are statically checked for dimensional consistency at compile-time.

The dimensions are part of their types, so that the compilation fails if an operation or a function call is not dimensionally consistent.

Members

Functions

abs
Q abs(Q quantity)
cbrt
auto cbrt(Q quantity)
cubic
auto cubic(Q quantity)
nthRoot
auto nthRoot(Q quantity)
pow
auto pow(Q quantity)
sqrt
auto sqrt(Q quantity)

Basic math functions that work with Quantity.

square
auto square(Q quantity)

Basic math functions that work with Quantity.

unit
auto unit()

Creates a new monodimensional unit as a Quantity.

Structs

Quantity
struct Quantity(N, alias dims)

A quantity checked at compile-time for dimensional consistency.

Templates

isQuantity
template isQuantity(T)

Tests whether T is a quantity type.

Examples

1 import quantities.compiletime;
2 import quantities.si;
3 import std.format : format;
4 import std.math : approxEqual;
5 
6 // Introductory example
7 {
8     // Use the predefined quantity types (in module quantities.si)
9     Volume volume;
10     Concentration concentration;
11     Mass mass;
12 
13     // Define a new quantity type
14     alias MolarMass = typeof(kilogram / mole);
15 
16     // I have to make a new solution at the concentration of 5 mmol/L
17     concentration = 5.0 * milli(mole) / liter;
18 
19     // The final volume is 100 ml.
20     volume = 100.0 * milli(liter);
21 
22     // The molar mass of my compound is 118.9 g/mol
23     MolarMass mm = 118.9 * gram / mole;
24 
25     // What mass should I weigh?
26     mass = concentration * volume * mm;
27     assert(format("%s", mass) == "5.945e-05 [M]");
28     // Wait! That's not really useful!
29     assert(siFormat!"%.1f mg"(mass) == "59.5 mg");
30 }
31 
32 // Working with predefined units
33 {
34     auto distance = 384_400 * kilo(meter); // From Earth to Moon
35     auto speed = 299_792_458 * meter / second; // Speed of light
36     auto time = distance / speed;
37     assert(time.siFormat!"%.3f s" == "1.282 s");
38 }
39 
40 // Dimensional correctness is check at compile-time
41 {
42     Mass mass;
43     assert(!__traits(compiles, mass = 15 * meter));
44     assert(!__traits(compiles, mass = 1.2));
45 }
46 
47 // Calculations can be done at compile-time
48 {
49     enum distance = 384_400 * kilo(meter); // From Earth to Moon
50     enum speed = 299_792_458 * meter / second; // Speed of light
51     enum time = distance / speed;
52     /* static */
53     assert(time.siFormat!"%.3f s" == "1.282 s");
54     // NB. Phobos can't format floating point values at run-time.
55 }
56 
57 // Create a new unit from the predefined ones
58 {
59     auto inch = 2.54 * centi(meter);
60     auto mile = 1609 * meter;
61     assert(mile.value(inch).approxEqual(63_346)); // inches in a mile
62     // NB. Cannot use siFormatter, because inches are not SI units
63 }
64 
65 // Create a new unit with new dimensions
66 {
67     // Create a new base unit of currency
68     auto euro = unit!(double, "C"); // C is the chosen dimension symol (for currency...)
69 
70     auto dollar = euro / 1.35;
71     auto price = 2000 * dollar;
72     assert(price.value(euro).approxEqual(1481)); // Price in euros
73 }
74 
75 // Compile-time parsing
76 {
77     enum distance = si!"384_400 km";
78     enum speed = si!"299_792_458 m/s";
79     assert(is(typeof(distance) == Length));
80     assert(is(typeof(speed) == Speed));
81 }
82 
83 // Run-time parsing of statically typed Quantities
84 {
85     auto data = ["distance-to-the-moon" : "384_400 km", "speed-of-light" : "299_792_458 m/s"];
86     auto distance = parseSI!Length(data["distance-to-the-moon"]);
87     auto speed = parseSI!Speed(data["speed-of-light"]);
88 }

Meta

Authors

Nicolas Sicard