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