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
13
14 \brief conversion.cpp
15
16 \details
17 Test explicit and implicit unit conversion.
18
19 Output:
20 @verbatim
21
22 //[conversion_output_1
23 L1 = 2 m
24 L2 = 2 m
25 L3 = 2 m
26 L4 = 200 cm
27 L5 = 5 m
28 L6 = 4 m
29 L7 = 200 cm
30 //]
31
32 //[conversion_output_2
33 volume (m^3) = 1 m^3
34 volume (cm^3) = 1e+06 cm^3
35 volume (m^3) = 1 m^3
36
37 energy (joules) = 1 J
38 energy (ergs) = 1e+07 erg
39 energy (joules) = 1 J
40
41 velocity (2 m/s) = 2 m s^-1
42 velocity (2 cm/s) = 0.02 m s^-1
43 //]
44
45 @endverbatim
46 **/
47
48 #include <iostream>
49
50 #include <boost/units/io.hpp>
51 #include <boost/units/pow.hpp>
52 #include <boost/units/systems/cgs.hpp>
53 #include <boost/units/systems/cgs/io.hpp>
54 #include <boost/units/systems/si.hpp>
55 #include <boost/units/systems/si/io.hpp>
56
57 using namespace boost::units;
58
main()59 int main()
60 {
61 // test quantity_cast
62 {
63 // implicit value_type conversions
64 //[conversion_snippet_1
65 quantity<si::length> L1 = quantity<si::length,int>(int(2.5)*si::meters);
66 quantity<si::length,int> L2(quantity<si::length,double>(2.5*si::meters));
67 //]
68
69 //[conversion_snippet_3
70 quantity<si::length,int> L3 = static_cast<quantity<si::length,int> >(L1);
71 //]
72
73 //[conversion_snippet_4
74 quantity<cgs::length> L4 = static_cast<quantity<cgs::length> >(L1);
75 //]
76
77 quantity<si::length,int> L5(4*si::meters),
78 L6(5*si::meters);
79 quantity<cgs::length> L7(L1);
80
81 swap(L5,L6);
82
83 std::cout << "L1 = " << L1 << std::endl
84 << "L2 = " << L2 << std::endl
85 << "L3 = " << L3 << std::endl
86 << "L4 = " << L4 << std::endl
87 << "L5 = " << L5 << std::endl
88 << "L6 = " << L6 << std::endl
89 << "L7 = " << L7 << std::endl
90 << std::endl;
91 }
92
93 // test explicit unit system conversion
94 {
95 //[conversion_snippet_5
96 quantity<si::volume> vs(1.0*pow<3>(si::meter));
97 quantity<cgs::volume> vc(vs);
98 quantity<si::volume> vs2(vc);
99
100 quantity<si::energy> es(1.0*si::joule);
101 quantity<cgs::energy> ec(es);
102 quantity<si::energy> es2(ec);
103
104 quantity<si::velocity> v1 = 2.0*si::meters/si::second,
105 v2(2.0*cgs::centimeters/cgs::second);
106 //]
107
108 std::cout << "volume (m^3) = " << vs << std::endl
109 << "volume (cm^3) = " << vc << std::endl
110 << "volume (m^3) = " << vs2 << std::endl
111 << std::endl;
112
113 std::cout << "energy (joules) = " << es << std::endl
114 << "energy (ergs) = " << ec << std::endl
115 << "energy (joules) = " << es2 << std::endl
116 << std::endl;
117
118 std::cout << "velocity (2 m/s) = " << v1 << std::endl
119 << "velocity (2 cm/s) = " << v2 << std::endl
120 << std::endl;
121 }
122
123 return 0;
124 }
125