measurement.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #ifndef BOOST_UNITS_MEASUREMENT_HPP
  11. #define BOOST_UNITS_MEASUREMENT_HPP
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <iomanip>
  15. #include <iostream>
  16. #include <boost/io/ios_state.hpp>
  17. #include <boost/units/static_rational.hpp>
  18. namespace boost {
  19. namespace units {
  20. namespace sqr_namespace /**/ {
  21. template<class Y>
  22. constexpr
  23. Y sqr(Y val)
  24. { return val*val; }
  25. } // namespace
  26. using sqr_namespace::sqr;
  27. template<class Y>
  28. class measurement
  29. {
  30. public:
  31. typedef measurement<Y> this_type;
  32. typedef Y value_type;
  33. constexpr measurement(const value_type& val = value_type(),
  34. const value_type& err = value_type()) :
  35. value_(val),
  36. uncertainty_(std::abs(err))
  37. { }
  38. constexpr measurement(const this_type& source) :
  39. value_(source.value_),
  40. uncertainty_(source.uncertainty_)
  41. { }
  42. //~measurement() { }
  43. constexpr this_type& operator=(const this_type& source)
  44. {
  45. if (this == &source) return *this;
  46. value_ = source.value_;
  47. uncertainty_ = source.uncertainty_;
  48. return *this;
  49. }
  50. constexpr operator value_type() const { return value_; }
  51. constexpr value_type value() const { return value_; }
  52. constexpr value_type uncertainty() const { return uncertainty_; }
  53. constexpr value_type lower_bound() const { return value_-uncertainty_; }
  54. constexpr value_type upper_bound() const { return value_+uncertainty_; }
  55. constexpr this_type& operator+=(const value_type& val)
  56. {
  57. value_ += val;
  58. return *this;
  59. }
  60. constexpr this_type& operator-=(const value_type& val)
  61. {
  62. value_ -= val;
  63. return *this;
  64. }
  65. constexpr this_type& operator*=(const value_type& val)
  66. {
  67. value_ *= val;
  68. uncertainty_ *= val;
  69. return *this;
  70. }
  71. constexpr this_type& operator/=(const value_type& val)
  72. {
  73. value_ /= val;
  74. uncertainty_ /= val;
  75. return *this;
  76. }
  77. constexpr this_type& operator+=(const this_type& /*source*/);
  78. constexpr this_type& operator-=(const this_type& /*source*/);
  79. constexpr this_type& operator*=(const this_type& /*source*/);
  80. constexpr this_type& operator/=(const this_type& /*source*/);
  81. private:
  82. value_type value_,
  83. uncertainty_;
  84. };
  85. }
  86. }
  87. #if BOOST_UNITS_HAS_BOOST_TYPEOF
  88. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::measurement, 1)
  89. #endif
  90. namespace boost {
  91. namespace units {
  92. template<class Y>
  93. inline
  94. constexpr
  95. measurement<Y>&
  96. measurement<Y>::operator+=(const this_type& source)
  97. {
  98. uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_));
  99. value_ += source.value_;
  100. return *this;
  101. }
  102. template<class Y>
  103. inline
  104. constexpr
  105. measurement<Y>&
  106. measurement<Y>::operator-=(const this_type& source)
  107. {
  108. uncertainty_ = std::sqrt(sqr(uncertainty_)+sqr(source.uncertainty_));
  109. value_ -= source.value_;
  110. return *this;
  111. }
  112. template<class Y>
  113. inline
  114. constexpr
  115. measurement<Y>&
  116. measurement<Y>::operator*=(const this_type& source)
  117. {
  118. uncertainty_ = (value_*source.value_)*
  119. std::sqrt(sqr(uncertainty_/value_)+
  120. sqr(source.uncertainty_/source.value_));
  121. value_ *= source.value_;
  122. return *this;
  123. }
  124. template<class Y>
  125. inline
  126. constexpr
  127. measurement<Y>&
  128. measurement<Y>::operator/=(const this_type& source)
  129. {
  130. uncertainty_ = (value_/source.value_)*
  131. std::sqrt(sqr(uncertainty_/value_)+
  132. sqr(source.uncertainty_/source.value_));
  133. value_ /= source.value_;
  134. return *this;
  135. }
  136. // value_type op measurement
  137. template<class Y>
  138. inline
  139. constexpr
  140. measurement<Y>
  141. operator+(Y lhs,const measurement<Y>& rhs)
  142. {
  143. return (measurement<Y>(lhs,Y(0))+=rhs);
  144. }
  145. template<class Y>
  146. inline
  147. constexpr
  148. measurement<Y>
  149. operator-(Y lhs,const measurement<Y>& rhs)
  150. {
  151. return (measurement<Y>(lhs,Y(0))-=rhs);
  152. }
  153. template<class Y>
  154. inline
  155. constexpr
  156. measurement<Y>
  157. operator*(Y lhs,const measurement<Y>& rhs)
  158. {
  159. return (measurement<Y>(lhs,Y(0))*=rhs);
  160. }
  161. template<class Y>
  162. inline
  163. constexpr
  164. measurement<Y>
  165. operator/(Y lhs,const measurement<Y>& rhs)
  166. {
  167. return (measurement<Y>(lhs,Y(0))/=rhs);
  168. }
  169. // measurement op value_type
  170. template<class Y>
  171. inline
  172. constexpr
  173. measurement<Y>
  174. operator+(const measurement<Y>& lhs,Y rhs)
  175. {
  176. return (measurement<Y>(lhs)+=measurement<Y>(rhs,Y(0)));
  177. }
  178. template<class Y>
  179. inline
  180. constexpr
  181. measurement<Y>
  182. operator-(const measurement<Y>& lhs,Y rhs)
  183. {
  184. return (measurement<Y>(lhs)-=measurement<Y>(rhs,Y(0)));
  185. }
  186. template<class Y>
  187. inline
  188. constexpr
  189. measurement<Y>
  190. operator*(const measurement<Y>& lhs,Y rhs)
  191. {
  192. return (measurement<Y>(lhs)*=measurement<Y>(rhs,Y(0)));
  193. }
  194. template<class Y>
  195. inline
  196. constexpr
  197. measurement<Y>
  198. operator/(const measurement<Y>& lhs,Y rhs)
  199. {
  200. return (measurement<Y>(lhs)/=measurement<Y>(rhs,Y(0)));
  201. }
  202. // measurement op measurement
  203. template<class Y>
  204. inline
  205. constexpr
  206. measurement<Y>
  207. operator+(const measurement<Y>& lhs,const measurement<Y>& rhs)
  208. {
  209. return (measurement<Y>(lhs)+=rhs);
  210. }
  211. template<class Y>
  212. inline
  213. constexpr
  214. measurement<Y>
  215. operator-(const measurement<Y>& lhs,const measurement<Y>& rhs)
  216. {
  217. return (measurement<Y>(lhs)-=rhs);
  218. }
  219. template<class Y>
  220. inline
  221. constexpr
  222. measurement<Y>
  223. operator*(const measurement<Y>& lhs,const measurement<Y>& rhs)
  224. {
  225. return (measurement<Y>(lhs)*=rhs);
  226. }
  227. template<class Y>
  228. inline
  229. constexpr
  230. measurement<Y>
  231. operator/(const measurement<Y>& lhs,const measurement<Y>& rhs)
  232. {
  233. return (measurement<Y>(lhs)/=rhs);
  234. }
  235. /// specialize power typeof helper
  236. template<class Y,long N,long D>
  237. struct power_typeof_helper<measurement<Y>,static_rational<N,D> >
  238. {
  239. typedef measurement<
  240. typename power_typeof_helper<Y,static_rational<N,D> >::type
  241. > type;
  242. static constexpr type value(const measurement<Y>& x)
  243. {
  244. const static_rational<N,D> rat;
  245. const Y m = Y(rat.numerator())/Y(rat.denominator()),
  246. newval = std::pow(x.value(),m),
  247. err = newval*std::sqrt(std::pow(m*x.uncertainty()/x.value(),2));
  248. return type(newval,err);
  249. }
  250. };
  251. /// specialize root typeof helper
  252. template<class Y,long N,long D>
  253. struct root_typeof_helper<measurement<Y>,static_rational<N,D> >
  254. {
  255. typedef measurement<
  256. typename root_typeof_helper<Y,static_rational<N,D> >::type
  257. > type;
  258. static constexpr type value(const measurement<Y>& x)
  259. {
  260. const static_rational<N,D> rat;
  261. const Y m = Y(rat.denominator())/Y(rat.numerator()),
  262. newval = std::pow(x.value(),m),
  263. err = newval*std::sqrt(std::pow(m*x.uncertainty()/x.value(),2));
  264. return type(newval,err);
  265. }
  266. };
  267. // stream output
  268. template<class Y>
  269. inline
  270. std::ostream& operator<<(std::ostream& os,const measurement<Y>& val)
  271. {
  272. boost::io::ios_precision_saver precision_saver(os);
  273. boost::io::ios_flags_saver flags_saver(os);
  274. os << val.value() << "(+/-" << val.uncertainty() << ")";
  275. return os;
  276. }
  277. } // namespace units
  278. } // namespace boost
  279. #endif // BOOST_UNITS_MEASUREMENT_HPP