scale.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) 2007-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_SCALE_HPP_INCLUDED
  11. #define BOOST_UNITS_SCALE_HPP_INCLUDED
  12. ///
  13. /// \file
  14. /// \brief 10^3 Engineering & 2^10 binary scaling factors for autoprefixing.
  15. /// \details
  16. ///
  17. #include <string>
  18. #include <boost/units/config.hpp>
  19. #include <boost/units/static_rational.hpp>
  20. #include <boost/units/units_fwd.hpp>
  21. #include <boost/units/detail/one.hpp>
  22. #include <boost/units/detail/static_rational_power.hpp>
  23. namespace boost {
  24. namespace units {
  25. template<class S, class Scale>
  26. struct scaled_base_unit;
  27. /// class representing a scaling factor such as 10^3
  28. /// The exponent must be a static rational.
  29. template<long Base, class Exponent>
  30. struct scale
  31. {
  32. BOOST_STATIC_CONSTEXPR long base = Base;
  33. typedef Exponent exponent;
  34. typedef double value_type;
  35. static BOOST_CONSTEXPR value_type value() { return(detail::static_rational_power<Exponent>(static_cast<double>(base))); }
  36. // These need to be defined in specializations for
  37. // printing to work.
  38. // static std::string name();
  39. // static std::string symbol();
  40. };
  41. template<long Base, class Exponent>
  42. BOOST_CONSTEXPR_OR_CONST long scale<Base, Exponent>::base;
  43. /// INTERNAL ONLY
  44. template<long Base>
  45. struct scale<Base, static_rational<0> >
  46. {
  47. BOOST_STATIC_CONSTEXPR long base = Base;
  48. typedef static_rational<0> exponent;
  49. typedef one value_type;
  50. static BOOST_CONSTEXPR one value() { return(one()); }
  51. static std::string name() { return(""); }
  52. static std::string symbol() { return(""); }
  53. };
  54. template<long Base>
  55. BOOST_CONSTEXPR_OR_CONST long scale<Base, static_rational<0> >::base;
  56. template<long Base,class Exponent>
  57. std::string symbol_string(const scale<Base,Exponent>&)
  58. {
  59. return scale<Base,Exponent>::symbol();
  60. }
  61. template<long Base,class Exponent>
  62. std::string name_string(const scale<Base,Exponent>&)
  63. {
  64. return scale<Base,Exponent>::name();
  65. }
  66. #ifndef BOOST_UNITS_DOXYGEN
  67. #define BOOST_UNITS_SCALE_SPECIALIZATION(base_,exponent_,val_,name_,symbol_) \
  68. template<> \
  69. struct scale<base_, exponent_ > \
  70. { \
  71. BOOST_STATIC_CONSTEXPR long base = base_; \
  72. typedef exponent_ exponent; \
  73. typedef double value_type; \
  74. static BOOST_CONSTEXPR value_type value() { return(val_); } \
  75. static std::string name() { return(#name_); } \
  76. static std::string symbol() { return(#symbol_); } \
  77. }
  78. #define BOOST_UNITS_SCALE_DEF(exponent_,value_,name_,symbol_) \
  79. BOOST_UNITS_SCALE_SPECIALIZATION(10,static_rational<exponent_>,value_, name_, symbol_)
  80. BOOST_UNITS_SCALE_DEF(-24, 1e-24, yocto, y);
  81. BOOST_UNITS_SCALE_DEF(-21, 1e-21, zepto, z);
  82. BOOST_UNITS_SCALE_DEF(-18, 1e-18, atto, a);
  83. BOOST_UNITS_SCALE_DEF(-15, 1e-15, femto, f);
  84. BOOST_UNITS_SCALE_DEF(-12, 1e-12, pico, p);
  85. BOOST_UNITS_SCALE_DEF(-9, 1e-9, nano, n);
  86. BOOST_UNITS_SCALE_DEF(-6, 1e-6, micro, u);
  87. BOOST_UNITS_SCALE_DEF(-3, 1e-3, milli, m);
  88. BOOST_UNITS_SCALE_DEF(-2, 1e-2, centi, c);
  89. BOOST_UNITS_SCALE_DEF(-1, 1e-1, deci, d);
  90. BOOST_UNITS_SCALE_DEF(1, 1e1, deka, da);
  91. BOOST_UNITS_SCALE_DEF(2, 1e2, hecto, h);
  92. BOOST_UNITS_SCALE_DEF(3, 1e3, kilo, k);
  93. BOOST_UNITS_SCALE_DEF(6, 1e6, mega, M);
  94. BOOST_UNITS_SCALE_DEF(9, 1e9, giga, G);
  95. BOOST_UNITS_SCALE_DEF(12, 1e12, tera, T);
  96. BOOST_UNITS_SCALE_DEF(15, 1e15, peta, P);
  97. BOOST_UNITS_SCALE_DEF(18, 1e18, exa, E);
  98. BOOST_UNITS_SCALE_DEF(21, 1e21, zetta, Z);
  99. BOOST_UNITS_SCALE_DEF(24, 1e24, yotta, Y);
  100. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<10>, 1024.0, kibi, Ki);
  101. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<20>, 1048576.0, mebi, Mi);
  102. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<30>, 1073741824.0, gibi, Gi);
  103. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<40>, 1099511627776.0, tebi, Ti);
  104. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<50>, 1125899906842624.0, pebi, Pi);
  105. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<60>, 1152921504606846976.0, exbi, Ei);
  106. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<70>, 1180591620717411303424.0, zebi, Zi);
  107. BOOST_UNITS_SCALE_SPECIALIZATION(2, static_rational<80>, 1208925819614629174706176.0, yobi, Yi);
  108. #undef BOOST_UNITS_SCALE_DEF
  109. #undef BOOST_UNITS_SCALE_SPECIALIZATION
  110. #endif
  111. } // namespace units
  112. } // namespace boost
  113. #if BOOST_UNITS_HAS_BOOST_TYPEOF
  114. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  115. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::scale, (long)(class))
  116. #endif
  117. #endif