quantities

The purpose of this package is to perform automatic compile-time or runtime dimensional checking when dealing with quantities and units.

Synopsis:

1 import quantities;
2 import std.math : approxEqual;
3 import std.stdio : writeln, writefln;
4 
5 // Working with predefined units
6 {
7     auto distance = 384_400 * kilo(meter);
8     auto speed = 299_792_458  * meter/second;
9     
10     Time time;
11     time = distance / speed;
12     writefln("Travel time of light from the moon: %s s", time.value(second));
13 
14     static assert(is(typeof(distance) == Length));
15     static assert(is(Speed == Quantity!(double, ["L": 1, "T": -1])));
16 }
17 
18 // Dimensional correctness is check at compile-time
19 {
20     Mass mass;
21     static assert(!__traits(compiles, mass = 15 * meter));
22     static assert(!__traits(compiles, mass = 1.2));
23 }
24 
25 // Calculations can be done at compile-time
26 {
27     enum distance = 384_400 * kilo(meter);
28     enum speed = 299_792_458  * meter/second;
29     enum time = distance / speed;
30     writefln("Travel time of light from the moon: %s s", time.value(second));
31 }
32 
33 // Create a new unit from the predefined ones
34 {
35     enum inch = 2.54 * centi(meter);
36     enum mile = 1609 * meter;
37     writefln("There are %s inches in a mile", mile.value(inch));
38 }
39 
40 // Create a new unit with new dimensions
41 {
42     // Create a new base unit of currency
43     enum euro = unit!(double, "C"); // C is the chosen dimension symol (for currency...)
44 
45     auto dollar = euro / 1.35;
46     auto price = 2000 * dollar;
47     writefln("This computer costs €%.2f", price.value(euro));
48 }
49 
50 // Compile-time parsing
51 {
52     enum distance = si!"384_400 km";
53     enum speed = si!"299_792_458 m/s";
54     enum time = distance / speed;
55     writefln("Travel time of light from the moon: %s s", time.value(second));
56 
57     static assert(is(typeof(distance) == Length));
58     static assert(is(typeof(speed) == Speed));
59 }
60 
61 // Runtime parsing
62 {
63     auto data = [
64         "distance-to-the-moon": "384_400 km",
65         "speed-of-light": "299_792_458 m/s"
66     ];
67     auto distance = parseSI!Length(data["distance-to-the-moon"]);
68     auto speed = parseSI!Speed(data["speed-of-light"]);
69     auto time = distance / speed;
70     writefln("Travel time of light from the moon: %s s", time.value(second));
71 }
72 
73 // Chemistry session
74 {
75     // Use the predefined quantity types (in module quantities.si)
76     Volume volume;
77     Concentration concentration;
78     Mass mass;
79 
80     // Define a new quantity type
81     alias MolarMass = typeof(kilogram/mole);
82 
83     // I have to make a new solution at the concentration of 25 mmol/L
84     concentration = 25 * milli(mole)/liter;
85 
86     // The final volume is 100 ml.
87     volume = 100 * milli(liter);
88 
89     // The molar mass of my compound is 118.9 g/mol
90     MolarMass mm = 118.9 * gram/mole;
91 
92     // What mass should I weigh?
93     mass = concentration * volume * mm;
94     writefln("Weigh %s of substance", mass); 
95     // prints: Weigh 0.00029725 [M] of substance
96     // Wait! That's not really useful!
97     // My scales graduations are in 1/10 milligrams!
98     writefln("Weigh %.1f mg of substance", mass.value(milli(gram)));
99     // prints: Weigh 297.3 mg of substance
100 }

Modules

base
module quantities.base

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

math
module quantities.math

This module defines common math operations on quantities.

parsing
module quantities.parsing

This module defines functions to parse units and quantities. The text input is parsed according to the following grammar. For example:

Prefixes and unit symbols must be joined:
"1 mm" = 1 millimeter
"1 m m" = 1 square meter


Standalone units are preferred over prefixed ones:
"1 cd" = 1 candela, not 1 centiday


Powers of units:
"1 m^2"
"1 m²" (superscript integer)


Multiplication of to units:
"1 N m" (whitespace)
"1 N . m"
"1 N ⋅ m" (centered dot)
"1 N * m"
"1 N × m" (times sign)


Division of to units:
"1 mol / s"
"1 mol ÷ s"


Grouping of units with parentheses:
"1 kg/(m.s^2)" = 1 kg m⁻¹ s⁻²
qvariant
module quantities.qvariant

This module defines dimensionnaly variant quantities.

si
module quantities.si

This module defines the SI units and prefixes.

Public Imports

quantities.base
public import quantities.base;
Undocumented in source.
quantities.math
public import quantities.math;
Undocumented in source.
quantities.parsing
public import quantities.parsing;
Undocumented in source.
quantities.qvariant
public import quantities.qvariant;
Undocumented in source.
quantities.si
public import quantities.si;
Undocumented in source.

Meta

Authors

Nicolas Sicard