Boost logo

Boost Users :

Subject: [Boost-users] [units] non-rational scaling
From: Matthias Wohlgemuth (mail_at_[hidden])
Date: 2015-04-08 12:23:47


Dear all,

I have an issue with scaled units. I tried to implement a non-rational
scaling of units adapted from scale<> class (all in boost::units
namespace):

template<class Derived>
struct non_rational_scale
{
     static const unsigned long base = 10;
     typedef boost::units::static_rational<0> exponent;
     typedef double value_type;

     static double value() { return Derived::value(); }
     static std::string name() { return Derived::name(); }
     static std::string symbol() { return Derived::symbol(); }
};

struct electron_volt_scale : non_rational_scale<electron_volt_scale> {
     static double value() { return 1.60218e-19; } /* elementary charge
in Coulombs */
     static std::string name() { return "electron volt"; }
     static std::string symbol() { return "eV"; }
};

typedef make_scaled_unit<si::energy, electron_volt_scale>::type
electron_volt;

This works fine in one direction:

quantity<si::energy>(1.*electron_volt()); /* gives 1.60218e-19 J. */

However, in the other direction

quantity<electron_volt>(1.*si::joule); /* gives 1 eV */

I guess, that I need some specialization for non_rational_scale to
workout the correct scaling factor in the reverse direction.

Thank you for your help,

Matthias


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