| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 | // Boost.Units - A C++ library for zero-overhead dimensional analysis and // unit/quantity manipulation and conversion//// Copyright (C) 2003-2008 Matthias Christian Schabel// Copyright (C) 2008 Steven Watanabe//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)#ifndef BOOST_UNITS_STATIC_RATIONAL_HPP #define BOOST_UNITS_STATIC_RATIONAL_HPP#include <boost/integer/common_factor_ct.hpp>#include <boost/mpl/less.hpp>#include <boost/mpl/arithmetic.hpp>#ifdef __BORLANDC__#include <boost/mpl/eval_if.hpp>#include <boost/mpl/integral_c.hpp>#include <boost/mpl/identity.hpp>#endif#include <boost/units/config.hpp>#include <boost/units/operators.hpp>/// \file /// \brief Compile-time rational numbers and operators.namespace boost {namespace units { namespace detail {struct static_rational_tag {};}typedef long   integer_type;/// Compile time absolute value.template<integer_type Value>struct static_abs{    BOOST_STATIC_CONSTANT(integer_type,value = Value < 0 ? -Value : Value);};// Compile time rational number./** This is an implementation of a compile time rational number, where @c static_rational<N,D> representsa rational number with numerator @c N and denominator @c D. Because of the potential for ambiguity arising from multiple equivalent values of @c static_rational (e.g. @c static_rational<6,2>==static_rational<3>), static rationals should always be accessed through @c static_rational<N,D>::type. Template specialization prevents instantiation of zero denominators (i.e. @c static_rational<N,0>). The following compile-time arithmetic operators are provided for static_rational variables only (no operators are defined between long and static_rational):    - @c mpl::negate    - @c mpl::plus    - @c mpl::minus    - @c mpl::times    - @c mpl::dividesNeither @c static_power nor @c static_root are defined for @c static_rational. This is because template types may not be floating point values, while powers and roots of rational numbers can produce floating point values. */#ifdef __BORLANDC__template<integer_type X>struct make_integral_c {    typedef boost::mpl::integral_c<integer_type, X> type;};template<integer_type N,integer_type D = 1>class static_rational{    public:        typedef static_rational this_type;        typedef boost::mpl::integral_c<integer_type, N> N_type;        typedef boost::mpl::integral_c<integer_type, D> D_type;        typedef typename make_integral_c<            (::boost::integer::static_gcd<                ::boost::units::static_abs<N>::value,                ::boost::units::static_abs<D>::value            >::value)>::type gcd_type;        typedef typename boost::mpl::eval_if<            boost::mpl::less<                D_type,                boost::mpl::integral_c<integer_type, 0>            >,            boost::mpl::negate<gcd_type>,            gcd_type        >::type den_type;            public:         // for mpl arithmetic support        typedef detail::static_rational_tag tag;                BOOST_STATIC_CONSTANT(integer_type, Numerator =            (::boost::mpl::divides<N_type, den_type>::value));        BOOST_STATIC_CONSTANT(integer_type, Denominator =            (::boost::mpl::divides<D_type, den_type>::value));                /// INTERNAL ONLY        typedef static_rational<N,D>    this_type;                /// static_rational<N,D> reduced by GCD        typedef static_rational<            (::boost::mpl::divides<N_type, den_type>::value),            (::boost::mpl::divides<D_type, den_type>::value)        >  type;                                         static BOOST_CONSTEXPR integer_type numerator()     { return Numerator; }        static BOOST_CONSTEXPR integer_type denominator()   { return Denominator; }                // INTERNAL ONLY        BOOST_CONSTEXPR static_rational() { }        //~static_rational() { }};#elsetemplate<integer_type N,integer_type D = 1>class static_rational{    private:        BOOST_STATIC_CONSTEXPR integer_type nabs = static_abs<N>::value,                                            dabs = static_abs<D>::value;                /// greatest common divisor of N and D        // need cast to signed because static_gcd returns unsigned long        BOOST_STATIC_CONSTEXPR integer_type den =             static_cast<integer_type>(boost::integer::static_gcd<nabs,dabs>::value) * ((D < 0) ? -1 : 1);            public:         // for mpl arithmetic support        typedef detail::static_rational_tag tag;                BOOST_STATIC_CONSTEXPR integer_type Numerator = N/den,            Denominator = D/den;                /// INTERNAL ONLY        typedef static_rational<N,D>    this_type;                /// static_rational<N,D> reduced by GCD        typedef static_rational<Numerator,Denominator>  type;                                         static BOOST_CONSTEXPR integer_type numerator()     { return Numerator; }        static BOOST_CONSTEXPR integer_type denominator()   { return Denominator; }                // INTERNAL ONLY        BOOST_CONSTEXPR static_rational() { }        //~static_rational() { }   };#endif}}#if BOOST_UNITS_HAS_BOOST_TYPEOF#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()BOOST_TYPEOF_REGISTER_TEMPLATE(boost::units::static_rational, (long)(long))#endifnamespace boost {namespace units {// prohibit zero denominatortemplate<integer_type N> class static_rational<N,0>;/// get decimal value of @c static_rationaltemplate<class T,integer_type N,integer_type D>inline BOOST_CONSTEXPR typename divide_typeof_helper<T,T>::type value(const static_rational<N,D>&){    return T(N)/T(D);}} // namespace units#ifndef BOOST_UNITS_DOXYGENnamespace mpl {#ifdef __BORLANDC__template<>struct plus_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            ::boost::mpl::plus<                boost::mpl::times<typename T0::N_type, typename T1::D_type>,                boost::mpl::times<typename T1::N_type, typename T0::D_type>            >::value,            ::boost::mpl::times<typename T0::D_type, typename T1::D_type>::value        >::type type;    };};template<>struct minus_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            ::boost::mpl::minus<                boost::mpl::times<typename T0::N_type, typename T1::D_type>,                boost::mpl::times<typename T1::N_type, typename T0::D_type>            >::value,            ::boost::mpl::times<typename T0::D_type, typename T1::D_type>::value        >::type type;    };};template<>struct times_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            ::boost::mpl::times<typename T0::N_type, typename T1::N_type>::value,            ::boost::mpl::times<typename T0::D_type, typename T1::D_type>::value        >::type type;    };};template<>struct divides_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            ::boost::mpl::times<typename T0::N_type, typename T1::D_type>::value,            ::boost::mpl::times<typename T0::D_type, typename T1::N_type>::value        >::type type;    };};template<>struct negate_impl<boost::units::detail::static_rational_tag>{    template<class T0>    struct apply {        typedef typename boost::units::static_rational<            ::boost::mpl::negate<typename T0::N_type>::value,            ::boost::mpl::identity<T0>::type::Denominator        >::type type;    };};template<>struct less_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply    {        typedef mpl::bool_<((mpl::minus<T0, T1>::type::Numerator) < 0)> type;    };};#elsetemplate<>struct plus_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            T0::Numerator*T1::Denominator+T1::Numerator*T0::Denominator,            T0::Denominator*T1::Denominator        >::type type;    };};template<>struct minus_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            T0::Numerator*T1::Denominator-T1::Numerator*T0::Denominator,            T0::Denominator*T1::Denominator        >::type type;    };};template<>struct times_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            T0::Numerator*T1::Numerator,            T0::Denominator*T1::Denominator        >::type type;    };};template<>struct divides_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply {        typedef typename boost::units::static_rational<            T0::Numerator*T1::Denominator,            T0::Denominator*T1::Numerator        >::type type;    };};template<>struct negate_impl<boost::units::detail::static_rational_tag>{    template<class T0>    struct apply {        typedef typename boost::units::static_rational<-T0::Numerator,T0::Denominator>::type type;    };};template<>struct less_impl<boost::units::detail::static_rational_tag, boost::units::detail::static_rational_tag>{    template<class T0, class T1>    struct apply    {        typedef mpl::bool_<((mpl::minus<T0, T1>::type::Numerator) < 0)> type;    };};#endif}#endif} // namespace boost#endif // BOOST_UNITS_STATIC_RATIONAL_HPP
 |