heterogeneous_unit.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  11. \file
  12. \brief heterogeneous_unit.cpp
  13. \details
  14. Test heterogeneous units and quantities.
  15. Output:
  16. @verbatim
  17. //[heterogeneous_unit_output_1
  18. 1.5 m
  19. 1 g
  20. 1.5 m g
  21. 1.5 m g^-1
  22. 1 N
  23. 1 kg s^-2
  24. 1 cm kg s^-2
  25. 1 cm m^-1 kg s^-2
  26. //]
  27. //[heterogeneous_unit_output_2
  28. 1.5 cm m
  29. 0.015 m^2
  30. //]
  31. @endverbatim
  32. **/
  33. #define MCS_USE_DEMANGLING
  34. //#define MCS_USE_BOOST_REGEX_DEMANGLING
  35. #include <iostream>
  36. #include <boost/units/io.hpp>
  37. #include <boost/units/pow.hpp>
  38. #include <boost/units/detail/utility.hpp>
  39. #include <boost/units/systems/cgs.hpp>
  40. #include <boost/units/systems/si.hpp>
  41. #include <boost/units/systems/si/io.hpp>
  42. using namespace boost::units;
  43. int main()
  44. {
  45. //[heterogeneous_unit_snippet_1
  46. quantity<si::length> L(1.5*si::meter);
  47. quantity<cgs::mass> M(1.0*cgs::gram);
  48. std::cout << L << std::endl
  49. << M << std::endl
  50. << L*M << std::endl
  51. << L/M << std::endl
  52. << std::endl;
  53. std::cout << 1.0*si::meter*si::kilogram/pow<2>(si::second) << std::endl
  54. << 1.0*si::meter*si::kilogram/pow<2>(si::second)/si::meter
  55. << std::endl << std::endl;
  56. std::cout << 1.0*cgs::centimeter*si::kilogram/pow<2>(si::second) << std::endl
  57. << 1.0*cgs::centimeter*si::kilogram/pow<2>(si::second)/si::meter
  58. << std::endl << std::endl;
  59. //]
  60. //[heterogeneous_unit_snippet_2
  61. quantity<si::area> A(1.5*si::meter*cgs::centimeter);
  62. std::cout << 1.5*si::meter*cgs::centimeter << std::endl
  63. << A << std::endl
  64. << std::endl;
  65. //]
  66. return 0;
  67. }