[units] How to multiply a quantity with a dimensionless factor
Hi, I'm new to boost and units and hope this is not too trivial. If I try to multiply a quantity with a dimensionless factor like quantity<length> a; quantity<length> b = 2.0 * b; I get compiler errors due to missing operator*. So I worked around this by using quantity<length> b = quantity<dimensionless>(2.0) * b; But I'm wondering if there is a easier and better readable way to do this? Thank you in advance, Marcel
Hi Marcel, On Sat, Jul 30, 2011 at 2:25 PM, Marcel <minusdreidb+boost@gmail.com> wrote:
Hi,
I'm new to boost and units and hope this is not too trivial. If I try to multiply a quantity with a dimensionless factor like
quantity<length> a; quantity<length> b = 2.0 * b;
I get compiler errors due to missing operator*. So I worked around this by using
quantity<length> b = quantity<dimensionless>(2.0) * b;
But I'm wondering if there is a easier and better readable way to do this?
Your first example should work -- in fact, the following works for me on g++4 and VC10 (using boost trunk, but should work with all units versions): #include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> using namespace boost::units; int main() { quantity<si::length> a; quantity<si::length> b = 2.0 * a; return 0; } Perhaps a compilable example, boost version, and compiler would aid diagnosis . . . HTH, Nate
On 31.07.2011 19:13, Nathan Crookston wrote:
Hi Marcel,
On Sat, Jul 30, 2011 at 2:25 PM, Marcel <minusdreidb+boost@gmail.com> wrote:
Hi,
I'm new to boost and units and hope this is not too trivial. If I try to multiply a quantity with a dimensionless factor like
quantity<length> a; quantity<length> b = 2.0 * b;
I get compiler errors due to missing operator*. So I worked around this by using
quantity<length> b = quantity<dimensionless>(2.0) * b;
But I'm wondering if there is a easier and better readable way to do this?
Your first example should work -- in fact, the following works for me on g++4 and VC10 (using boost trunk, but should work with all units versions):
#include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp>
using namespace boost::units;
int main() { quantity<si::length> a; quantity<si::length> b = 2.0 * a;
return 0; }
Perhaps a compilable example, boost version, and compiler would aid diagnosis . . .
HTH, Nate
Hi Nate, thanks for your reply. The example compiles here too in fact, which made me check my actual code more thoroughly: I had used "2" for the factor in my actual code instead of "2.0" as stated in my example. The differing value types seemed to cause the problem. Regards, Marcel
Hi Marcel, On Mon, Aug 1, 2011 at 7:39 AM, Marcel <minusdreidb+boost@gmail.com> wrote:
thanks for your reply. The example compiles here too in fact, which made me check my actual code more thoroughly: I had used "2" for the factor in my actual code instead of "2.0" as stated in my example. The differing value types seemed to cause the problem.
Ah, yes. The scalar multiplication is defined for the underlying type of the quantity -- had you written quantity<si::length, float> (rather than use the implicit double) you would have had to use 2.f * a; i.e. implicit conversions of scalar types aren't supported. That's bitten me a few times. Glad it's working. Nate
participants (2)
-
Marcel
-
Nathan Crookston