#ifndef BOOST_UTILITY_RATIO_HPP_INCLUDED_19JUN08 #define BOOST_UTILITY_RATIO_HPP_INCLUDED_19JUN08 #include #include #include #include #include #include #include #include #include /////////////////////////////////////////////////////////// #include "abs.hpp" #include "sign.hpp" //#include "intmax_t_gcd.hpp" namespace boost { namespace utility { template class ratio; // ratio arithmetic template struct ratio_add; template struct ratio_subtract; template struct ratio_multiply; template struct ratio_divide; // ratio comparison template struct ratio_equal; template struct ratio_not_equal; template struct ratio_less; template struct ratio_less_equal; template struct ratio_greater; template struct ratio_greater_equal; // convenience SI typedefs //typedef ratio<1, 1000000000000000000000000> yocto; // conditionally supported //typedef ratio<1, 1000000000000000000000> zepto; // conditionally supported //typedef ratio<1, 1000000000000000000> atto; //typedef ratio<1, 1000000000000000> femto; //typedef ratio<1, 1000000000000> pico; typedef ratio<1, 1000000000> nano; typedef ratio<1, 1000000> micro; typedef ratio<1, 1000> milli; typedef ratio<1, 100> centi; typedef ratio<1, 10> deci; typedef ratio< 10, 1> deca; typedef ratio< 100, 1> hecto; typedef ratio< 1000, 1> kilo; typedef ratio< 1000000, 1> mega; typedef ratio< 1000000000, 1> giga; //typedef ratio< 1000000000000, 1> tera; //typedef ratio< 1000000000000000, 1> peta; //typedef ratio< 1000000000000000000, 1> exa; //typedef ratio< 1000000000000000000000, 1> zetta; // conditionally supported //typedef ratio<1000000000000000000000000, 1> yotta; // conditionally supported /////////////////////////////////////////////////////////// #define DIVIDE_BY_ZERO_ERROR 0 // ratio.ratio template class ratio { // Print a diagnostic if trying to divide by zero BOOST_STATIC_ASSERT(( D != 0 )); typedef boost::integral_constant< boost::intmax_t , boost::math::static_gcd::value > gcd_type; // so we only instantiate it once public: // numerator BOOST_STATIC_CONSTANT( boost::intmax_t , num = (boost::mpl::divides< boost::integral_constant< boost::intmax_t , temp::sign::value * temp::sign::value * temp::abs::value > , gcd_type>::type::value) ); // denominator BOOST_STATIC_CONSTANT( boost::intmax_t , den = (boost::mpl::divides< boost::integral_constant< boost::intmax_t , temp::abs::value > , gcd_type>::type::value) ); }; // ratio.arithmetic template struct ratio_add { typedef ratio type; }; template struct ratio_subtract { typedef ratio type; }; template struct ratio_multiply { typedef ratio type; }; template struct ratio_divide { typedef ratio type; }; // ratio.comparison template struct ratio_equal : public boost::integral_constant< bool , (R1::num == R2::num && R1::den == R2::den) > {}; template struct ratio_less : public boost::integral_constant< bool , (R1::num * R2::den < R2::num * R1::den) // **FIXME** this is a bit dumb > {}; template struct ratio_not_equal : public boost::integral_constant::value> {}; template struct ratio_less_equal : public boost::integral_constant::value> {}; template struct ratio_greater : public boost::integral_constant::value> {}; template struct ratio_greater_equal : public boost::integral_constant::value> {}; } // namespace utility } // namespace boost #endif // BOOST_UTILITY_RATIO_HPP_INCLUDED_19JUN08