//Ê (C) 2003 Matthias Christian Schabel. Permission to copy, use, modify, sell and //Ê distribute this software is granted provided this copyright notice appears //Ê in all copies. This software is provided "as is" without express or implied //Ê warranty, and with no claim as to its suitability for any purpose. Any use of // this library or any part thereof in a commerical product must be acknowledged // in the copyright of that product. #ifndef MCS_STATIC_RATIONAL_HPP #define MCS_STATIC_RATIONAL_HPP #include #include namespace mcs { namespace units { // compile time absolute value template struct static_abs { BOOST_STATIC_CONSTANT(long,value) = Value < 0 ? -Value : Value; }; // generic compile time arithmetic operators template struct Negate; template struct Add; template struct Subtract; template struct Multiply; template struct Divide; template struct Power; template struct Root; // compile time rational number template struct Rational { static const long nabs = static_abs::value, dabs = static_abs::value; static const unsigned long nden = boost::math::static_gcd::value, dden = boost::math::static_gcd::value; // need cast to signed because static_gcd returns unsigned long static const long Numerator = N/static_cast(nden), Denominator = D/static_cast(dden); static long numerator() { return Numerator; } static long denominator() { return Denominator; } Rational() { } }; // prohibit zero denominator template struct Rational; // get decimal value of Rational template T value(const Rational& r) { return static_cast(r.numerator())/static_cast(r.denominator()); } // reduce rational type to lowest common denominator template struct ReduceRational { typedef Rational arg_type; typedef Rational type; }; // negate rational number template struct Negate< Rational > { typedef typename ReduceRational<-N,D>::type type; }; // add rational numbers template struct Add< Rational,Rational > { typedef typename ReduceRational::type type; }; // subtract rational numbers template struct Subtract< Rational,Rational > { typedef typename ReduceRational::type type; }; // multiply rational numbers template struct Multiply< Rational,Rational > { typedef typename ReduceRational::type type; }; template struct Multiply< Rational,Rational > { typedef typename ReduceRational::type type; }; // divide rational numbers template struct Divide< Rational,Rational > { typedef typename ReduceRational::type type; }; template struct Divide< Rational,Rational > { typedef typename ReduceRational::type type; }; } // namespace units } // namespace mcs #endif // MCS_STATIC_RATIONAL_HPP