// ratio.hpp ---------------------------------------------------------------// // Copyright 2008 Howard Hinnant // Copyright 2008 Beman Dawes // Copyright 2009 Vicente J. Botet Escriba // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt /* This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. Many thanks to Howard for making his code available under the Boost license. The original code was modified to conform to Boost conventions and to section 20.4 Compile-time rational arithmetic [ratio], of the C++ committee working paper N2798. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. time2_demo contained this comment: Much thanks to Andrei Alexandrescu, Walter Brown, Peter Dimov, Jeff Garland, Terry Golubiewski, Daniel Krugler, Anthony Williams. */ // The way overflow is managed for ratio_less is taken from llvm/libcxx/include/ratio #ifndef BOOST_RATIO_RATIO_HPP #define BOOST_RATIO_RATIO_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef BOOST_RATIO_EXTENSIONS #include #include #endif // // We simply cannot include this header on gcc without getting copious warnings of the kind: // // boost/integer.hpp:77:30: warning: use of C99 long long integer constant // // And yet there is no other reasonable implementation, so we declare this a system header // to suppress these warnings. // #if defined(__GNUC__) && (__GNUC__ >= 4) #pragma GCC system_header #endif namespace lslboost { //----------------------------------------------------------------------------// // // // 20.6.1 Class template ratio [ratio.ratio] // // // //----------------------------------------------------------------------------// template class ratio { static const lslboost::intmax_t ABS_N = mpl::abs_c::value; static const lslboost::intmax_t ABS_D = mpl::abs_c::value; BOOST_RATIO_STATIC_ASSERT(ABS_N >= 0, BOOST_RATIO_NUMERATOR_IS_OUT_OF_RANGE, ()); BOOST_RATIO_STATIC_ASSERT(ABS_D > 0, BOOST_RATIO_DENOMINATOR_IS_OUT_OF_RANGE, ()); BOOST_RATIO_STATIC_ASSERT(D != 0, BOOST_RATIO_DIVIDE_BY_0 , ()); static const lslboost::intmax_t SIGN_N = mpl::sign_c::value * mpl::sign_c::value; static const lslboost::intmax_t GCD = mpl::gcd_c::value; public: BOOST_STATIC_CONSTEXPR lslboost::intmax_t num = SIGN_N * ABS_N / GCD; BOOST_STATIC_CONSTEXPR lslboost::intmax_t den = ABS_D / GCD; #ifdef BOOST_RATIO_EXTENSIONS typedef mpl::rational_c_tag tag; typedef lslboost::rational value_type; typedef lslboost::intmax_t num_type; typedef lslboost::intmax_t den_type; ratio() {} template ratio(const ratio<_N2, _D2>&, typename enable_if_c < (ratio<_N2, _D2>::num == num && ratio<_N2, _D2>::den == den) >::type* = 0) {} template typename enable_if_c < (ratio<_N2, _D2>::num == num && ratio<_N2, _D2>::den == den), ratio& >::type operator=(const ratio<_N2, _D2>&) {return *this;} static value_type value() {return value_type(num,den);} value_type operator()() const {return value();} #endif typedef ratio type; }; #if defined(BOOST_NO_CXX11_CONSTEXPR) template const lslboost::intmax_t ratio::num; template const lslboost::intmax_t ratio::den; #endif //----------------------------------------------------------------------------// // // // 20.6.2 Arithmetic on ratio types [ratio.arithmetic] // // // //----------------------------------------------------------------------------// template struct ratio_add : lslboost::ratio_detail::ratio_add::type { }; template struct ratio_subtract : lslboost::ratio_detail::ratio_subtract::type { }; template struct ratio_multiply : lslboost::ratio_detail::ratio_multiply::type { }; template struct ratio_divide : lslboost::ratio_detail::ratio_divide::type { }; //----------------------------------------------------------------------------// // // // 20.6.3 Comparision of ratio types [ratio.comparison] // // // //----------------------------------------------------------------------------// // ratio_equal template struct ratio_equal : public lslboost::integral_constant {}; template struct ratio_not_equal : public lslboost::integral_constant::value> {}; // ratio_less template struct ratio_less : lslboost::integral_constant::value> {}; template struct ratio_less_equal : lslboost::integral_constant::value> {}; template struct ratio_greater : lslboost::integral_constant::value> {}; template struct ratio_greater_equal : lslboost::integral_constant::value> {}; template struct ratio_gcd : ratio::value, mpl::lcm_c::value>::type { }; //----------------------------------------------------------------------------// // // // More arithmetic on ratio types [ratio.arithmetic] // // // //----------------------------------------------------------------------------// #ifdef BOOST_RATIO_EXTENSIONS template struct ratio_negate : ratio<-R::num, R::den>::type { }; template struct ratio_abs : ratio::value, R::den>::type { }; template struct ratio_sign : mpl::sign_c { }; template struct ratio_inverse : ratio::type { }; template struct ratio_lcm : ratio::value, mpl::gcd_c::value>::type { }; template struct ratio_modulo : ratio<(R1::num * R2::den) % (R2::num * R1::den), R1::den * R2::den>::type { }; namespace detail { template struct ratio_min : R1 {}; template struct ratio_min : R2 {}; template struct ratio_max : R2 {}; template struct ratio_max : R1 {}; } template struct ratio_min : detail::ratio_min::value>::type { }; template struct ratio_max : detail::ratio_max::value>::type { }; template struct ratio_power : ratio_multiply< typename ratio_power::type, typename ratio_power::type, p/2>::type >::type {}; template struct ratio_power : ratio<1>::type {}; template struct ratio_power : R {}; template struct ratio_power : ratio_divide, R>::type {}; #endif } // namespace lslboost #endif // BOOST_RATIO_RATIO_HPP