1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>User's Guide</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../ratio.html" title="Chapter 34. Boost.Ratio 2.1.0"> 10<link rel="prev" href="../ratio.html" title="Chapter 34. Boost.Ratio 2.1.0"> 11<link rel="next" href="reference.html" title="Reference"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../ratio.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ratio.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="ratio.users_guide"></a><a class="link" href="users_guide.html" title="User's Guide">User's Guide</a> 29</h2></div></div></div> 30<div class="toc"><dl class="toc"> 31<dt><span class="section"><a href="users_guide.html#ratio.users_guide.getting_started">Getting Started</a></span></dt> 32<dt><span class="section"><a href="users_guide.html#ratio.users_guide.tutorial">Tutorial</a></span></dt> 33<dt><span class="section"><a href="users_guide.html#ratio.users_guide.Examples">Example</a></span></dt> 34<dt><span class="section"><a href="users_guide.html#ratio.users_guide.ext_references">External Resources</a></span></dt> 35</dl></div> 36<div class="section"> 37<div class="titlepage"><div><div><h3 class="title"> 38<a name="ratio.users_guide.getting_started"></a><a class="link" href="users_guide.html#ratio.users_guide.getting_started" title="Getting Started">Getting Started</a> 39</h3></div></div></div> 40<div class="toc"><dl class="toc"><dt><span class="section"><a href="users_guide.html#ratio.users_guide.getting_started.install">Installing 41 Ratio</a></span></dt></dl></div> 42<div class="section"> 43<div class="titlepage"><div><div><h4 class="title"> 44<a name="ratio.users_guide.getting_started.install"></a><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install" title="Installing Ratio">Installing 45 Ratio</a> 46</h4></div></div></div> 47<h6> 48<a name="ratio.users_guide.getting_started.install.h0"></a> 49 <span class="phrase"><a name="ratio.users_guide.getting_started.install.getting_boost_ratio_"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.getting_boost_ratio_">Getting 50 Boost.Ratio </a> 51 </h6> 52<p> 53 Boost.Ratio is in the latest Boost release in the folder <code class="computeroutput"><span class="special">/</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">ratio</span></code>. 54 Documentation, tests and examples folder are at <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">libs</span><span class="special">/</span><span class="identifier">ratio</span><span class="special">/</span></code>. 55 </p> 56<p> 57 You can also access the latest (unstable?) state from the <a href="http://svn.boost.org/svn/boost/trunk" target="_top">Boost 58 trunk</a> directories <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">ratio</span></code> 59 and <code class="computeroutput"><span class="identifier">libs</span><span class="special">/</span><span class="identifier">ratio</span></code>. 60 </p> 61<p> 62 Just go to <a href="http://svn.boost.org/trac/boost/wiki/BoostSubversion" target="_top">the 63 wiki</a> and follow the instructions there for anonymous SVN access. 64 </p> 65<h6> 66<a name="ratio.users_guide.getting_started.install.h1"></a> 67 <span class="phrase"><a name="ratio.users_guide.getting_started.install.where_to_install_boost_ratio__"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.where_to_install_boost_ratio__">Where 68 to install Boost.Ratio? </a> 69 </h6> 70<p> 71 The simple way is to decompress (or checkout from SVN) the files in your 72 BOOST_ROOT directory. 73 </p> 74<h6> 75<a name="ratio.users_guide.getting_started.install.h2"></a> 76 <span class="phrase"><a name="ratio.users_guide.getting_started.install.building_boost_ratio_"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.building_boost_ratio_">Building 77 Boost.Ratio </a> 78 </h6> 79<p> 80 <span class="bold"><strong>Boost.Ratio</strong></span> is a header only library, 81 so no need to compile anything, you just need to <code class="computeroutput"><span class="identifier">include</span> 82 <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">ratio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>. 83 </p> 84<h6> 85<a name="ratio.users_guide.getting_started.install.h3"></a> 86 <span class="phrase"><a name="ratio.users_guide.getting_started.install.requirements"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.requirements">Requirements</a> 87 </h6> 88<p> 89 <span class="bold"><strong>Boost.Ratio</strong></span> depends on some Boost libraries. 90 For these specific parts you must use either Boost version 1.39.0 or later 91 (even older versions may work). 92 </p> 93<p> 94 In particular, <span class="bold"><strong>Boost.Ratio</strong></span> depends on: 95 </p> 96<div class="variablelist"> 97<p class="title"><b></b></p> 98<dl class="variablelist"> 99<dt><span class="term"><a href="http://www.boost.org/libs/config" target="_top"><span class="bold"><strong>Boost.Config</strong></span></a></span></dt> 100<dd><p> 101 for configuration purposes, ... 102 </p></dd> 103<dt><span class="term"><a href="http://www.boost.org/libs/integer" target="_top"><span class="bold"><strong>Boost.Integer</strong></span></a></span></dt> 104<dd><p> 105 for cstdint conformance, and integer traits ... 106 </p></dd> 107<dt><span class="term"><a href="http://www.boost.org/libs/mpl" target="_top"><span class="bold"><strong>Boost.MPL</strong></span></a></span></dt> 108<dd><p> 109 for MPL Assert and bool, logical ... 110 </p></dd> 111<dt><span class="term"><a href="http://www.boost.org/libs/static_assert" target="_top"><span class="bold"><strong>Boost.StaticAssert</strong></span></a></span></dt> 112<dd><p> 113 for STATIC_ASSERT, ... 114 </p></dd> 115<dt><span class="term"><a href="http://www.boost.org/libs/type_traits" target="_top"><span class="bold"><strong>Boost.TypeTraits</strong></span></a></span></dt> 116<dd><p> 117 for is_base, is_convertible ... 118 </p></dd> 119<dt><span class="term"><a href="http://www.boost.org/libs/utility" target="_top"><span class="bold"><strong>Boost.Utility/EnableIf</strong></span></a></span></dt> 120<dd><p> 121 for enable_if, ... 122 </p></dd> 123</dl> 124</div> 125<h6> 126<a name="ratio.users_guide.getting_started.install.h4"></a> 127 <span class="phrase"><a name="ratio.users_guide.getting_started.install.building_an_executable_that_uses__emphasis_role__bold__boost_ratio__emphasis__"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.building_an_executable_that_uses__emphasis_role__bold__boost_ratio__emphasis__">Building 128 an executable that uses <span class="bold"><strong>Boost.Ratio</strong></span> </a> 129 </h6> 130<p> 131 No link is needed. 132 </p> 133<h6> 134<a name="ratio.users_guide.getting_started.install.h5"></a> 135 <span class="phrase"><a name="ratio.users_guide.getting_started.install.exception_safety_"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.exception_safety_">Exception 136 safety </a> 137 </h6> 138<p> 139 All functions in the library are exception-neutral, providing the strong 140 exception safety guarantee. 141 </p> 142<h6> 143<a name="ratio.users_guide.getting_started.install.h6"></a> 144 <span class="phrase"><a name="ratio.users_guide.getting_started.install.thread_safety_"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.thread_safety_">Thread 145 safety </a> 146 </h6> 147<p> 148 All functions in the library are thread-unsafe except when noted explicitly. 149 </p> 150<h6> 151<a name="ratio.users_guide.getting_started.install.h7"></a> 152 <span class="phrase"><a name="ratio.users_guide.getting_started.install.tested_compilers_"></a></span><a class="link" href="users_guide.html#ratio.users_guide.getting_started.install.tested_compilers_">Tested 153 compilers </a> 154 </h6> 155<p> 156 <span class="bold"><strong>Boost.Ratio</strong></span> should work with an C++03 157 conforming compiler. The current version has been tested on: 158 </p> 159<p> 160 Windows with 161 </p> 162<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 163 MSVC 10.0 164 </li></ul></div> 165<p> 166 MinGW with 167 </p> 168<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 169<li class="listitem"> 170 GCC 4.5.0 171 </li> 172<li class="listitem"> 173 GCC 4.5.0 -std=c++0x 174 </li> 175<li class="listitem"> 176 GCC 4.5.2 177 </li> 178<li class="listitem"> 179 GCC 4.5.2 -std=c++0x 180 </li> 181<li class="listitem"> 182 GCC 4.6.0 183 </li> 184<li class="listitem"> 185 GCC 4.6.0 -std=c++0x 186 </li> 187</ul></div> 188<p> 189 Ubuntu with * GCC 4.4.6 * GCC 4.4.6 -std=c++0x * GCC 4.5.4 * GCC 4.5.4 190 -std=c++0x * GCC 4.6.1 * GCC 4.6.1 -std=c++0x * Intel 12.1.3 * Intel 12.1.3 191 -std=c++0x 192 </p> 193<p> 194 OsX with 195 </p> 196<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 197<li class="listitem"> 198 GCC 4.1.2 199 </li> 200<li class="listitem"> 201 GCC 4.6.2 202 </li> 203<li class="listitem"> 204 GCC 4.6.2 -std=c++0x 205 </li> 206<li class="listitem"> 207 GCC 4.7.0 208 </li> 209<li class="listitem"> 210 GCC 4.7.0 -std=c++0x 211 </li> 212<li class="listitem"> 213 GCC 4.7.1 214 </li> 215<li class="listitem"> 216 GCC 4.7.1 -std=c++0x 217 </li> 218<li class="listitem"> 219 clang 1.6 220 </li> 221<li class="listitem"> 222 clang 2.9 223 </li> 224<li class="listitem"> 225 clang 2.9 -std=c++0x 226 </li> 227<li class="listitem"> 228 clang 3.0 229 </li> 230<li class="listitem"> 231 clang 3.0 -std=c++0x 232 </li> 233<li class="listitem"> 234 clang 3.1 235 </li> 236<li class="listitem"> 237 clang 3.1 -std=c++0x 238 </li> 239<li class="listitem"> 240 clang 3.1 -std=c++0x -stdlib=libc++ 241 </li> 242<li class="listitem"> 243 clang 3.2 244 </li> 245<li class="listitem"> 246 clang 3.2 -std=c++11 247 </li> 248<li class="listitem"> 249 clang 3.2 -std=c++11 -stdlib=libc++ 250 </li> 251</ul></div> 252<div class="note"><table border="0" summary="Note"> 253<tr> 254<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td> 255<th align="left">Note</th> 256</tr> 257<tr><td align="left" valign="top"><p> 258 Please let us know how this works on other platforms/compilers. 259 </p></td></tr> 260</table></div> 261<div class="note"><table border="0" summary="Note"> 262<tr> 263<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td> 264<th align="left">Note</th> 265</tr> 266<tr><td align="left" valign="top"><p> 267 Please send any questions, comments and bug reports to boost <at> 268 lists <dot> boost <dot> org. 269 </p></td></tr> 270</table></div> 271</div> 272</div> 273<div class="section"> 274<div class="titlepage"><div><div><h3 class="title"> 275<a name="ratio.users_guide.tutorial"></a><a class="link" href="users_guide.html#ratio.users_guide.tutorial" title="Tutorial">Tutorial</a> 276</h3></div></div></div> 277<h5> 278<a name="ratio.users_guide.tutorial.h0"></a> 279 <span class="phrase"><a name="ratio.users_guide.tutorial.ratio"></a></span><a class="link" href="users_guide.html#ratio.users_guide.tutorial.ratio">Ratio</a> 280 </h5> 281<p> 282 <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> 283 is a general purpose utility inspired by Walter Brown allowing one to easily 284 and safely compute rational values at compile-time. The <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> class catches all errors (such 285 as divide by zero and overflow) at compile time. It is used in the duration 286 and <code class="computeroutput"><span class="identifier">time_point</span></code> classes to 287 efficiently create units of time. It can also be used in other "quantity" 288 libraries or anywhere there is a rational constant which is known at compile-time. 289 The use of this utility can greatly reduce the chances of run-time overflow 290 because the <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> (and any ratios resulting 291 from <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> 292 arithmetic) are always reduced to the lowest terms. 293 </p> 294<p> 295 <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> 296 is a template taking two <code class="computeroutput"><span class="identifier">intmax_ts</span></code>, 297 with the second defaulted to 1. In addition to copy constructors and assignment, 298 it only has two public members, both of which are <code class="computeroutput"><span class="keyword">static</span> 299 <span class="keyword">const</span> <span class="identifier">intmax_t</span></code>. 300 One is the numerator of the <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> and the other is the denominator. 301 The <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a> 302 is always normalized such that it is expressed in lowest terms, and the denominator 303 is always positive. When the numerator is 0, the denominator is always 1. 304 </p> 305<p> 306 <span class="bold"><strong>Example:</strong></span> 307 </p> 308<pre class="programlisting"><span class="keyword">typedef</span> <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">5</span><span class="special">,</span> <span class="number">3</span><span class="special">></span> <span class="identifier">five_thirds</span><span class="special">;</span> 309<span class="comment">// five_thirds::num == 5, five_thirds::den == 3</span> 310 311<span class="keyword">typedef</span> <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">25</span><span class="special">,</span> <span class="number">15</span><span class="special">></span> <span class="identifier">also_five_thirds</span><span class="special">;</span> 312<span class="comment">// also_five_thirds::num == 5, also_five_thirds::den == 3</span> 313 314<span class="keyword">typedef</span> <span class="identifier">ratio_divide</span><span class="special"><</span><span class="identifier">five_thirds</span><span class="special">,</span> <span class="identifier">also_five_thirds</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">one</span><span class="special">;</span> 315<span class="comment">// one::num == 1, one::den == 1</span> 316</pre> 317<p> 318 This facility also includes convenience typedefs for the SI prefixes <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">atto</span></code></a> 319 through <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">exa</span></code></a> corresponding to their internationally 320 recognized definitions (in terms of <a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a>). This is a tremendous syntactic 321 convenience. It will prevent errors in specifying constants as one no longer 322 has to double count the number of zeros when trying to write millions or 323 billions. 324 </p> 325<p> 326 <span class="bold"><strong>Example:</strong></span> 327 </p> 328<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">ratio_multiply</span><span class="special"><</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="identifier">giga</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">_5giga</span><span class="special">;</span> 329<span class="comment">// _5giga::num == 5000000000, _5giga::den == 1</span> 330 331<span class="keyword">typedef</span> <span class="identifier">ratio_multiply</span><span class="special"><</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="identifier">nano</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">_5nano</span><span class="special">;</span> 332<span class="comment">// _5nano::num == 1, _5nano::den == 200000000</span> 333</pre> 334<h5> 335<a name="ratio.users_guide.tutorial.h1"></a> 336 <span class="phrase"><a name="ratio.users_guide.tutorial.ratio_i_o"></a></span><a class="link" href="users_guide.html#ratio.users_guide.tutorial.ratio_i_o">Ratio 337 I/O</a> 338 </h5> 339<p> 340 For each <code class="computeroutput"><span class="identifier">ratio</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span> <span class="identifier">D</span><span class="special">></span></code> there exists a <code class="computeroutput"><span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span> 341 <span class="identifier">D</span><span class="special">>,</span> 342 <span class="identifier">CharT</span><span class="special">></span></code> 343 for which you can query two strings: <code class="computeroutput"><span class="identifier">symbol</span></code> 344 and <code class="computeroutput"><span class="identifier">prefix</span></code>. For those <code class="computeroutput"><span class="identifier">ratio</span></code>'s that correspond to an <a href="http://en.wikipedia.org/wiki/SI_prefix#List_of_SI_prefixes" target="_top">SI 345 prefix</a> prefix corresponds to the internationally recognized prefix, 346 stored as a <code class="computeroutput"><span class="identifier">basic_string</span><span class="special"><</span><span class="identifier">CharT</span><span class="special">></span></code>. For example <code class="computeroutput"><span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">mega</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span></code> 347 returns <code class="computeroutput"><span class="identifier">string</span><span class="special">(</span><span class="string">"mega"</span><span class="special">)</span></code>. 348 For those <code class="computeroutput"><span class="identifier">ratio</span></code>s that correspond 349 to an <a href="http://en.wikipedia.org/wiki/SI_prefix#List_of_SI_prefixes" target="_top">SI 350 prefix</a> <code class="computeroutput"><span class="identifier">symbol</span></code> corresponds 351 to the internationally recognized symbol, stored as a <code class="computeroutput"><span class="identifier">basic_string</span><span class="special"><</span><span class="identifier">CharT</span><span class="special">></span></code>. For example, <code class="computeroutput"><span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">mega</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span></code> 352 returns <code class="computeroutput"><span class="identifier">string</span><span class="special">(</span><span class="string">"M"</span><span class="special">)</span></code>. 353 For all other <code class="computeroutput"><span class="identifier">ratio</span></code>s, both 354 <code class="computeroutput"><span class="identifier">prefix</span><span class="special">()</span></code> 355 and <code class="computeroutput"><span class="identifier">symbol</span><span class="special">()</span></code> 356 return a <code class="computeroutput"><span class="identifier">basic_string</span></code> containing 357 "[<code class="computeroutput"><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">num</span><span class="special">/</span><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">den</span></code>]". 358 </p> 359<p> 360 <code class="computeroutput"><span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span> <span class="identifier">D</span><span class="special">>,</span> <span class="identifier">CharT</span><span class="special">></span></code> is only defined for four character types: 361 </p> 362<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 363<li class="listitem"> 364 <code class="computeroutput"><span class="keyword">char</span></code>: UTF-8 365 </li> 366<li class="listitem"> 367 <code class="computeroutput"><span class="keyword">char16_t</span></code>: UTF-16 368 </li> 369<li class="listitem"> 370 <code class="computeroutput"><span class="keyword">char32_t</span></code>: UTF-32 371 </li> 372<li class="listitem"> 373 <code class="computeroutput"><span class="keyword">wchar_t</span></code>: UTF-16 (if wchar_t 374 is 16 bits) or UTF-32 375 </li> 376</ul></div> 377<p> 378 When the character is char, UTF-8 will be used to encode the names. When 379 the character is <code class="computeroutput"><span class="keyword">char16_t</span></code>, UTF-16 380 will be used to encode the names. When the character is <code class="computeroutput"><span class="keyword">char32_t</span></code>, 381 UTF-32 will be used to encode the names. When the character is <code class="computeroutput"><span class="keyword">wchar_t</span></code>, the encoding will be UTF-16 if <code class="computeroutput"><span class="keyword">wchar_t</span></code> is 16 bits, and otherwise UTF-32. 382 </p> 383<p> 384 The <code class="computeroutput"><span class="identifier">symbol</span></code> (Greek mu or μ) 385 for micro is defined by <a href="http://www.unicode.org/charts/PDF/U0080.pdf" target="_top">Unicode</a> 386 to be U+00B5. 387 </p> 388<p> 389 <span class="bold"><strong>Examples:</strong></span> 390 </p> 391<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">ratio</span><span class="special">/</span><span class="identifier">ratio_io</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 392<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 393 394<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 395<span class="special">{</span> 396 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span> 397 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span> 398 399 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<deca, char>::prefix() = "</span> 400 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">deca</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 401 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<deca, char>::symbol() = "</span> 402 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">deca</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 403 404 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<giga, char>::prefix() = "</span> 405 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">giga</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 406 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<giga, char>::symbol() = "</span> 407 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">giga</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 408 409 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<ratio<4, 6>, char>::prefix() = "</span> 410 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="number">4</span><span class="special">,</span> <span class="number">6</span><span class="special">>,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 411 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"ratio_string<ratio<4, 6>, char>::symbol() = "</span> 412 <span class="special"><<</span> <span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="number">4</span><span class="special">,</span> <span class="number">6</span><span class="special">>,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 413<span class="special">}</span> 414</pre> 415<p> 416 The output will be 417 </p> 418<pre class="programlisting"><span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">deca</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">deca</span> 419<span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">deca</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">da</span> 420<span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">giga</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">giga</span> 421<span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">giga</span><span class="special">,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">G</span> 422<span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="number">4</span><span class="special">,</span> <span class="number">6</span><span class="special">>,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">prefix</span><span class="special">()</span> <span class="special">=</span> <span class="special">[</span><span class="number">2</span><span class="special">/</span><span class="number">3</span><span class="special">]</span> 423<span class="identifier">ratio_string</span><span class="special"><</span><span class="identifier">ratio</span><span class="special"><</span><span class="number">4</span><span class="special">,</span> <span class="number">6</span><span class="special">>,</span> <span class="keyword">char</span><span class="special">>::</span><span class="identifier">symbol</span><span class="special">()</span> <span class="special">=</span> <span class="special">[</span><span class="number">2</span><span class="special">/</span><span class="number">3</span><span class="special">]</span> 424</pre> 425<h5> 426<a name="ratio.users_guide.tutorial.h2"></a> 427 <span class="phrase"><a name="ratio.users_guide.tutorial.ratio_mpl_numeric_metafunctions"></a></span><a class="link" href="users_guide.html#ratio.users_guide.tutorial.ratio_mpl_numeric_metafunctions">Ratio 428 MPL Numeric Metafunctions</a> 429 </h5> 430<p> 431 With the view of the _ratio class as a <a class="link" href="reference.html#ratio.reference.mpl.rational_constant" title="Rational Constant Concept">Rational 432 Constant</a> we can mix _ratio<> and <span class="bold"><strong>Boost.MPL</strong></span> 433 Integral Constants in the same expression, as in 434 </p> 435<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="identifier">giga</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">_5giga</span><span class="special">;</span> 436<span class="comment">// _5giga::num == 5000000000, _5giga::den == 1</span> 437 438<span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="identifier">nano</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">_5nano</span><span class="special">;</span> 439<span class="comment">// _5nano::num == 1, _5nano::den == 200000000</span> 440</pre> 441</div> 442<div class="section"> 443<div class="titlepage"><div><div><h3 class="title"> 444<a name="ratio.users_guide.Examples"></a><a class="link" href="users_guide.html#ratio.users_guide.Examples" title="Example">Example</a> 445</h3></div></div></div> 446<div class="toc"><dl class="toc"><dt><span class="section"><a href="users_guide.html#ratio.users_guide.Examples.si_units">SI units</a></span></dt></dl></div> 447<div class="section"> 448<div class="titlepage"><div><div><h4 class="title"> 449<a name="ratio.users_guide.Examples.si_units"></a><a class="link" href="users_guide.html#ratio.users_guide.Examples.si_units" title="SI units">SI units</a> 450</h4></div></div></div> 451<p> 452 This example illustrates the use of type-safe physics code interoperating 453 with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span></code> types, taking advantage of the 454 <span class="bold"><strong>Boost.Ratio</strong></span> infrastructure and design 455 philosophy. 456 </p> 457<p> 458 Let's start by defining a <code class="computeroutput"><span class="identifier">length</span></code> 459 class template that mimics <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span></code>, 460 which represents a time duration in various units, but restricts the representation 461 to <code class="computeroutput"><span class="keyword">double</span></code> and uses <span class="bold"><strong>Boost.Ratio</strong></span> for length unit conversions: 462 </p> 463<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Ratio</span><span class="special">></span> 464<span class="keyword">class</span> <span class="identifier">length</span> <span class="special">{</span> 465<span class="keyword">private</span><span class="special">:</span> 466 <span class="keyword">double</span> <span class="identifier">len_</span><span class="special">;</span> 467<span class="keyword">public</span><span class="special">:</span> 468 <span class="keyword">typedef</span> <span class="identifier">Ratio</span> <span class="identifier">ratio</span><span class="special">;</span> 469 <span class="identifier">length</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">len_</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">{}</span> 470 <span class="identifier">length</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">double</span><span class="special">&</span> <span class="identifier">len</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">len_</span><span class="special">(</span><span class="identifier">len</span><span class="special">)</span> <span class="special">{}</span> 471 472 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> 473 <span class="identifier">length</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">R</span><span class="special">>&</span> <span class="identifier">d</span><span class="special">)</span> 474 <span class="special">:</span> <span class="identifier">len_</span><span class="special">(</span><span class="identifier">d</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">*</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_divide</span><span class="special"><</span><span class="identifier">Ratio</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">den</span> <span class="special">/</span> 475 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_divide</span><span class="special"><</span><span class="identifier">Ratio</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">num</span><span class="special">)</span> <span class="special">{}</span> 476 477 <span class="keyword">double</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">len_</span><span class="special">;}</span> 478 479 <span class="identifier">length</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="keyword">const</span> <span class="identifier">length</span><span class="special">&</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">{</span><span class="identifier">len_</span> <span class="special">+=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;}</span> 480 <span class="identifier">length</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">-=(</span><span class="keyword">const</span> <span class="identifier">length</span><span class="special">&</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">{</span><span class="identifier">len_</span> <span class="special">-=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;}</span> 481 482 <span class="identifier">length</span> <span class="keyword">operator</span><span class="special">+()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;}</span> 483 <span class="identifier">length</span> <span class="keyword">operator</span><span class="special">-()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">length</span><span class="special">(-</span><span class="identifier">len_</span><span class="special">);}</span> 484 485 <span class="identifier">length</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">*=(</span><span class="keyword">double</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="special">{</span><span class="identifier">len_</span> <span class="special">*=</span> <span class="identifier">rhs</span><span class="special">;</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;}</span> 486 <span class="identifier">length</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">/=(</span><span class="keyword">double</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="special">{</span><span class="identifier">len_</span> <span class="special">/=</span> <span class="identifier">rhs</span><span class="special">;</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;}</span> 487<span class="special">};</span> 488</pre> 489<p> 490 Here's a small sampling of length units: 491 </p> 492<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> <span class="identifier">meter</span><span class="special">;</span> <span class="comment">// set meter as "unity"</span> 493<span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">centi</span></code></a><span class="special">></span> <span class="identifier">centimeter</span><span class="special">;</span> <span class="comment">// 1/100 meter</span> 494<span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">kilo</span></code></a><span class="special">></span> <span class="identifier">kilometer</span><span class="special">;</span> <span class="comment">// 1000 meters</span> 495<span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">254</span><span class="special">,</span> <span class="number">10000</span><span class="special">></span> <span class="special">></span> <span class="identifier">inch</span><span class="special">;</span> <span class="comment">// 254/10000 meters</span> 496</pre> 497<p> 498 Note that since <code class="computeroutput"><span class="identifier">length</span></code>'s 499 template parameter is actually a generic ratio type, so we can use boost::ratio 500 allowing for more complex length units: 501 </p> 502<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_multiply</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">12</span><span class="special">>,</span> <span class="identifier">inch</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> <span class="identifier">foot</span><span class="special">;</span> <span class="comment">// 12 inchs</span> 503<span class="keyword">typedef</span> <span class="identifier">length</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_multiply</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">5280</span><span class="special">>,</span> <span class="identifier">foot</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> <span class="identifier">mile</span><span class="special">;</span> <span class="comment">// 5280 feet</span> 504</pre> 505<p> 506 Now we need a floating point-based definition of seconds: 507 </p> 508<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">seconds</span><span class="special">;</span> <span class="comment">// unity</span> 509</pre> 510<p> 511 We can even support sub-nanosecond durations: 512 </p> 513<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">pico</span></code></a><span class="special">></span> <span class="identifier">picosecond</span><span class="special">;</span> <span class="comment">// 10^-12 seconds</span> 514<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">femto</span></code></a><span class="special">></span> <span class="identifier">femtosecond</span><span class="special">;</span> <span class="comment">// 10^-15 seconds</span> 515<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">duration</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio_si_typedefs" title="SI typedefs"><code class="computeroutput"><span class="identifier">atto</span></code></a><span class="special">></span> <span class="identifier">attosecond</span><span class="special">;</span> <span class="comment">// 10^-18 seconds</span> 516</pre> 517<p> 518 Finally, we can write a proof-of-concept of an SI units library, hard-wired 519 for meters and floating point seconds, though it will accept other units: 520 </p> 521<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R2</span><span class="special">></span> 522<span class="keyword">class</span> <span class="identifier">quantity</span> 523<span class="special">{</span> 524 <span class="keyword">double</span> <span class="identifier">q_</span><span class="special">;</span> 525<span class="keyword">public</span><span class="special">:</span> 526 <span class="keyword">typedef</span> <span class="identifier">R1</span> <span class="identifier">time_dim</span><span class="special">;</span> 527 <span class="keyword">typedef</span> <span class="identifier">R2</span> <span class="identifier">distance_dim</span><span class="special">;</span> 528 <span class="identifier">quantity</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">{}</span> 529 530 <span class="keyword">double</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">q_</span><span class="special">;}</span> 531 <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">q</span><span class="special">)</span> <span class="special">{</span><span class="identifier">q_</span> <span class="special">=</span> <span class="identifier">q</span><span class="special">;}</span> 532<span class="special">};</span> 533 534<span class="keyword">template</span> <span class="special"><></span> 535<span class="keyword">class</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">></span> 536<span class="special">{</span> 537 <span class="keyword">double</span> <span class="identifier">q_</span><span class="special">;</span> 538<span class="keyword">public</span><span class="special">:</span> 539 <span class="identifier">quantity</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">{}</span> 540 <span class="identifier">quantity</span><span class="special">(</span><span class="identifier">seconds</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="identifier">d</span><span class="special">.</span><span class="identifier">count</span><span class="special">())</span> <span class="special">{}</span> <span class="comment">// note: only User1::seconds needed here</span> 541 542 <span class="keyword">double</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">q_</span><span class="special">;}</span> 543 <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">q</span><span class="special">)</span> <span class="special">{</span><span class="identifier">q_</span> <span class="special">=</span> <span class="identifier">q</span><span class="special">;}</span> 544<span class="special">};</span> 545 546<span class="keyword">template</span> <span class="special"><></span> 547<span class="keyword">class</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> 548<span class="special">{</span> 549 <span class="keyword">double</span> <span class="identifier">q_</span><span class="special">;</span> 550<span class="keyword">public</span><span class="special">:</span> 551 <span class="identifier">quantity</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">{}</span> 552 <span class="identifier">quantity</span><span class="special">(</span><span class="identifier">meter</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="identifier">d</span><span class="special">.</span><span class="identifier">count</span><span class="special">())</span> <span class="special">{}</span> <span class="comment">// note: only User1::meter needed here</span> 553 554 <span class="keyword">double</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">q_</span><span class="special">;}</span> 555 <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">q</span><span class="special">)</span> <span class="special">{</span><span class="identifier">q_</span> <span class="special">=</span> <span class="identifier">q</span><span class="special">;}</span> 556<span class="special">};</span> 557 558<span class="keyword">template</span> <span class="special"><></span> 559<span class="keyword">class</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">></span> 560<span class="special">{</span> 561 <span class="keyword">double</span> <span class="identifier">q_</span><span class="special">;</span> 562<span class="keyword">public</span><span class="special">:</span> 563 <span class="identifier">quantity</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">{}</span> 564 <span class="identifier">quantity</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">d</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">q_</span><span class="special">(</span><span class="identifier">d</span><span class="special">)</span> <span class="special">{}</span> 565 566 <span class="keyword">double</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">q_</span><span class="special">;}</span> 567 <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">q</span><span class="special">)</span> <span class="special">{</span><span class="identifier">q_</span> <span class="special">=</span> <span class="identifier">q</span><span class="special">;}</span> 568<span class="special">};</span> 569</pre> 570<p> 571 That allows us to create some useful SI-based unit types: 572 </p> 573<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">></span> <span class="identifier">Scalar</span><span class="special">;</span> 574<span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">></span> <span class="identifier">Time</span><span class="special">;</span> <span class="comment">// second</span> 575<span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> <span class="identifier">Distance</span><span class="special">;</span> <span class="comment">// meter</span> 576<span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><-</span><span class="number">1</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> <span class="identifier">Speed</span><span class="special">;</span> <span class="comment">// meter/second</span> 577<span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><-</span><span class="number">2</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">></span> <span class="identifier">Acceleration</span><span class="special">;</span> <span class="comment">// meter/second^2</span> 578</pre> 579<p> 580 To make quantity useful, we need to be able to do arithmetic: 581 </p> 582<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R3</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R4</span><span class="special">></span> 583<span class="identifier">quantity</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R3</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> 584 <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">R2</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> 585<span class="keyword">operator</span><span class="special">/(</span><span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R3</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>&</span> <span class="identifier">y</span><span class="special">)</span> 586<span class="special">{</span> 587 <span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R3</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> 588 <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">R2</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> <span class="identifier">R</span><span class="special">;</span> 589 <span class="identifier">R</span> <span class="identifier">r</span><span class="special">;</span> 590 <span class="identifier">r</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">/</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span> 591 <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span> 592<span class="special">}</span> 593 594<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R3</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R4</span><span class="special">></span> 595<span class="identifier">quantity</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_add</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R3</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> 596 <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_add</span><span class="special"><</span><span class="identifier">R2</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> 597<span class="keyword">operator</span><span class="special">*(</span><span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R3</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>&</span> <span class="identifier">y</span><span class="special">)</span> 598<span class="special">{</span> 599 <span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_add</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R3</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> 600 <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_add</span><span class="special"><</span><span class="identifier">R2</span><span class="special">,</span> <span class="identifier">R4</span><span class="special">>::</span><span class="identifier">type</span><span class="special">></span> <span class="identifier">R</span><span class="special">;</span> 601 <span class="identifier">R</span> <span class="identifier">r</span><span class="special">;</span> 602 <span class="identifier">r</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">*</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span> 603 <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span> 604<span class="special">}</span> 605 606<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R2</span><span class="special">></span> 607<span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">></span> 608<span class="keyword">operator</span><span class="special">+(</span><span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">y</span><span class="special">)</span> 609<span class="special">{</span> 610 <span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">></span> <span class="identifier">R</span><span class="special">;</span> 611 <span class="identifier">R</span> <span class="identifier">r</span><span class="special">;</span> 612 <span class="identifier">r</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span> 613 <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span> 614<span class="special">}</span> 615 616<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">R1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R2</span><span class="special">></span> 617<span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">></span> 618<span class="keyword">operator</span><span class="special">-(</span><span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>&</span> <span class="identifier">y</span><span class="special">)</span> 619<span class="special">{</span> 620 <span class="keyword">typedef</span> <span class="identifier">quantity</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">></span> <span class="identifier">R</span><span class="special">;</span> 621 <span class="identifier">R</span> <span class="identifier">r</span><span class="special">;</span> 622 <span class="identifier">r</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">-</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span> 623 <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span> 624<span class="special">}</span> 625</pre> 626<p> 627 With all of the foregoing scaffolding, we can now write an exemplar of 628 a type-safe physics function: 629 </p> 630<pre class="programlisting"><span class="identifier">Distance</span> 631<span class="identifier">compute_distance</span><span class="special">(</span><span class="identifier">Speed</span> <span class="identifier">v0</span><span class="special">,</span> <span class="identifier">Time</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">Acceleration</span> <span class="identifier">a</span><span class="special">)</span> 632<span class="special">{</span> 633 <span class="keyword">return</span> <span class="identifier">v0</span> <span class="special">*</span> <span class="identifier">t</span> <span class="special">+</span> <span class="identifier">Scalar</span><span class="special">(.</span><span class="number">5</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">a</span> <span class="special">*</span> <span class="identifier">t</span> <span class="special">*</span> <span class="identifier">t</span><span class="special">;</span> <span class="comment">// if a units mistake is made here it won't compile</span> 634<span class="special">}</span> 635</pre> 636<p> 637 Finally, we can exercise what we've created, even using custom time durations 638 (<code class="computeroutput"><span class="identifier">User1</span><span class="special">::</span><span class="identifier">seconds</span></code>) as well as Boost time durations 639 (<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">hours</span></code>). The input can be in arbitrary, 640 though type-safe, units, the output is always in SI units. (A complete 641 Units library would support other units, of course.) 642 </p> 643<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 644<span class="special">{</span> 645 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">8</span><span class="special">,</span> <span class="identifier">BOOST_INTMAX_C</span><span class="special">(</span><span class="number">0</span><span class="identifier">x7FFFFFFFD</span><span class="special">)></span> <span class="identifier">R1</span><span class="special">;</span> 646 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">3</span><span class="special">,</span> <span class="identifier">BOOST_INTMAX_C</span><span class="special">(</span><span class="number">0</span><span class="identifier">x7FFFFFFFD</span><span class="special">)></span> <span class="identifier">R2</span><span class="special">;</span> 647 <span class="keyword">typedef</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">quantity</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> 648 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">1</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><a class="link" href="reference.html#ratio.reference.std.ratio_hpp.ratio" title="Class Template ratio<>"><code class="computeroutput"><span class="identifier">ratio</span></code></a><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="identifier">RR</span><span class="special">;</span> 649 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ratio_subtract</span><span class="special"><</span><span class="identifier">R1</span><span class="special">,</span> <span class="identifier">R2</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">RS</span><span class="special">;</span> 650 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">RS</span><span class="special">::</span><span class="identifier">num</span> <span class="special"><<</span> <span class="char">'/'</span> <span class="special"><<</span> <span class="identifier">RS</span><span class="special">::</span><span class="identifier">den</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 651 652 653 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"*************\n"</span><span class="special">;</span> 654 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"* testUser1 *\n"</span><span class="special">;</span> 655 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"*************\n"</span><span class="special">;</span> 656 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Distance</span> <span class="identifier">d</span><span class="special">(</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span><span class="special">(</span><span class="number">110</span><span class="special">)</span> <span class="special">);</span> 657 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Time</span> <span class="identifier">t</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><code class="computeroutput"><span class="identifier">hours</span></code><span class="special">(</span><span class="number">2</span><span class="special">)</span> <span class="special">);</span> 658 659 <span class="identifier">RR</span> <span class="identifier">r</span><span class="special">=</span><span class="identifier">d</span> <span class="special">/</span> <span class="identifier">t</span><span class="special">;</span> 660 <span class="comment">//r.set(d.get() / t.get());</span> 661 662 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Speed</span> <span class="identifier">rc</span><span class="special">=</span> <span class="identifier">r</span><span class="special">;</span> 663 664 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Speed</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">d</span> <span class="special">/</span> <span class="identifier">t</span><span class="special">;</span> 665 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Speed = "</span> <span class="special"><<</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" meters/sec\n"</span><span class="special">;</span> 666 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Acceleration</span> <span class="identifier">a</span> <span class="special">=</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Distance</span><span class="special">(</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">foot</span><span class="special">(</span><span class="number">32.2</span><span class="special">)</span> <span class="special">)</span> <span class="special">/</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Time</span><span class="special">()</span> <span class="special">/</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Time</span><span class="special">();</span> 667 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Acceleration = "</span> <span class="special"><<</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" meters/sec^2\n"</span><span class="special">;</span> 668 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Distance</span> <span class="identifier">df</span> <span class="special">=</span> <span class="identifier">compute_distance</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">Time</span><span class="special">(</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">0.5</span><span class="special">)</span> <span class="special">),</span> <span class="identifier">a</span><span class="special">);</span> 669 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Distance = "</span> <span class="special"><<</span> <span class="identifier">df</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" meters\n"</span><span class="special">;</span> 670 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"There are "</span> 671 <span class="special"><<</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span><span class="special">::</span><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">den</span> <span class="special"><<</span> <span class="char">'/'</span> <span class="special"><<</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span><span class="special">::</span><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">num</span> <span class="special"><<</span> <span class="string">" miles/meter"</span><span class="special">;</span> 672 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">meter</span> <span class="identifier">mt</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 673 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span> <span class="identifier">mi</span> <span class="special">=</span> <span class="identifier">mt</span><span class="special">;</span> 674 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">" which is approximately "</span> <span class="special"><<</span> <span class="identifier">mi</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 675 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"There are "</span> 676 <span class="special"><<</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span><span class="special">::</span><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">num</span> <span class="special"><<</span> <span class="char">'/'</span> <span class="special"><<</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">mile</span><span class="special">::</span><span class="identifier">ratio</span><span class="special">::</span><span class="identifier">den</span> <span class="special"><<</span> <span class="string">" meters/mile"</span><span class="special">;</span> 677 <span class="identifier">mi</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 678 <span class="identifier">mt</span> <span class="special">=</span> <span class="identifier">mi</span><span class="special">;</span> 679 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">" which is approximately "</span> <span class="special"><<</span> <span class="identifier">mt</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> 680 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">attosecond</span> <span class="identifier">as</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> 681 <span class="identifier">User1</span><span class="special">::</span><span class="identifier">seconds</span> <span class="identifier">sec</span> <span class="special">=</span> <span class="identifier">as</span><span class="special">;</span> 682 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"1 attosecond is "</span> <span class="special"><<</span> <span class="identifier">sec</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" seconds\n"</span><span class="special">;</span> 683 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"sec = as; // compiles\n"</span><span class="special">;</span> 684 <span class="identifier">sec</span> <span class="special">=</span> <span class="identifier">User1</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> 685 <span class="identifier">as</span> <span class="special">=</span> <span class="identifier">sec</span><span class="special">;</span> 686 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"1 second is "</span> <span class="special"><<</span> <span class="identifier">as</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" attoseconds\n"</span><span class="special">;</span> 687 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"as = sec; // compiles\n"</span><span class="special">;</span> 688 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 689 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 690<span class="special">}</span> 691</pre> 692<p> 693 <span class="emphasis"><em>See the source file <a href="../../../libs/ratio/example/si_physics.cpp" target="_top">example/si_physics.cpp</a></em></span> 694 </p> 695</div> 696</div> 697<div class="section"> 698<div class="titlepage"><div><div><h3 class="title"> 699<a name="ratio.users_guide.ext_references"></a><a class="link" href="users_guide.html#ratio.users_guide.ext_references" title="External Resources">External Resources</a> 700</h3></div></div></div> 701<div class="variablelist"> 702<p class="title"><b></b></p> 703<dl class="variablelist"> 704<dt><span class="term"><a href="http://www.open-std.org/jtc1/sc22/wg21" target="_top"><span class="bold"><strong>C++ 705 Standards Committee's current Working Paper</strong></span></a></span></dt> 706<dd><p> 707 The most authoritative reference material for the library is the C++ 708 Standards Committee's current Working Paper (WP). 20.6 Compile-time 709 rational arithmetic "ratio" 710 </p></dd> 711<dt><span class="term"><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm" target="_top"><span class="bold"><strong>N2661 - A Foundation to Sleep On</strong></span></a></span></dt> 712<dd><p> 713 From Howard E. Hinnant, Walter E. Brown, Jeff Garland and Marc Paterno. 714 Is very informative and provides motivation for key design decisions 715 </p></dd> 716<dt><span class="term"><a href="http://lwg.github.com/issues/lwg-defects.html#1281" target="_top"><span class="bold"><strong>LWG 1281. CopyConstruction and Assignment between ratios having 717 the same normalized form</strong></span></a></span></dt> 718<dd><p> 719 From Vicente Juan Botet Escriba. 720 </p></dd> 721</dl> 722</div> 723</div> 724</div> 725<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 726<td align="left"></td> 727<td align="right"><div class="copyright-footer">Copyright © 2008 Howard Hinnant<br>Copyright © 2006, 2008 Beman Dawes<br>Copyright © 2009-2012 Vicente 728 J. Botet Escriba<p> 729 Distributed under the Boost Software License, Version 1.0. (See accompanying 730 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 731 </p> 732</div></td> 733</tr></table> 734<hr> 735<div class="spirit-nav"> 736<a accesskey="p" href="../ratio.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ratio.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 737</div> 738</body> 739</html> 740