// DimensionedValue #ifndef _DIMENSIONED_VALUE_H_INCLUDED #define _DIMENSIONED_VALUE_H_INCLUDED #include using std::ostream; // class DimensionedUnit is used to output the unit template class DimensionedUnit { static bool printUnit(bool showDot, ostream& os, char* unit, int dim) { if(dim!=0) { if(showDot) { os<<'\xb7'; } os< class DimensionedUnit<0,0,-1,0> { static void printOn(ostream& os) { os<<"Hz"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<1,1,-2,0> { static void printOn(ostream& os) { os<<"N"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<-1,1,-2,0> { static void printOn(ostream& os) { os<<"Pa"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<-1,1,-1,0> { static void printOn(ostream& os) { os<<"Pa\xb7s"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-2,0> { static void printOn(ostream& os) { os<<"J"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-3,0> { static void printOn(ostream& os) { os<<"W"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<0,0,1,1> { static void printOn(ostream& os) { os<<"C"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-3,-1> { static void printOn(ostream& os) { os<<"V"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<-2,-1,4,2> { static void printOn(ostream& os) { os<<"F"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-3,-2> { static void printOn(ostream& os) { os<<"Ohm"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<-2,-1,3,2> { static void printOn(ostream& os) { os<<"S"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-2,-1> { static void printOn(ostream& os) { os<<"Wb"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<0,1,-2,-1> { static void printOn(ostream& os) { os<<"T"; } static bool unitsAreDerived() { return true; } }; template<> class DimensionedUnit<2,1,-2,-2> { static void printOn(ostream& os) { os<<"H"; } static bool unitsAreDerived() { return true; } }; // class DimensionedValue holds values that are dimensioned template class DimensionedValue { double v; public: DimensionedValue(double val) : v(val) {} inline double magnitude() { return v; } /* inline DimensionedValue operator*(double d) { return DimensionedValue(magnitude()*d); }*/ template DimensionedValue operator*(DimensionedValue other) { return DimensionedValue(magnitude()*other.magnitude()); } template DimensionedValue operator/(DimensionedValue other) { return DimensionedValue(magnitude()/other.magnitude()); } DimensionedValue operator+(DimensionedValue other) { return DimensionedValue(magnitude()+other.magnitude()); } DimensionedValue operator-(DimensionedValue other) { return DimensionedValue(magnitude()-other.magnitude()); } void printOn(ostream& os) { os<::printOn(os); } bool unitsAreDerived() { return DimensionedUnit::unitsAreDerived(); } static const int mExponent; static const int kExponent; static const int sExponent; static const int aExponent; }; template const int DimensionedValue::mExponent = L; template const int DimensionedValue::kExponent = M; template const int DimensionedValue::sExponent = T; template const int DimensionedValue::aExponent = C; template static ostream& operator<<(ostream& os, DimensionedValue u) { u.printOn(os); return os; } template static DimensionedValue operator*(double d,DimensionedValue u) { return DimensionedValue(d*u.magnitude()); } // Standard names typedef DimensionedValue<0,0,0,0> unity; typedef DimensionedValue<1,0,0,0> length; typedef DimensionedValue<0,1,0,0> mass; typedef DimensionedValue<0,0,1,0> time_; // avoid conflict with "time" in "time.h" typedef DimensionedValue<0,0,0,1> current; typedef DimensionedValue<2,0,0,0> area; typedef DimensionedValue<3,0,0,0> volume; typedef DimensionedValue<1,0,-1,0> velocity; typedef DimensionedValue<1,0,-2,0> acceleration; typedef DimensionedValue<1,1,-1,0> momentum; typedef DimensionedValue<2,1,-1,0> angular_momentum; typedef DimensionedValue<0,0,-1,0> frequency; typedef DimensionedValue<1,1,-2,0> force; typedef DimensionedValue<-1,1,-2,0> pressure; typedef DimensionedValue<-1,1,-1,0> viscosity; typedef DimensionedValue<2,1,-2,0> energy; typedef DimensionedValue<2,1,-3,0> power; typedef DimensionedValue<0,0,1,1> charge; typedef DimensionedValue<2,1,-3,-1> potential; typedef DimensionedValue<-2,-1,4,2> capacitance; typedef DimensionedValue<2,1,-3,-2> resistance; typedef DimensionedValue<-2,-1,3,2> conductance; typedef DimensionedValue<2,1,-2,-1> magnetic_flux; typedef DimensionedValue<0,1,-2,-1> magnetic_flux_density; typedef DimensionedValue<2,1,-2,-2> inductance; // Standard units & English units const length meter=1; const length inch=0.0254; const length foot=12*inch; const length feet=foot; const length yard=3*feet; const length mile=5280*feet; const mass kilogram=1; const mass gram=1e-3; const time_ second=1; const time_ minute=60; const time_ hour=60*minute; const time_ day=24*hour; const frequency hertz=1; const current ampere=1; const force newton=1; const pressure pascal_=1; //pascal is a keyword in VC++ const viscosity poiseuille=1; const energy joule=1; const power watt=1; const charge coulomb=1; const potential volt=1; const capacitance farad=1; const resistance ohm=1; const conductance siemens=1; const magnetic_flux weber=1; const magnetic_flux_density tesla=1; const inductance henry=1; // Standard qualifiers const double yotta=1e24; const double zetta=1e21; const double exa=1e18; const double peta=1e15; const double tera=1e12; const double giga=1e9; const double mega=1e6; const double kilo=1e3; const double hecto=1e2; const double deka=10; const double deci=1e-1; const double centi=1e-2; const double milli=1e-3; const double micro=1e-6; const double nano=1e-9; const double pico=1e-12; const double femto=1e-15; const double atto=1e-18; const double zepto=1e-21; const double yocto=1e-24; #endif