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

source code of boost/boost/units/scale.hpp