1 | <?xml version="1.0" encoding="utf-8" ?> |
---|
2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
---|
3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
---|
4 | <head> |
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
6 | <meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" /> |
---|
7 | <title>THE BOOST MPL LIBRARY: Dimensional Analysis</title> |
---|
8 | <link rel="stylesheet" href="../style.css" type="text/css" /> |
---|
9 | </head> |
---|
10 | <body class="docframe"> |
---|
11 | <table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Prev</a> <a href="./representing-dimensions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group">Back <a href="./higher-order.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Up</a> <a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td> |
---|
12 | <td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./dimensional-analysis.html" class="navigation-link">Dimensional Analysis</a></td> |
---|
13 | </tr></table><div class="header-separator"></div> |
---|
14 | <div class="section" id="dimensional-analysis"> |
---|
15 | <h1><a class="toc-backref" href="./tutorial-metafunctions.html#id41" name="dimensional-analysis">Dimensional Analysis</a></h1> |
---|
16 | <p>The first rule of doing physical calculations |
---|
17 | on paper is that the numbers being manipulated don't stand alone: |
---|
18 | most quantities have attached <em>dimensions</em>, to be ignored at our |
---|
19 | peril. As computations become more complex, keeping track of |
---|
20 | dimensions is what keeps us from inadvertently assigning a mass to |
---|
21 | what should be a length or adding acceleration to velocity — it |
---|
22 | establishes a type system for numbers.</p> |
---|
23 | <p>Manual checking of types is tedious, and as a result, it's also |
---|
24 | error-prone. When human beings become bored, their attention |
---|
25 | wanders and they tend to make mistakes. Doesn't type checking seem |
---|
26 | like the sort of job a computer might be good at, though? If we |
---|
27 | could establish a framework of C++ types for dimensions and |
---|
28 | quantities, we might be able to catch errors in formulae before |
---|
29 | they cause serious problems in the real world.</p> |
---|
30 | <p>Preventing quantities with different dimensions from interoperating |
---|
31 | isn't hard; we could simply represent dimensions as classes that |
---|
32 | only work with dimensions of the same type. What makes this |
---|
33 | problem interesting is that different dimensions <em>can</em> be combined, |
---|
34 | via multiplication or division, to produce arbitrarily complex new |
---|
35 | dimensions. For example, take Newton's law, which relates force to |
---|
36 | mass and acceleration:</p> |
---|
37 | <blockquote> |
---|
38 | <em>F</em> = <em>ma</em></blockquote> |
---|
39 | <p>Since mass and acceleration have different dimensions, the |
---|
40 | dimensions of force must somehow capture their combination. In |
---|
41 | fact, the dimensions of acceleration are already just such a |
---|
42 | composite, a change in velocity over time:</p> |
---|
43 | <blockquote> |
---|
44 | <em>dv</em>/<em>dt</em></blockquote> |
---|
45 | <p>Since velocity is just change in distance (<em>l</em>) over time (<em>t</em>), |
---|
46 | the fundamental dimensions of acceleration are:</p> |
---|
47 | <blockquote> |
---|
48 | (<em>l</em>/<em>t</em>)/<em>t</em> = <em>l</em>/<em>t</em><sup>2</sup></blockquote> |
---|
49 | <p>And indeed, acceleration is commonly measured in "meters per second |
---|
50 | squared." It follows that the dimensions of force must be:</p> |
---|
51 | <blockquote> |
---|
52 | <em>ml</em>/<em>t</em><sup>2</sup></blockquote> |
---|
53 | <!-- @litre_translator.line_offset -= 7 --> |
---|
54 | <p>and force is commonly measured in kg(m/s<sup>2</sup>), or |
---|
55 | "kilogram-meters per second squared." When multiplying quantities |
---|
56 | of mass and acceleration, we multiply their dimensions as well and |
---|
57 | carry the result along, which helps us to ensure that the result is |
---|
58 | meaningful. The formal name for this bookkeeping is <strong>dimensional |
---|
59 | analysis</strong>, and our next task will be to implement its rules in the C++ |
---|
60 | type system. John Barton and Lee Nackman were the first to show |
---|
61 | how to do this in their seminal book, <em>Scientific and Engineering |
---|
62 | C++</em> <a class="citation-reference" href="#bn94" id="id5" name="id5">[BN94]</a>. We will recast their approach here in |
---|
63 | metaprogramming terms.</p> |
---|
64 | <table class="citation" frame="void" id="bn94" rules="none"> |
---|
65 | <colgroup><col class="label" /><col /></colgroup> |
---|
66 | <tbody valign="top"> |
---|
67 | <tr><td class="label"><a class="fn-backref" href="#id5" name="bn94">[BN94]</a></td><td>John J. Barton and Lee R. Nackman. <em>Scientific and |
---|
68 | Engineering C++: an Introduction with Advanced Techniques and |
---|
69 | Examples.</em> Reading, MA: Addison Wesley. ISBN |
---|
70 | 0-201-53393-6. 1994.</td></tr> |
---|
71 | </tbody> |
---|
72 | </table> |
---|
73 | <ul class="toc simple" id="outline"> |
---|
74 | <li><a class="reference" href="./representing-dimensions.html" id="id42" name="id42">Representing Dimensions</a></li> |
---|
75 | <li><a class="reference" href="./representing-quantities.html" id="id43" name="id43">Representing Quantities</a></li> |
---|
76 | <li><a class="reference" href="./implementing-addition-and.html" id="id44" name="id44">Implementing Addition and Subtraction</a></li> |
---|
77 | <li><a class="reference" href="./implementing.html" id="id45" name="id45">Implementing Multiplication</a></li> |
---|
78 | <li><a class="reference" href="./implementing-division.html" id="id46" name="id46">Implementing Division</a></li> |
---|
79 | </ul> |
---|
80 | </div> |
---|
81 | |
---|
82 | <div class="footer-separator"></div> |
---|
83 | <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Prev</a> <a href="./representing-dimensions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group">Back <a href="./higher-order.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Up</a> <a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td> |
---|
84 | </tr></table></body> |
---|
85 | </html> |
---|