// 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) #include #include #include #include #include #include #include #include #include #include namespace boost { namespace units { struct length_base_dimension : boost::units::base_dimension { }; ///> base dimension of length struct time_base_dimension : boost::units::base_dimension { }; ///> base dimension of time typedef length_base_dimension::dimension_type length_dimension; typedef time_base_dimension::dimension_type time_dimension; struct length1_base_unit : base_unit { static std::string name() { return "length 1"; } static std::string symbol() { return "l1"; } }; struct length2_base_unit : base_unit { static std::string name() { return "length2"; } static std::string symbol() { return "l2"; } }; struct time1_base_unit : base_unit { static std::string name() { return "time1"; } static std::string symbol() { return "t1"; } }; struct time2_base_unit : base_unit { static std::string name() { return "time2"; } static std::string symbol() { return "t2"; } }; namespace s1 { typedef make_system::type system; /// unit typedefs typedef unit dimensionless; typedef unit length; typedef unit time; /// unit constants BOOST_UNITS_STATIC_CONSTANT(length1,length); BOOST_UNITS_STATIC_CONSTANT(time1,time); } // namespace s1 namespace s2 { typedef make_system::type system; /// unit typedefs typedef unit dimensionless; typedef unit length; typedef unit time; /// unit constants BOOST_UNITS_STATIC_CONSTANT(length2,length); BOOST_UNITS_STATIC_CONSTANT(time2,time); } // namespace s2 template struct conversion_helper< quantity,quantity > { static quantity convert(const quantity& source) { return quantity::from_value(2.5*source.value()); } }; template struct conversion_helper< quantity,quantity > { static quantity convert(const quantity& source) { return quantity::from_value((1.0/2.5)*source.value()); } }; template struct conversion_helper< quantity,quantity > { static quantity convert(const quantity& source) { return quantity::from_value(0.5*source.value()); } }; } // namespace units } // namespace boost int main(void) { using namespace boost::units; quantity l1(1.0*s1::length1); quantity l2(1.5*l1); quantity l3(2.0*l2/3.0); quantity t1(1.0*s1::time1); quantity t2(1.5*t1); // quantity t3(2.0*t2/3.0); return 0; } /* // 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) #include #include #include #include #include #include #include #include #include #include namespace boost { namespace units { struct length_base_dimension : boost::units::base_dimension { }; ///> base dimension of length struct mass_base_dimension : boost::units::base_dimension { }; ///> base dimension of mass struct time_base_dimension : boost::units::base_dimension { }; ///> base dimension of time typedef length_base_dimension::dimension_type length_dimension; typedef mass_base_dimension::dimension_type mass_dimension; typedef time_base_dimension::dimension_type time_dimension; struct centimeter_base_unit : base_unit { static std::string name() { return "centimeter"; } static std::string symbol() { return "cm"; } }; struct gram_base_unit : base_unit { static std::string name() { return "gram"; } static std::string symbol() { return "g"; } }; struct second_base_unit : base_unit { static std::string name() { return "second"; } static std::string symbol() { return "s"; } }; namespace CG { typedef make_system::type system; /// unit typedefs typedef unit dimensionless; typedef unit length; typedef unit mass; /// unit constants BOOST_UNITS_STATIC_CONSTANT(centimeter,length); BOOST_UNITS_STATIC_CONSTANT(gram,mass); } // namespace CG namespace cgs { typedef make_system::type system; /// unit typedefs typedef unit dimensionless; typedef unit length; typedef unit mass; typedef unit time; /// unit constants BOOST_UNITS_STATIC_CONSTANT(centimeter,length); BOOST_UNITS_STATIC_CONSTANT(gram,mass); BOOST_UNITS_STATIC_CONSTANT(second,time); } // namespace cgs namespace esu { typedef make_system::type system; /// derived dimension for force in electrostatic units : L M T^-2 typedef derived_dimension::type force_dimension; /// derived dimension for charge in electrostatic units : L^3/2 M^1/2 T^-1 typedef make_dimension_list< mpl::list< dim >, dim >, dim > > >::type charge_dimension; /// derived dimension for current in electrostatic units : L^3/2 M^1/2 T^-2 typedef make_dimension_list< mpl::list< dim >, dim >, dim > > >::type current_dimension; /// derived dimension for electric potential in electrostatic units : L^1/2 M^1/2 T^-1 typedef make_dimension_list< mpl::list< dim >, dim >, dim > > >::type electric_potential_dimension; /// derived dimension for electric field in electrostatic units : L^-1/2 M^1/2 T^-1 typedef make_dimension_list< mpl::list< dim >, dim >, dim > > >::type electric_field_dimension; /// unit typedefs typedef unit dimensionless; typedef unit length; typedef unit mass; typedef unit time; typedef unit force; typedef unit charge; typedef unit current; typedef unit electric_potential; typedef unit electric_field; /// unit constants BOOST_UNITS_STATIC_CONSTANT(centimeter,length); BOOST_UNITS_STATIC_CONSTANT(gram,mass); BOOST_UNITS_STATIC_CONSTANT(second,time); BOOST_UNITS_STATIC_CONSTANT(dyne,force); BOOST_UNITS_STATIC_CONSTANT(esu,charge); BOOST_UNITS_STATIC_CONSTANT(statvolt,electric_potential); } // namespace esu template quantity coulombLaw(const quantity& q1, const quantity& q2, const quantity& r) { return q1*q2/(r*r); } } // namespace units } // namespace boost int main(void) { using namespace boost::units; quantity cg_length(1.0*CG::centimeter); quantity cgs_length(1.0*cgs::centimeter); std::cout << cg_length/cgs_length << std::endl; std::cout << esu::gram*pow<2>(esu::centimeter/esu::second)/esu::esu << std::endl; std::cout << esu::statvolt/esu::centimeter << std::endl; quantity q1 = 1.0*esu::esu, q2 = 2.0*esu::esu; quantity r = 1.0*esu::centimeter; std::cout << coulombLaw(q1,q2,r) << std::endl; std::cout << coulombLaw(q1,q2,cgs_length) << std::endl; return 0; } */