Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostUnitsExample1

 

Cpp98BoostQt CreatorLubuntuWindows

 

Boost.Units example 1: example from Boost is a Boost.Units example.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppBoostUnitsExample1/CppBoostUnitsExample1.pro

 

include(../../ConsoleApplication.pri)
include(../../Libraries/Boost.pri)

SOURCES += main.cpp

 

 

 

 

 

./CppBoostUnitsExample1/main.cpp

 

//Slightly modified example from
// http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units/Examples.html

#include <complex>
#include <iostream>

#include <boost/typeof/std/complex.hpp>

#include <boost/units/systems/si/current.hpp>
#include <boost/units/systems/si/electric_potential.hpp>
#include <boost/units/systems/si/energy.hpp>
#include <boost/units/systems/si/force.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/resistance.hpp>

using namespace boost::units;
using namespace boost::units::si;

const quantity<energy> work(const quantity<force>& F,const quantity<length>& dx)
{
  return F*dx;
}

int main()
{
  //test calculation of work
  const quantity<force>     F(2.0*newton);
  const quantity<length>    dx(2.0*meter);
  const quantity<energy>    E(work(F,dx));

  std::cout
    << "F  = " << F <<  '\n'
    << "dx = " << dx <<  '\n'
    << "E  = " << E <<  '\n';


  //check complex quantities
  typedef std::complex<double>    complex_type;

  const quantity<electric_potential,complex_type> v = complex_type(12.5,0.0)*volts;
  const quantity<current,complex_type>            i = complex_type(3.0,4.0)*amperes;
  const quantity<resistance,complex_type>         z = complex_type(1.5,-2.0)*ohms;

  std::cout
    << "V   = " << v << '\n'
    << "I   = " << i << '\n'
    << "Z   = " << z << '\n'
    << "I*Z = " << i*z << '\n'
    << "I*Z == V? " << std::boolalpha << (i*z == v) <<  '\n';
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml