]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/units/example/tutorial.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / units / example / tutorial.cpp
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
3 //
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10
11 /**
12 \file tutorial.cpp
13
14 \brief Basic tutorial using SI units.
15
16 \details
17 Tutorial
18 Defines a function that computes the work, in joules,
19 done by exerting a force in newtons over a specified distance
20 in meters and outputs the result to std::cout.
21
22 Also code for computing the complex impedance
23 using std::complex<double> as the value type.
24
25 Output:
26 @verbatim
27 //[tutorial_output
28 F = 2 N
29 dx = 2 m
30 E = 4 J
31
32 V = (12.5,0) V
33 I = (3,4) A
34 Z = (1.5,-2) Ohm
35 I*Z = (12.5,0) V
36 I*Z == V? true
37 //]
38 @endverbatim
39 */
40
41 //[tutorial_code
42 #include <complex>
43 #include <iostream>
44
45 #include <boost/typeof/std/complex.hpp>
46
47 #include <boost/units/systems/si/energy.hpp>
48 #include <boost/units/systems/si/force.hpp>
49 #include <boost/units/systems/si/length.hpp>
50 #include <boost/units/systems/si/electric_potential.hpp>
51 #include <boost/units/systems/si/current.hpp>
52 #include <boost/units/systems/si/resistance.hpp>
53 #include <boost/units/systems/si/io.hpp>
54
55 using namespace boost::units;
56 using namespace boost::units::si;
57
58 quantity<energy>
59 work(const quantity<force>& F, const quantity<length>& dx)
60 {
61 return F * dx; // Defines the relation: work = force * distance.
62 }
63
64 int main()
65 {
66 /// Test calculation of work.
67 quantity<force> F(2.0 * newton); // Define a quantity of force.
68 quantity<length> dx(2.0 * meter); // and a distance,
69 quantity<energy> E(work(F,dx)); // and calculate the work done.
70
71 std::cout << "F = " << F << std::endl
72 << "dx = " << dx << std::endl
73 << "E = " << E << std::endl
74 << std::endl;
75
76 /// Test and check complex quantities.
77 typedef std::complex<double> complex_type; // double real and imaginary parts.
78
79 // Define some complex electrical quantities.
80 quantity<electric_potential, complex_type> v = complex_type(12.5, 0.0) * volts;
81 quantity<current, complex_type> i = complex_type(3.0, 4.0) * amperes;
82 quantity<resistance, complex_type> z = complex_type(1.5, -2.0) * ohms;
83
84 std::cout << "V = " << v << std::endl
85 << "I = " << i << std::endl
86 << "Z = " << z << std::endl
87 // Calculate from Ohm's law voltage = current * resistance.
88 << "I * Z = " << i * z << std::endl
89 // Check defined V is equal to calculated.
90 << "I * Z == V? " << std::boolalpha << (i * z == v) << std::endl
91 << std::endl;
92 return 0;
93 }
94 //]