Skip to content

Instantly share code, notes, and snippets.

@danhper
Created October 30, 2017 09:00
Show Gist options
  • Save danhper/29b2635456f66c18575bb2da4c48a03c to your computer and use it in GitHub Desktop.
Save danhper/29b2635456f66c18575bb2da4c48a03c to your computer and use it in GitHub Desktop.
#include <complex>
#include <iostream>
#include <boost/typeof/std/complex.hpp>
#include <boost/units/systems/si/energy.hpp>
#include <boost/units/systems/si/force.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/electric_potential.hpp>
#include <boost/units/systems/si/current.hpp>
#include <boost/units/systems/si/resistance.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/prefixes.hpp>
using namespace boost::units;
using namespace boost::units::si;
static const auto millimeter = milli * meter;
namespace money {
struct money_base_dimension :
boost::units::base_dimension<money_base_dimension, -10>
{ };
typedef money_base_dimension::dimension_type money_dimension;
struct money_base_unit :
boost::units::base_unit<money_base_unit, money_dimension, -10>
{
static std::string name() { return "dollar"; }
static std::string symbol() { return "$"; }
};
typedef boost::units::make_system<money_base_unit>::type system;
/// unit typedefs
typedef unit<money_dimension,system> money;
static const money dollar,dollars;
} // namespace money
// helper for conversions between nautical length and si length
// BOOST_UNITS_DEFINE_CONVERSION_FACTOR(money::length_base_unit,
// boost::units::si::meter_base_unit,
// double, 1.852e3);
// using namespace money;
quantity<energy>
work(const quantity<force>& F, const quantity<length>& dx)
{
return F * dx; // Defines the relation: work = force * distance.
}
int main()
{
// /// Test calculation of work.
// quantity<force> F(2.0 * newton); // Define a quantity of force.
// quantity<length> dx(2.0 * meter); // and a distance,
// quantity<energy> E(work(F,dx)); // and calculate the work done.
//
quantity<money::money> my_money(5.0 * money::dollar);
quantity<length> my_distance(5.0 * meter);
auto foo = my_money / my_distance;
std::cout << foo << std::endl;
// quantity<length> m(1.0 * meter);
// quantity<length> ml(1.0 * millimeter);
//
// quantity<length> foo = ml + m;
// std::cout << foo << std::endl;
//
// std::cout << "F = " << F << std::endl
// << "dx = " << dx << std::endl
// << "E = " << E << std::endl
// << std::endl;
//
// /// Test and check complex quantities.
// typedef std::complex<double> complex_type; // double real and imaginary parts.
//
// // Define some complex electrical quantities.
// quantity<electric_potential, complex_type> v = complex_type(12.5, 0.0) * volts;
// quantity<current, complex_type> i = complex_type(3.0, 4.0) * amperes;
// quantity<resistance, complex_type> z = complex_type(1.5, -2.0) * ohms;
//
// std::cout << "V = " << v << std::endl
// << "I = " << i << std::endl
// << "Z = " << z << std::endl
// // Calculate from Ohm's law voltage = current * resistance.
// << "I * Z = " << i * z << std::endl
// // Check defined V is equal to calculated.
// << "I * Z == V? " << std::boolalpha << (i * z == v) << std::endl
// << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment