[units] Conversions from quantities

Hello, Just wanted to clarify something. These should convert from given quantities to the desired units? double unit_conversion::from_length_to_inches(quantity<length> const & L) { const quantity<inch_base_unit::unit_type> result_(L); return result_.value(); } double unit_conversion::from_mass_to_pounds(quantity<mass> const & M) { const quantity<ounce_base_unit::unit_type> result_(M); return result_.value(); }
From an earlier post, I think maybe so, but I was interested in expert opinions, as well.
https://groups.google.com/forum/#!topic/boost-list/YDJoMcu1Xx4 Thank you... Best regards, Michael Powell

On 13/08/2015 10:39, Michael Powell wrote:
Just wanted to clarify something. These should convert from given quantities to the desired units?
double unit_conversion::from_length_to_inches(quantity<length> const & L) { const quantity<inch_base_unit::unit_type> result_(L); return result_.value(); }
double unit_conversion::from_mass_to_pounds(quantity<mass> const & M) { const quantity<ounce_base_unit::unit_type> result_(M); return result_.value(); }
From an earlier post, I think maybe so, but I was interested in expert opinions, as well.
Conversion between related dimensions is basically automatic: typedef inch_base_unit::unit_type inch_unit; BOOST_UNITS_STATIC_CONSTANT(inches, inch_unit); You can then express quantities in those types as you'd expect: quantity<length> a(24.5 * milli * meters); quantity<inch_unit> b(18.0 * inches); quantity<inch_unit> c(a); quantity<length> d(16.5 * inches); Once you have a quantity<U>, you can use value() to extract the raw number, as you've done above. Another way to do it (which is a little more "correct", albeit at times more painful) is to convert it to a dimensionless value first -- dimensionless values can be implicitly cast to the base type (typically double). eg: double to_mm(quantity<length> const& L) { const quantity<dimensionless> result(a / milli / meters); return result; } double to_inches(quantity<length> const& L) { const quantity<inch_unit> result(L); return result / inches; } to_mm(a); to_mm(b); to_mm(0.54 * meters); to_mm(quantity<length>(51.2 * milli * meters)); to_mm(quantity<length>(16.5 * inches)); to_inches(a); to_inches(b); to_inches(0.54 * meters); to_inches(quantity<length>(16.5 * inches)); Note that due to the scaled units quirks, you do have to have the intermediate dimensionless variable in to_mm to force it to do the conversion. If it were to_m instead then you wouldn't. If you're working with inches/pounds/etc a lot, you might want to consider making a system for them (similar to SI vs. CGS in the examples). Regardless, converting to a raw value should be done at the last possible moment, when you're outputting to some external code that doesn't understand Boost.Units. Within your own code you should always keep the unit attached, to reduce errors.

AMDG On 08/12/2015 04:39 PM, Michael Powell wrote:
Just wanted to clarify something. These should convert from given quantities to the desired units?
If what you really want is a raw double, than, yes, this should work.
double unit_conversion::from_length_to_inches(quantity<length> const & L) { const quantity<inch_base_unit::unit_type> result_(L); return result_.value(); }
double unit_conversion::from_mass_to_pounds(quantity<mass> const & M) {
Pounds is a unit of force, not mass. To do such a conversion, you need to multiply by g.
const quantity<ounce_base_unit::unit_type> result_(M); return result_.value(); }
In Christ, Steven Watanabe

On Thu, Aug 13, 2015 at 3:14 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 08/12/2015 04:39 PM, Michael Powell wrote:
Just wanted to clarify something. These should convert from given quantities to the desired units?
If what you really want is a raw double, than, yes, this should work.
double unit_conversion::from_length_to_inches(quantity<length> const & L) { const quantity<inch_base_unit::unit_type> result_(L); return result_.value(); }
double unit_conversion::from_mass_to_pounds(quantity<mass> const & M) {
Pounds is a unit of force, not mass. To do such a conversion, you need to multiply by g.
Pound-force (literally, "lbf") or pounds (i.e. "lb")? Interestingly enough, I may also need to work in Pound-force, or a neighboring unit, i.e. "slugs.ft/s" http://en.wikipedia.org/wiki/Pound_(mass) http://en.wikipedia.org/wiki/Pound_(force) Would you mind disambiguating that in terms of Boost.Units? As far as I can tell from the US base unit (BOOST_UNIT_SYSTEMS_US_POUND_HPP_INCLUDED), we're talking about Mass in this instance. Thank you...
const quantity<ounce_base_unit::unit_type> result_(M); return result_.value(); }
In Christ,
BTW, did you find the PR okay for next version(s) of Units and/or Boost?
Steven Watanabe
In Him.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Always deadlock, what is the problem, who can review my code attached in the ticket and give me an answer? Thanks very much . My problem url: https://svn.boost.org/trac/boost/ticket/11537 At 2015-08-14 03:20:47, "Michael Powell" <mwpowellhtx@gmail.com> wrote:
On Thu, Aug 13, 2015 at 3:14 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 08/12/2015 04:39 PM, Michael Powell wrote:
Just wanted to clarify something. These should convert from given quantities to the desired units?
If what you really want is a raw double, than, yes, this should work.
double unit_conversion::from_length_to_inches(quantity<length> const & L) { const quantity<inch_base_unit::unit_type> result_(L); return result_.value(); }
double unit_conversion::from_mass_to_pounds(quantity<mass> const & M) {
Pounds is a unit of force, not mass. To do such a conversion, you need to multiply by g.
Pound-force (literally, "lbf") or pounds (i.e. "lb")? Interestingly enough, I may also need to work in Pound-force, or a neighboring unit, i.e. "slugs.ft/s"
http://en.wikipedia.org/wiki/Pound_(mass)
http://en.wikipedia.org/wiki/Pound_(force)
Would you mind disambiguating that in terms of Boost.Units? As far as I can tell from the US base unit (BOOST_UNIT_SYSTEMS_US_POUND_HPP_INCLUDED), we're talking about Mass in this instance.
Thank you...
const quantity<ounce_base_unit::unit_type> result_(M); return result_.value(); }
In Christ,
BTW, did you find the PR okay for next version(s) of Units and/or Boost?
Steven Watanabe
In Him.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Gavin Lambert
-
Michael Powell
-
Steven Watanabe
-
钟志文