Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active March 26, 2016 00:10
Show Gist options
  • Save usagi/5141ae477cede70875fa to your computer and use it in GitHub Desktop.
Save usagi/5141ae477cede70875fa to your computer and use it in GitHub Desktop.
boost::units : 単位付きの quantity 型と無次元数の係数の積を簡単に記述する方法 ref: http://qiita.com/usagi/items/c9122be0ad2ce0d3ccca
#include <boost/units/systems/si.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/units/io.hpp>
#include <iostream>
namespace some_app
{
using namespace boost::units;
using namespace boost::units::si;
using float_type = long double;
using length_type = quantity< length, float_type >;
/// @brief π
constexpr auto pi = boost::math::constants::pi< float_type >();
/// @brief 地球の半径(ref. WGS84)
const length_type earth_equatorial_radius = 6.378'137e+6 * meters;
/// @brief 地球の赤道の長さ
const length_type earth_equator_length = pi * earth_equatorial_radius * 2; // <-- Oops! Compile Error!!
}
int main()
{
std::cout << some_app::earth_equator_length;
}
prog.cc:29:76: error: no match for 'operator*' (operand types are 'boost::units::multiply_typeof_helper<long double, boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10l, boost::units::static_rational<3l> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, long double> >::type {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10l, boost::units::static_rational<3l> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, long double>}' and 'int')
const length_type earth_equator_length = pi * earth_equatorial_radius * 2; // <-- Oops! Compile Error!!
^
/// @brief 無次元数 × 単位付き型
template<typename UNIT, typename IN_BASE, typename IN_FACTOR >
inline auto operator*( const IN_FACTOR& in_factor, const quantity< UNIT, IN_BASE >& in_base )
{ return static_cast< IN_BASE >( in_factor ) * in_base; }
/// @brief 単位付き型 × 無次元数
template<typename UNIT, typename IN_FACTOR, typename IN_BASE >
inline auto operator*( const quantity< UNIT, IN_BASE >& in_base, const IN_FACTOR& in_factor )
{ return in_base * static_cast< IN_BASE >( in_factor ); }
#include <boost/units/systems/si.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/units/io.hpp>
#include <iostream>
namespace some_app
{
using namespace boost::units;
using namespace boost::units::si;
using float_type = long double;
using length_type = quantity< length, float_type >;
/// @brief 無次元数 × 単位付き型
template<typename UNIT, typename IN_BASE, typename IN_FACTOR >
inline auto operator*( const IN_FACTOR& in_factor, const quantity< UNIT, IN_BASE >& in_base )
{ return static_cast< IN_BASE >( in_factor ) * in_base; }
/// @brief 単位付き型 × 無次元数
template<typename UNIT, typename IN_FACTOR, typename IN_BASE >
inline auto operator*( const quantity< UNIT, IN_BASE >& in_base, const IN_FACTOR& in_factor )
{ return in_base * static_cast< IN_BASE >( in_factor ); }
/// @brief π
constexpr auto pi = boost::math::constants::pi< float_type >();
/// @brief 地球の半径(ref. WGS84)
const length_type earth_equatorial_radius = 6.378'137e+6 * meters;
/// @brief 地球の赤道の長さ
const length_type earth_equator_length = pi * earth_equatorial_radius * 2; // <-- Oops! Compile Error!!
}
int main()
{
std::cout << some_app::earth_equator_length;
}
4.0075e+07 m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment