// 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) /** \file \brief conversion.cpp \details Test explicit and implicit unit conversion. Output: @verbatim //[conversion_output_1 L1 = 2 m L2 = 2 m L3 = 2 m L4 = 200 cm L5 = 5 m L6 = 4 m L7 = 200 cm //] //[conversion_output_2 volume (m^3) = 1 m^3 volume (cm^3) = 1e+06 cm^3 volume (m^3) = 1 m^3 energy (joules) = 1 J energy (ergs) = 1e+07 erg energy (joules) = 1 J velocity (2 m/s) = 2 m s^-1 velocity (2 cm/s) = 0.02 m s^-1 //] @endverbatim **/ #include #include #include #include #include #include #include using namespace boost::units; int main() { // test quantity_cast { // implicit value_type conversions //[conversion_snippet_1 quantity L1 = quantity(int(2.5)*si::meters); quantity L2(quantity(2.5*si::meters)); //] //[conversion_snippet_3 quantity L3 = static_cast >(L1); //] //[conversion_snippet_4 quantity L4 = static_cast >(L1); //] quantity L5(4*si::meters), L6(5*si::meters); quantity L7(L1); swap(L5,L6); std::cout << "L1 = " << L1 << std::endl << "L2 = " << L2 << std::endl << "L3 = " << L3 << std::endl << "L4 = " << L4 << std::endl << "L5 = " << L5 << std::endl << "L6 = " << L6 << std::endl << "L7 = " << L7 << std::endl << std::endl; } // test explicit unit system conversion { //[conversion_snippet_5 quantity vs(1.0*pow<3>(si::meter)); quantity vc(vs); quantity vs2(vc); quantity es(1.0*si::joule); quantity ec(es); quantity es2(ec); quantity v1 = 2.0*si::meters/si::second, v2(2.0*cgs::centimeters/cgs::second); //] std::cout << "volume (m^3) = " << vs << std::endl << "volume (cm^3) = " << vc << std::endl << "volume (m^3) = " << vs2 << std::endl << std::endl; std::cout << "energy (joules) = " << es << std::endl << "energy (ergs) = " << ec << std::endl << "energy (joules) = " << es2 << std::endl << std::endl; std::cout << "velocity (2 m/s) = " << v1 << std::endl << "velocity (2 cm/s) = " << v2 << std::endl << std::endl; } return 0; }