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