conversion.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 conversion.cpp
  13. \details
  14. Test explicit and implicit unit conversion.
  15. Output:
  16. @verbatim
  17. //[conversion_output_1
  18. L1 = 2 m
  19. L2 = 2 m
  20. L3 = 2 m
  21. L4 = 200 cm
  22. L5 = 5 m
  23. L6 = 4 m
  24. L7 = 200 cm
  25. //]
  26. //[conversion_output_2
  27. volume (m^3) = 1 m^3
  28. volume (cm^3) = 1e+06 cm^3
  29. volume (m^3) = 1 m^3
  30. energy (joules) = 1 J
  31. energy (ergs) = 1e+07 erg
  32. energy (joules) = 1 J
  33. velocity (2 m/s) = 2 m s^-1
  34. velocity (2 cm/s) = 0.02 m s^-1
  35. //]
  36. @endverbatim
  37. **/
  38. #include <iostream>
  39. #include <boost/units/io.hpp>
  40. #include <boost/units/pow.hpp>
  41. #include <boost/units/systems/cgs.hpp>
  42. #include <boost/units/systems/cgs/io.hpp>
  43. #include <boost/units/systems/si.hpp>
  44. #include <boost/units/systems/si/io.hpp>
  45. using namespace boost::units;
  46. int main()
  47. {
  48. // test quantity_cast
  49. {
  50. // implicit value_type conversions
  51. //[conversion_snippet_1
  52. quantity<si::length> L1 = quantity<si::length,int>(int(2.5)*si::meters);
  53. quantity<si::length,int> L2(quantity<si::length,double>(2.5*si::meters));
  54. //]
  55. //[conversion_snippet_3
  56. quantity<si::length,int> L3 = static_cast<quantity<si::length,int> >(L1);
  57. //]
  58. //[conversion_snippet_4
  59. quantity<cgs::length> L4 = static_cast<quantity<cgs::length> >(L1);
  60. //]
  61. quantity<si::length,int> L5(4*si::meters),
  62. L6(5*si::meters);
  63. quantity<cgs::length> L7(L1);
  64. swap(L5,L6);
  65. std::cout << "L1 = " << L1 << std::endl
  66. << "L2 = " << L2 << std::endl
  67. << "L3 = " << L3 << std::endl
  68. << "L4 = " << L4 << std::endl
  69. << "L5 = " << L5 << std::endl
  70. << "L6 = " << L6 << std::endl
  71. << "L7 = " << L7 << std::endl
  72. << std::endl;
  73. }
  74. // test explicit unit system conversion
  75. {
  76. //[conversion_snippet_5
  77. quantity<si::volume> vs(1.0*pow<3>(si::meter));
  78. quantity<cgs::volume> vc(vs);
  79. quantity<si::volume> vs2(vc);
  80. quantity<si::energy> es(1.0*si::joule);
  81. quantity<cgs::energy> ec(es);
  82. quantity<si::energy> es2(ec);
  83. quantity<si::velocity> v1 = 2.0*si::meters/si::second,
  84. v2(2.0*cgs::centimeters/cgs::second);
  85. //]
  86. std::cout << "volume (m^3) = " << vs << std::endl
  87. << "volume (cm^3) = " << vc << std::endl
  88. << "volume (m^3) = " << vs2 << std::endl
  89. << std::endl;
  90. std::cout << "energy (joules) = " << es << std::endl
  91. << "energy (ergs) = " << ec << std::endl
  92. << "energy (joules) = " << es2 << std::endl
  93. << std::endl;
  94. std::cout << "velocity (2 m/s) = " << v1 << std::endl
  95. << "velocity (2 cm/s) = " << v2 << std::endl
  96. << std::endl;
  97. }
  98. return 0;
  99. }