tutorial.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 tutorial.cpp
  12. \brief Basic tutorial using SI units.
  13. \details
  14. Tutorial
  15. Defines a function that computes the work, in joules,
  16. done by exerting a force in newtons over a specified distance
  17. in meters and outputs the result to std::cout.
  18. Also code for computing the complex impedance
  19. using std::complex<double> as the value type.
  20. Output:
  21. @verbatim
  22. //[tutorial_output
  23. F = 2 N
  24. dx = 2 m
  25. E = 4 J
  26. V = (12.5,0) V
  27. I = (3,4) A
  28. Z = (1.5,-2) Ohm
  29. I*Z = (12.5,0) V
  30. I*Z == V? true
  31. //]
  32. @endverbatim
  33. */
  34. //[tutorial_code
  35. #include <complex>
  36. #include <iostream>
  37. #include <boost/typeof/std/complex.hpp>
  38. #include <boost/units/systems/si/energy.hpp>
  39. #include <boost/units/systems/si/force.hpp>
  40. #include <boost/units/systems/si/length.hpp>
  41. #include <boost/units/systems/si/electric_potential.hpp>
  42. #include <boost/units/systems/si/current.hpp>
  43. #include <boost/units/systems/si/resistance.hpp>
  44. #include <boost/units/systems/si/io.hpp>
  45. using namespace boost::units;
  46. using namespace boost::units::si;
  47. constexpr
  48. quantity<energy>
  49. work(const quantity<force>& F, const quantity<length>& dx)
  50. {
  51. return F * dx; // Defines the relation: work = force * distance.
  52. }
  53. int main()
  54. {
  55. /// Test calculation of work.
  56. quantity<force> F(2.0 * newton); // Define a quantity of force.
  57. quantity<length> dx(2.0 * meter); // and a distance,
  58. quantity<energy> E(work(F,dx)); // and calculate the work done.
  59. std::cout << "F = " << F << std::endl
  60. << "dx = " << dx << std::endl
  61. << "E = " << E << std::endl
  62. << std::endl;
  63. /// Test and check complex quantities.
  64. typedef std::complex<double> complex_type; // double real and imaginary parts.
  65. // Define some complex electrical quantities.
  66. quantity<electric_potential, complex_type> v = complex_type(12.5, 0.0) * volts;
  67. quantity<current, complex_type> i = complex_type(3.0, 4.0) * amperes;
  68. quantity<resistance, complex_type> z = complex_type(1.5, -2.0) * ohms;
  69. std::cout << "V = " << v << std::endl
  70. << "I = " << i << std::endl
  71. << "Z = " << z << std::endl
  72. // Calculate from Ohm's law voltage = current * resistance.
  73. << "I * Z = " << i * z << std::endl
  74. // Check defined V is equal to calculated.
  75. << "I * Z == V? " << std::boolalpha << (i * z == v) << std::endl
  76. << std::endl;
  77. return 0;
  78. }
  79. //]