]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/units/include/boost/units/systems/detail/constants.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / units / include / boost / units / systems / detail / constants.hpp
CommitLineData
7c673cae
FG
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#ifndef BOOST_UNITS_CONSTANTS_HPP
12#define BOOST_UNITS_CONSTANTS_HPP
13
14#include <boost/config/no_tr1/cmath.hpp>
15#include <iosfwd>
16#include <iomanip>
17
18#include <boost/io/ios_state.hpp>
19
20#include <boost/units/static_constant.hpp>
21#include <boost/units/units_fwd.hpp>
22#include <boost/units/operators.hpp>
23#include <boost/units/static_rational.hpp>
24#include <boost/units/detail/one.hpp>
25
26namespace boost {
27
28namespace units {
29
30template<class Base>
31struct constant
32{
33 typedef typename Base::value_type value_type;
34 operator value_type() const { return Base().value(); }
35 value_type value() const { return Base().value(); }
36 value_type uncertainty() const { return Base().uncertainty(); }
37 value_type lower_bound() const { return Base().lower_bound(); }
38 value_type upper_bound() const { return Base().upper_bound(); }
39};
40
41template<class Base>
42struct physical_constant
43{
44 typedef typename Base::value_type value_type;
45 operator value_type() const { return Base().value(); }
46 value_type value() const { return Base().value(); }
47 value_type uncertainty() const { return Base().uncertainty(); }
48 value_type lower_bound() const { return Base().lower_bound(); }
49 value_type upper_bound() const { return Base().upper_bound(); }
50};
51
52#define BOOST_UNITS_DEFINE_HELPER(name, symbol, template_name) \
53 \
54template<class T, class Arg1, class Arg2> \
55struct name ## _typeof_helper<constant<T>, template_name<Arg1, Arg2> >\
56{ \
57 typedef typename name ## _typeof_helper<typename T::value_type, template_name<Arg1, Arg2> >::type type;\
58}; \
59 \
60template<class T, class Arg1, class Arg2> \
61struct name ## _typeof_helper<template_name<Arg1, Arg2>, constant<T> >\
62{ \
63 typedef typename name ## _typeof_helper<template_name<Arg1, Arg2>, typename T::value_type>::type type;\
64}; \
65 \
66template<class T, class Arg1, class Arg2> \
67typename name ## _typeof_helper<typename T::value_type, template_name<Arg1, Arg2> >::type \
68operator symbol(const constant<T>& t, const template_name<Arg1, Arg2>& u)\
69{ \
70 return(t.value() symbol u); \
71} \
72 \
73template<class T, class Arg1, class Arg2> \
74typename name ## _typeof_helper<template_name<Arg1, Arg2>, typename T::value_type>::type \
75operator symbol(const template_name<Arg1, Arg2>& u, const constant<T>& t)\
76{ \
77 return(u symbol t.value()); \
78}
79
80BOOST_UNITS_DEFINE_HELPER(add, +, unit)
81BOOST_UNITS_DEFINE_HELPER(add, +, quantity)
82BOOST_UNITS_DEFINE_HELPER(subtract, -, unit)
83BOOST_UNITS_DEFINE_HELPER(subtract, -, quantity)
84BOOST_UNITS_DEFINE_HELPER(multiply, *, unit)
85BOOST_UNITS_DEFINE_HELPER(multiply, *, quantity)
86BOOST_UNITS_DEFINE_HELPER(divide, /, unit)
87BOOST_UNITS_DEFINE_HELPER(divide, /, quantity)
88
89#undef BOOST_UNITS_DEFINE_HELPER
90
91#define BOOST_UNITS_DEFINE_HELPER(name, symbol) \
92 \
93template<class T1, class T2> \
94struct name ## _typeof_helper<constant<T1>, constant<T2> > \
95{ \
96 typedef typename name ## _typeof_helper<typename T1::value_type, typename T2::value_type>::type type;\
97}; \
98 \
99template<class T1, class T2> \
100typename name ## _typeof_helper<typename T1::value_type, typename T2::value_type>::type \
101operator symbol(const constant<T1>& t, const constant<T2>& u) \
102{ \
103 return(t.value() symbol u.value()); \
104} \
105 \
106template<class T1, class T2> \
107struct name ## _typeof_helper<constant<T1>, T2> \
108{ \
109 typedef typename name ## _typeof_helper<typename T1::value_type, T2>::type type;\
110}; \
111 \
112template<class T1, class T2> \
113struct name ## _typeof_helper<T1, constant<T2> > \
114{ \
115 typedef typename name ## _typeof_helper<T1, typename T2::value_type>::type type;\
116}; \
117 \
118template<class T1, class T2> \
119typename name ## _typeof_helper<typename T1::value_type, T2>::type \
120operator symbol(const constant<T1>& t, const T2& u) \
121{ \
122 return(t.value() symbol u); \
123} \
124 \
125template<class T1, class T2> \
126typename name ## _typeof_helper<T1, typename T2::value_type>::type \
127operator symbol(const T1& t, const constant<T2>& u) \
128{ \
129 return(t symbol u.value()); \
130}
131
132BOOST_UNITS_DEFINE_HELPER(add, +)
133BOOST_UNITS_DEFINE_HELPER(subtract, -)
134BOOST_UNITS_DEFINE_HELPER(multiply, *)
135BOOST_UNITS_DEFINE_HELPER(divide, /)
136
137#undef BOOST_UNITS_DEFINE_HELPER
138
139#define BOOST_UNITS_DEFINE_HELPER(name, symbol) \
140 \
141template<class T1> \
142struct name ## _typeof_helper<constant<T1>, one> \
143{ \
144 typedef typename name ## _typeof_helper<typename T1::value_type, one>::type type;\
145}; \
146 \
147template<class T2> \
148struct name ## _typeof_helper<one, constant<T2> > \
149{ \
150 typedef typename name ## _typeof_helper<one, typename T2::value_type>::type type;\
151}; \
152 \
153template<class T1> \
154typename name ## _typeof_helper<typename T1::value_type, one>::type \
155operator symbol(const constant<T1>& t, const one& u) \
156{ \
157 return(t.value() symbol u); \
158} \
159 \
160template<class T2> \
161typename name ## _typeof_helper<one, typename T2::value_type>::type \
162operator symbol(const one& t, const constant<T2>& u) \
163{ \
164 return(t symbol u.value()); \
165}
166
167BOOST_UNITS_DEFINE_HELPER(multiply, *)
168BOOST_UNITS_DEFINE_HELPER(divide, /)
169
170#undef BOOST_UNITS_DEFINE_HELPER
171
172template<class T1, long N, long D>
173struct power_typeof_helper<constant<T1>, static_rational<N,D> >
174{
175 typedef power_typeof_helper<typename T1::value_type, static_rational<N,D> > base;
176 typedef typename base::type type;
177 static type value(const constant<T1>& arg)
178 {
179 return base::value(arg.value());
180 }
181};
182
183#define BOOST_UNITS_DEFINE_HELPER(name, symbol) \
184 \
185template<class T1, class E> \
186struct name ## _typeof_helper<constant<T1> > \
187{ \
188 typedef typename name ## _typeof_helper<typename T1::value_type, E>::type type;\
189}; \
190 \
191template<class T1> \
192typename name ## _typeof_helper<typename T1::value_type, one>::type \
193operator symbol(const constant<T1>& t, const one& u) \
194{ \
195 return(t.value() symbol u); \
196} \
197 \
198template<class T2> \
199typename name ## _typeof_helper<one, typename T2::value_type>::type \
200operator symbol(const one& t, const constant<T2>& u) \
201{ \
202 return(t symbol u.value()); \
203}
204
205#define BOOST_UNITS_PHYSICAL_CONSTANT(name, type, value_, uncertainty_) \
206struct name ## _t { \
207 typedef type value_type; \
208 operator value_type() const { return value_; } \
209 value_type value() const { return value_; } \
210 value_type uncertainty() const { return uncertainty_; } \
211 value_type lower_bound() const { return value_-uncertainty_; } \
212 value_type upper_bound() const { return value_+uncertainty_; } \
213}; \
214BOOST_UNITS_STATIC_CONSTANT(name, boost::units::constant<boost::units::physical_constant<name ## _t> >) = { }
215
216// stream output
217template<class Char, class Traits, class Y>
218inline
219std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>& os,const physical_constant<Y>& val)
220{
221 boost::io::ios_precision_saver precision_saver(os);
222 //boost::io::ios_width_saver width_saver(os);
223 boost::io::ios_flags_saver flags_saver(os);
224
225 //os << std::setw(21);
226 typedef typename Y::value_type value_type;
227
228 if (val.uncertainty() > value_type())
229 {
230 const double relative_uncertainty = std::abs(val.uncertainty()/val.value());
231
232 const double exponent = std::log10(relative_uncertainty);
233 const long digits_of_precision = static_cast<long>(std::ceil(std::abs(exponent)))+3;
234
235 // should try to replicate NIST CODATA syntax
236 os << std::setprecision(digits_of_precision)
237 //<< std::setw(digits_of_precision+8)
238 //<< std::scientific
239 << val.value();
240// << long(10*(relative_uncertainty/std::pow(Y(10),Y(exponent))));
241
242 os << " (rel. unc. = "
243 << std::setprecision(1)
244 //<< std::setw(7)
245 << std::scientific
246 << relative_uncertainty << ")";
247 }
248 else
249 {
250 os << val.value() << " (exact)";
251 }
252
253 return os;
254}
255
256// stream output
257template<class Char, class Traits, class Y>
258inline
259std::basic_ostream<Char,Traits>& operator<<(std::basic_ostream<Char,Traits>& os,const constant<Y>&)
260{
261 os << Y();
262 return os;
263}
264
265} // namespace units
266
267} // namespace boost
268
269#endif // BOOST_UNITS_CONSTANTS_HPP