Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) BoostUnitsExample7

 

Cpp98BoostQt CreatorLubuntuWindows

 

Boost.Units example 7: calculating a force from a mass and an acceleration 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: ./CppBoostUnitsExample7/CppBoostUnitsExample7.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppBoostUnitsExample7/main.cpp

 

#include <cassert>
#include <iostream>
#include <iomanip>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/units/systems/si/force.hpp>
#include <boost/units/systems/si/mass.hpp>
#include <boost/units/systems/si/acceleration.hpp>
#include <boost/units/systems/si/io.hpp>
#pragma GCC diagnostic pop

int main()
{
  //Increase readability
  typedef boost::units::quantity<boost::units::si::acceleration> Acceleration;
  typedef boost::units::quantity<boost::units::si::force> Force;
  typedef boost::units::quantity<boost::units::si::mass> Mass;
  using boost::units::si::kilogram;
  using boost::units::si::meters_per_second_squared;
  using boost::units::si::newton;

  //Plain doubles
  const double m = 5.0;
  const double a = 2.0;

  //Convert doubles to their unit-bearing equivalents
  const Mass mass(m * kilogram);
  const Acceleration acceleration(a * meters_per_second_squared);

  //Newton's second law of motion
  const Force force(mass * acceleration);

  //Display Newton's second law of motion
  std::cout
    << "F = m * a" << "\n"
    << "F: " << force << "\n"
    << "m: " << mass << "\n"
    << "a: " << acceleration << "\n";


  //Check if calculating the force from the plain doubles is identical
  const Force force_expected(m * a * newton);
  assert(force == force_expected);
}

 

 

 

 

 

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