quantity.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 quantity.cpp
  13. \details
  14. Test quantity algebra.
  15. Output:
  16. @verbatim
  17. //[quantity_output_double
  18. L = 2 m
  19. L+L = 4 m
  20. L-L = 0 m
  21. L*L = 4 m^2
  22. L/L = 1 dimensionless
  23. L*meter = 2 m^2
  24. kilograms*(L/seconds)*(L/seconds) = 4 m^2 kg s^-2
  25. kilograms*(L/seconds)^2 = 4 m^2 kg s^-2
  26. L^3 = 8 m^3
  27. L^(3/2) = 2.82843 m^(3/2)
  28. 2vL = 1.41421 m^(1/2)
  29. (3/2)vL = 1.5874 m^(2/3)
  30. //]
  31. //[quantity_output_complex
  32. L = (3,4) m
  33. L+L = (6,8) m
  34. L-L = (0,0) m
  35. L*L = (-7,24) m^2
  36. L/L = (1,0) dimensionless
  37. L*meter = (3,4) m^2
  38. kilograms*(L/seconds)*(L/seconds) = (-7,24) m^2 kg s^-2
  39. kilograms*(L/seconds)^2 = (-7,24) m^2 kg s^-2
  40. L^3 = (-117,44) m^3
  41. L^(3/2) = (2,11) m^(3/2)
  42. 2vL = (2,1) m^(1/2)
  43. (3/2)vL = (2.38285,1.69466) m^(2/3)
  44. //]
  45. @endverbatim
  46. **/
  47. #include <complex>
  48. #include <iostream>
  49. #include <boost/mpl/list.hpp>
  50. #include <boost/typeof/std/complex.hpp>
  51. #include <boost/units/pow.hpp>
  52. #include <boost/units/quantity.hpp>
  53. #include <boost/units/io.hpp>
  54. #include "test_system.hpp"
  55. int main(void)
  56. {
  57. using namespace boost::units;
  58. using namespace boost::units::test;
  59. {
  60. //[quantity_snippet_1
  61. quantity<length> L = 2.0*meters; // quantity of length
  62. quantity<energy> E = kilograms*pow<2>(L/seconds); // quantity of energy
  63. //]
  64. std::cout << "L = " << L << std::endl
  65. << "L+L = " << L+L << std::endl
  66. << "L-L = " << L-L << std::endl
  67. << "L*L = " << L*L << std::endl
  68. << "L/L = " << L/L << std::endl
  69. << "L*meter = " << L*meter << std::endl
  70. << "kilograms*(L/seconds)*(L/seconds) = "
  71. << kilograms*(L/seconds)*(L/seconds) << std::endl
  72. << "kilograms*(L/seconds)^2 = "
  73. << kilograms*pow<2>(L/seconds) << std::endl
  74. << "L^3 = "
  75. << pow<3>(L) << std::endl
  76. << "L^(3/2) = "
  77. << pow<static_rational<3,2> >(L) << std::endl
  78. << "2vL = "
  79. << root<2>(L) << std::endl
  80. << "(3/2)vL = "
  81. << root<static_rational<3,2> >(L) << std::endl
  82. << std::endl;
  83. }
  84. {
  85. //[quantity_snippet_2
  86. quantity<length,std::complex<double> > L(std::complex<double>(3.0,4.0)*meters);
  87. quantity<energy,std::complex<double> > E(kilograms*pow<2>(L/seconds));
  88. //]
  89. std::cout << "L = " << L << std::endl
  90. << "L+L = " << L+L << std::endl
  91. << "L-L = " << L-L << std::endl
  92. << "L*L = " << L*L << std::endl
  93. << "L/L = " << L/L << std::endl
  94. << "L*meter = " << L*meter << std::endl
  95. << "kilograms*(L/seconds)*(L/seconds) = "
  96. << kilograms*(L/seconds)*(L/seconds) << std::endl
  97. << "kilograms*(L/seconds)^2 = "
  98. << kilograms*pow<2>(L/seconds) << std::endl
  99. << "L^3 = "
  100. << pow<3>(L) << std::endl
  101. << "L^(3/2) = "
  102. << pow<static_rational<3,2> >(L) << std::endl
  103. << "2vL = "
  104. << root<2>(L) << std::endl
  105. << "(3/2)vL = "
  106. << root<static_rational<3,2> >(L) << std::endl
  107. << std::endl;
  108. }
  109. return 0;
  110. }