forked from boostorg/units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantity.cpp
128 lines (110 loc) · 4.66 KB
/
quantity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Boost.Units - A C++ library for zero-overhead dimensional analysis and
// unit/quantity manipulation and conversion
//
// Copyright (C) 2003-2008 Matthias Christian Schabel
// Copyright (C) 2008 Steven Watanabe
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/**
\file
\brief quantity.cpp
\details
Test quantity algebra.
Output:
@verbatim
//[quantity_output_double
L = 2 m
L+L = 4 m
L-L = 0 m
L*L = 4 m^2
L/L = 1 dimensionless
L*meter = 2 m^2
kilograms*(L/seconds)*(L/seconds) = 4 m^2 kg s^-2
kilograms*(L/seconds)^2 = 4 m^2 kg s^-2
L^3 = 8 m^3
L^(3/2) = 2.82843 m^(3/2)
2vL = 1.41421 m^(1/2)
(3/2)vL = 1.5874 m^(2/3)
//]
//[quantity_output_complex
L = (3,4) m
L+L = (6,8) m
L-L = (0,0) m
L*L = (-7,24) m^2
L/L = (1,0) dimensionless
L*meter = (3,4) m^2
kilograms*(L/seconds)*(L/seconds) = (-7,24) m^2 kg s^-2
kilograms*(L/seconds)^2 = (-7,24) m^2 kg s^-2
L^3 = (-117,44) m^3
L^(3/2) = (2,11) m^(3/2)
2vL = (2,1) m^(1/2)
(3/2)vL = (2.38285,1.69466) m^(2/3)
//]
@endverbatim
**/
#include <complex>
#include <iostream>
#include <boost/mpl/list.hpp>
#include <boost/typeof/std/complex.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include "test_system.hpp"
int main(void)
{
using namespace boost::units;
using namespace boost::units::test;
{
//[quantity_snippet_1
quantity<length> L = 2.0*meters; // quantity of length
quantity<energy> E = kilograms*pow<2>(L/seconds); // quantity of energy
//]
std::cout << "L = " << L << std::endl
<< "L+L = " << L+L << std::endl
<< "L-L = " << L-L << std::endl
<< "L*L = " << L*L << std::endl
<< "L/L = " << L/L << std::endl
<< "L*meter = " << L*meter << std::endl
<< "kilograms*(L/seconds)*(L/seconds) = "
<< kilograms*(L/seconds)*(L/seconds) << std::endl
<< "kilograms*(L/seconds)^2 = "
<< kilograms*pow<2>(L/seconds) << std::endl
<< "L^3 = "
<< pow<3>(L) << std::endl
<< "L^(3/2) = "
<< pow<static_rational<3,2> >(L) << std::endl
<< "2vL = "
<< root<2>(L) << std::endl
<< "(3/2)vL = "
<< root<static_rational<3,2> >(L) << std::endl
<< std::endl;
}
{
//[quantity_snippet_2
quantity<length,std::complex<double> > L(std::complex<double>(3.0,4.0)*meters);
quantity<energy,std::complex<double> > E(kilograms*pow<2>(L/seconds));
//]
std::cout << "L = " << L << std::endl
<< "L+L = " << L+L << std::endl
<< "L-L = " << L-L << std::endl
<< "L*L = " << L*L << std::endl
<< "L/L = " << L/L << std::endl
<< "L*meter = " << L*meter << std::endl
<< "kilograms*(L/seconds)*(L/seconds) = "
<< kilograms*(L/seconds)*(L/seconds) << std::endl
<< "kilograms*(L/seconds)^2 = "
<< kilograms*pow<2>(L/seconds) << std::endl
<< "L^3 = "
<< pow<3>(L) << std::endl
<< "L^(3/2) = "
<< pow<static_rational<3,2> >(L) << std::endl
<< "2vL = "
<< root<2>(L) << std::endl
<< "(3/2)vL = "
<< root<static_rational<3,2> >(L) << std::endl
<< std::endl;
}
return 0;
}