🐈

[C++] boost unitsでradian<->degree相互変換

2023/04/30に公開

単純なことをするだけの関数。以下使用例とともに。

#include <iostream>

#include <boost/units/systems/si.hpp> 
#include <boost/units/systems/angle/degrees.hpp> 

template<typename T>
T degree2radian(T arg_degree)
{
  //boost::units::quantity<boost::units::degree::plane_angle> deg(arg_degree * boost::units::degree::degrees);
  auto degree(arg_degree * boost::units::degree::degrees);

  boost::units::quantity<boost::units::si::plane_angle> radian(degree);

  return radian.value();
}

template<typename T>
T radian2degree(T arg_radian)
{
  // boost::units::quantity<boost::units::si::plane_angle> radian(arg_radian * boost::units::si::radians);
  auto radian(arg_radian * boost::units::si::radians);

  boost::units::quantity<boost::units::degree::plane_angle> degree(radian);

  return degree.value();
}

int main()
{

  double converted_radian = degree2radian(90.0);
  std::cout << "90.0 degree is " << converted_radian << "rad" << std::endl;

  double converted_degree = radian2degree(1.570796326);
  std::cout << "1.5708 rad is " << converted_degree  << "deg" << std::endl;

  return 0;
}

出力

90.0 degree is 1.5708rad
1.5708 rad is 90deg

Discussion