]>
git.proxmox.com Git - ceph.git/blob - 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
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
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)
14 \brief Basic tutorial using SI units.
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.
22 Also code for computing the complex impedance
23 using std::complex<double> as the value type.
45 #include <boost/typeof/std/complex.hpp>
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>
55 using namespace boost::units
;
56 using namespace boost::units::si
;
59 work(const quantity
<force
>& F
, const quantity
<length
>& dx
)
61 return F
* dx
; // Defines the relation: work = force * distance.
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.
71 std::cout
<< "F = " << F
<< std::endl
72 << "dx = " << dx
<< std::endl
73 << "E = " << E
<< std::endl
76 /// Test and check complex quantities.
77 typedef std::complex<double> complex_type
; // double real and imaginary parts.
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
;
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