rr.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 720 行 · 第 1/2 页

HPP
720
字号
inline RR pow(RR a, int b){ return ::NTL::power(a.value(), b); }inline RR sin(RR a){ return ::NTL::sin(a.value()); }/*inline RR sinh(RR a){ return ::NTL::sinh(a.value()); }*/inline RR sqrt(RR a){ return ::NTL::sqrt(a.value()); }/*inline RR tanh(RR a){ return ::NTL::tanh(a.value()); }*/   inline RR pow(const RR& r, long l)   {      return ::NTL::power(r.value(), l);   }   inline RR tan(const RR& a)   {      return sin(a)/cos(a);   }   inline RR frexp(RR r, int* exp)   {      *exp = r.value().e;      r.value().e = 0;      while(r >= 1)      {         *exp += 1;         r.value().e -= 1;      }      while(r < 0.5)      {         *exp -= 1;         r.value().e += 1;      }      BOOST_ASSERT(r < 1);      BOOST_ASSERT(r >= 0.5);      return r;   }   inline RR ldexp(RR r, int exp)   {      r.value().e += exp;      return r;   }// Streaming:template <class charT, class traits>inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const RR& a){   return os << a.value();}template <class charT, class traits>inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, RR& a){   ::NTL::RR v;   is >> v;   a = v;   return is;}} // namespace ntlnamespace tools{template<>inline int digits<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   return ::NTL::RR::precision();}template <>inline float real_cast<float, boost::math::ntl::RR>(boost::math::ntl::RR t){   double r;   conv(r, t.value());   return static_cast<float>(r);}template <>inline double real_cast<double, boost::math::ntl::RR>(boost::math::ntl::RR t){   double r;   conv(r, t.value());   return r;}namespace detail{template<class I>void convert_to_long_result(NTL::RR const& r, I& result){   result = 0;   I last_result(0);   NTL::RR t(r);   double term;   do   {      conv(term, t);      last_result = result;      result += static_cast<I>(term);      t -= term;   }while(result != last_result);}}template <>inline long double real_cast<long double, boost::math::ntl::RR>(boost::math::ntl::RR t){   long double result(0);   detail::convert_to_long_result(t.value(), result);   return result;}template <>inline boost::math::ntl::RR real_cast<boost::math::ntl::RR, boost::math::ntl::RR>(boost::math::ntl::RR t){   return t;}template <>inline unsigned real_cast<unsigned, boost::math::ntl::RR>(boost::math::ntl::RR t){   unsigned result;   detail::convert_to_long_result(t.value(), result);   return result;}template <>inline int real_cast<int, boost::math::ntl::RR>(boost::math::ntl::RR t){   unsigned result;   detail::convert_to_long_result(t.value(), result);   return result;}template <>inline boost::math::ntl::RR max_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   static bool has_init = false;   static NTL::RR val;   if(!has_init)   {      val = 1;      val.e = NTL_OVFBND-20;      has_init = true;   }   return val;}template <>inline boost::math::ntl::RR min_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   static bool has_init = false;   static NTL::RR val;   if(!has_init)   {      val = 1;      val.e = -NTL_OVFBND+20;      has_init = true;   }   return val;}template <>inline boost::math::ntl::RR log_max_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   static bool has_init = false;   static NTL::RR val;   if(!has_init)   {      val = 1;      val.e = NTL_OVFBND-20;      val = log(val);      has_init = true;   }   return val;}template <>inline boost::math::ntl::RR log_min_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   static bool has_init = false;   static NTL::RR val;   if(!has_init)   {      val = 1;      val.e = -NTL_OVFBND+20;      val = log(val);      has_init = true;   }   return val;}template <>inline boost::math::ntl::RR epsilon<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){   return ldexp(boost::math::ntl::RR(1), 1-boost::math::policies::digits<boost::math::ntl::RR, boost::math::policies::policy<> >());}} // namespace tools//// The number of digits precision in RR can vary with each call// so we need to recalculate these with each call://namespace constants{template<> inline boost::math::ntl::RR pi<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){    NTL::RR result;    ComputePi(result);    return result;}template<> inline boost::math::ntl::RR e<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR)){    NTL::RR result;    result = 1;    return exp(result);}} // namespace constantsnamespace ntl{   //   // These are some fairly brain-dead versions of the math   // functions that NTL fails to provide.   //   //   // Inverse trig functions:   //   struct asin_root   {      asin_root(RR const& target) : t(target){}      std::tr1::tuple<RR, RR, RR> operator()(RR const& p)      {         RR f0 = sin(p);         RR f1 = cos(p);         RR f2 = -f0;         f0 -= t;         return std::tr1::make_tuple(f0, f1, f2);      }   private:      RR t;   };   inline RR asin(RR z)   {      double r;      conv(r, z.value());      return boost::math::tools::halley_iterate(         asin_root(z),          RR(std::asin(r)),          RR(-boost::math::constants::pi<RR>()/2),         RR(boost::math::constants::pi<RR>()/2),         NTL::RR::precision());   }   struct acos_root   {      acos_root(RR const& target) : t(target){}      std::tr1::tuple<RR, RR, RR> operator()(RR const& p)      {         RR f0 = cos(p);         RR f1 = -sin(p);         RR f2 = -f0;         f0 -= t;         return std::tr1::make_tuple(f0, f1, f2);      }   private:      RR t;   };   inline RR acos(RR z)   {      double r;      conv(r, z.value());      return boost::math::tools::halley_iterate(         acos_root(z),          RR(std::acos(r)),          RR(-boost::math::constants::pi<RR>()/2),         RR(boost::math::constants::pi<RR>()/2),         NTL::RR::precision());   }   struct atan_root   {      atan_root(RR const& target) : t(target){}      std::tr1::tuple<RR, RR, RR> operator()(RR const& p)      {         RR c = cos(p);         RR ta = tan(p);         RR f0 = ta - t;         RR f1 = 1 / (c * c);         RR f2 = 2 * ta / (c * c);         return std::tr1::make_tuple(f0, f1, f2);      }   private:      RR t;   };   inline RR atan(RR z)   {      double r;      conv(r, z.value());      return boost::math::tools::halley_iterate(         atan_root(z),          RR(std::atan(r)),          -boost::math::constants::pi<RR>()/2,         boost::math::constants::pi<RR>()/2,         NTL::RR::precision());   }   inline RR sinh(RR z)   {      return (expm1(z.value()) - expm1(-z.value())) / 2;   }   inline RR cosh(RR z)   {      return (exp(z) + exp(-z)) / 2;   }   inline RR tanh(RR z)   {      return sinh(z) / cosh(z);   }   inline RR fmod(RR x, RR y)   {      // This is a really crummy version of fmod, we rely on lots      // of digits to get us out of trouble...      RR factor = floor(x/y);      return x - factor * y;   }   template <class Policy>   inline int iround(RR const& x, const Policy& pol)   {      return tools::real_cast<int>(round(x, pol));   }   template <class Policy>   inline int itrunc(RR const& x, const Policy& pol)   {      return tools::real_cast<int>(trunc(x, pol));   }} // namespace ntl} // namespace math} // namespace boost#endif // BOOST_MATH_REAL_CONCEPT_HPP

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?