|
Boost Users : |
Subject: Re: [Boost-users] boost::units - converting from one derived_dimension to another across systems (imperial to metric)
From: Eisuke Kawashima (e.kawaschima+boost_at_[hidden])
Date: 2018-11-19 17:23:10
Hi list,
> I am having trouble converting from one derived_dimension to another across systems (imperial to metric). I'd like to convert, say, lb/in to, say, kg/m. I thought I would do the following but get a compiler error #:
>
> *.h--------------------------------------------------------------------------------
> namespace dimensional_analysis {
> namespace lineal_force {
> //lb/in
> typedef boost::units::derived_dimension<
> boost::units::us::pound_force_base_unit, 1,
> boost::units::us::inch_base_unit, -1
> >::type lb_per_inch_dimension;
> typedef boost::units::unit<
> lb_per_inch_dimension,
> dimensional_analysis::lengths::ip_system
> > lb_per_inch_unit;
> typedef boost::units::quantity<lb_per_inch_unit, double> lb_per_inch_quantity;
> BOOST_UNITS_STATIC_CONSTANT(lbpin, lb_per_inch_quantity);
> //kg/m
> typedef boost::units::derived_dimension<
> boost::units::si::kilogram_base_unit, 1,
> boost::units::si::meter_base_unit, -1
> >::type kg_per_meter_dimension;
> typedef boost::units::unit<
> kg_per_meter_dimension,
> boost::units::si::system
> > kg_per_meter_unit;
> typedef boost::units::quantity<kg_per_meter_unit, double> kg_per_meter_unit_quantity;
> BOOST_UNITS_STATIC_CONSTANT(kgpm, kg_per_meter_unit_quantity);
> }//lineal_force
> }//dimensional_analysis
>
> *.cp ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> //Do I need these?
> BOOST_UNITS_DEFINE_CONVERSION_FACTOR(dimensional_analysis::lineal_force::kg_per_meter_unit, dimensional_analysis::lineal_force::lb_per_inch_unit, double, 0.0559974); // exact conversion
> BOOST_UNITS_DEFAULT_CONVERSION(dimensional_analysis::lineal_force::kg_per_meter_unit, dimensional_analysis::lineal_force::lb_per_inch_unit);
>
> //This does not compile#
> const auto conv_factor_try0 = conversion_factor(dimensional_analysis::lineal_force::kg_per_meter_unit::unit_type(), dimensional_analysis::lineal_force::lb_per_inch_unit::unit_type());
>
> # *\boost_1_68_0\boost\units\detail\conversion_impl.hpp(340): error C2672: 'conversion_factor': no matching overloaded function found (compiling source file...*.cpp)
>
> Any help is appreciated
I opened a PR (https://github.com/boostorg/units/pull/32) to address
such a situation;
the following code
```cpp
#include <iostream>
#include <boost/units/base_units/us/inch.hpp>
#include <boost/units/base_units/us/pound_force.hpp>
#include <boost/units/io.hpp>
#include <boost/units/physical_dimensions.hpp>
#include <boost/units/systems/si.hpp>
int main() {
auto force_us{1.0 * boost::units::us::pound_force};
auto length_us{1.0 * boost::units::us::inch};
boost::units::quantity<boost::units::si::force, double> force_si{force_us};
boost::units::quantity<boost::units::si::length, double> length_si{length_us};
std::cout << force_us << " = " << force_si << '\n';
std::cout << length_us << " = " << length_si << '\n';
std::cout << force_us / length_us << " = " << force_si / length_si << '\n';
return 0;
}
```
will print
```
1 lbf = 4.44822 m kg s^-2
1 in = 0.0254 m
1 lbf in^-1 = 175.127 kg s^-2
```
Best
-- Eisuke Kawashima
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net