1 // Copyright John Maddock 2007.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_NTL_RR_HPP
7 #define BOOST_MATH_NTL_RR_HPP
9 #include <boost/config.hpp>
10 #include <boost/limits.hpp>
11 #include <boost/math/tools/real_cast.hpp>
12 #include <boost/math/tools/precision.hpp>
13 #include <boost/math/constants/constants.hpp>
14 #include <boost/math/tools/roots.hpp>
15 #include <boost/math/special_functions/fpclassify.hpp>
16 #include <boost/math/bindings/detail/big_digamma.hpp>
17 #include <boost/math/bindings/detail/big_lanczos.hpp>
21 #include <boost/config/no_tr1/cmath.hpp>
24 namespace boost{ namespace math{
31 RR ldexp(RR r, int exp);
32 RR frexp(RR r, int* exp);
39 RR(const ::NTL::RR& c) : m_value(c){}
44 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
82 #ifdef BOOST_HAS_LONG_LONG
83 RR(boost::ulong_long_type c)
87 RR(boost::long_long_type c)
102 assign_large_real(c);
106 RR& operator=(char c) { m_value = c; return *this; }
107 RR& operator=(unsigned char c) { m_value = c; return *this; }
108 RR& operator=(signed char c) { m_value = c; return *this; }
109 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
110 RR& operator=(wchar_t c) { m_value = c; return *this; }
112 RR& operator=(short c) { m_value = c; return *this; }
113 RR& operator=(unsigned short c) { m_value = c; return *this; }
114 RR& operator=(int c) { assign_large_int(c); return *this; }
115 RR& operator=(unsigned int c) { assign_large_int(c); return *this; }
116 RR& operator=(long c) { assign_large_int(c); return *this; }
117 RR& operator=(unsigned long c) { assign_large_int(c); return *this; }
118 #ifdef BOOST_HAS_LONG_LONG
119 RR& operator=(boost::long_long_type c) { assign_large_int(c); return *this; }
120 RR& operator=(boost::ulong_long_type c) { assign_large_int(c); return *this; }
122 RR& operator=(float c) { m_value = c; return *this; }
123 RR& operator=(double c) { m_value = c; return *this; }
124 RR& operator=(long double c) { assign_large_real(c); return *this; }
127 NTL::RR& value(){ return m_value; }
128 NTL::RR const& value()const{ return m_value; }
130 // Member arithmetic:
131 RR& operator+=(const RR& other)
132 { m_value += other.value(); return *this; }
133 RR& operator-=(const RR& other)
134 { m_value -= other.value(); return *this; }
135 RR& operator*=(const RR& other)
136 { m_value *= other.value(); return *this; }
137 RR& operator/=(const RR& other)
138 { m_value /= other.value(); return *this; }
141 RR const& operator+()const
145 const ::NTL::ZZ& mantissa() const
146 { return m_value.mantissa(); }
147 long exponent() const
148 { return m_value.exponent(); }
150 static void SetPrecision(long p)
151 { ::NTL::RR::SetPrecision(p); }
153 static long precision()
154 { return ::NTL::RR::precision(); }
156 static void SetOutputPrecision(long p)
157 { ::NTL::RR::SetOutputPrecision(p); }
158 static long OutputPrecision()
159 { return ::NTL::RR::OutputPrecision(); }
166 void assign_large_real(const V& a)
181 if (!(boost::math::isfinite)(a))
183 throw std::overflow_error("Cannot construct an instance of NTL::RR with an infinite value.");
195 // extract 30 bits from f:
199 conv(t.x, (int)term);
207 void assign_large_int(V a)
210 #pragma warning(push)
211 #pragma warning(disable:4146)
216 bool neg = a < V(0) ? true : false;
221 t = static_cast<double>(a & 0xffff);
222 m_value += ldexp(RR(t), exp).value();
234 // Non-member arithmetic:
235 inline RR operator+(const RR& a, const RR& b)
241 inline RR operator-(const RR& a, const RR& b)
247 inline RR operator*(const RR& a, const RR& b)
253 inline RR operator/(const RR& a, const RR& b)
261 inline bool operator == (const RR& a, const RR& b)
262 { return a.value() == b.value() ? true : false; }
263 inline bool operator != (const RR& a, const RR& b)
264 { return a.value() != b.value() ? true : false;}
265 inline bool operator < (const RR& a, const RR& b)
266 { return a.value() < b.value() ? true : false; }
267 inline bool operator <= (const RR& a, const RR& b)
268 { return a.value() <= b.value() ? true : false; }
269 inline bool operator > (const RR& a, const RR& b)
270 { return a.value() > b.value() ? true : false; }
271 inline bool operator >= (const RR& a, const RR& b)
272 { return a.value() >= b.value() ? true : false; }
275 // Non-member mixed compare:
277 inline bool operator == (const T& a, const RR& b)
279 return a == b.value();
282 inline bool operator != (const T& a, const RR& b)
284 return a != b.value();
287 inline bool operator < (const T& a, const RR& b)
289 return a < b.value();
292 inline bool operator > (const T& a, const RR& b)
294 return a > b.value();
297 inline bool operator <= (const T& a, const RR& b)
299 return a <= b.value();
302 inline bool operator >= (const T& a, const RR& b)
304 return a >= b.value();
306 #endif // Non-member mixed compare:
308 // Non-member functions:
311 { return ::NTL::acos(a.value()); }
314 { return ::NTL::cos(a.value()); }
317 { return ::NTL::asin(a.value()); }
319 { return ::NTL::atan(a.value()); }
320 inline RR atan2(RR a, RR b)
321 { return ::NTL::atan2(a.value(), b.value()); }
324 { return ::NTL::ceil(a.value()); }
326 inline RR fmod(RR a, RR b)
327 { return ::NTL::fmod(a.value(), b.value()); }
329 { return ::NTL::cosh(a.value()); }
332 { return ::NTL::exp(a.value()); }
334 { return ::NTL::fabs(a.value()); }
336 { return ::NTL::abs(a.value()); }
337 inline RR floor(RR a)
338 { return ::NTL::floor(a.value()); }
340 inline RR modf(RR a, RR* ipart)
343 RR result = modf(a.value(), &ip);
347 inline RR frexp(RR a, int* expon)
348 { return ::NTL::frexp(a.value(), expon); }
349 inline RR ldexp(RR a, int expon)
350 { return ::NTL::ldexp(a.value(), expon); }
353 { return ::NTL::log(a.value()); }
354 inline RR log10(RR a)
355 { return ::NTL::log10(a.value()); }
358 { return ::NTL::tan(a.value()); }
360 inline RR pow(RR a, RR b)
361 { return ::NTL::pow(a.value(), b.value()); }
362 inline RR pow(RR a, int b)
363 { return ::NTL::power(a.value(), b); }
365 { return ::NTL::sin(a.value()); }
368 { return ::NTL::sinh(a.value()); }
371 { return ::NTL::sqrt(a.value()); }
374 { return ::NTL::tanh(a.value()); }
376 inline RR pow(const RR& r, long l)
378 return ::NTL::power(r.value(), l);
380 inline RR tan(const RR& a)
382 return sin(a)/cos(a);
384 inline RR frexp(RR r, int* exp)
399 BOOST_ASSERT(r >= 0.5);
402 inline RR ldexp(RR r, int exp)
409 template <class charT, class traits>
410 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const RR& a)
412 return os << a.value();
414 template <class charT, class traits>
415 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, RR& a)
429 static ntl::RR lanczos_sum(const ntl::RR& z)
431 unsigned long p = ntl::RR::precision();
433 return lanczos13UDT::lanczos_sum(z);
435 return lanczos22UDT::lanczos_sum(z);
437 return lanczos31UDT::lanczos_sum(z);
438 else //if(p <= 370) approx 100 digit precision:
439 return lanczos61UDT::lanczos_sum(z);
441 static ntl::RR lanczos_sum_expG_scaled(const ntl::RR& z)
443 unsigned long p = ntl::RR::precision();
445 return lanczos13UDT::lanczos_sum_expG_scaled(z);
447 return lanczos22UDT::lanczos_sum_expG_scaled(z);
449 return lanczos31UDT::lanczos_sum_expG_scaled(z);
450 else //if(p <= 370) approx 100 digit precision:
451 return lanczos61UDT::lanczos_sum_expG_scaled(z);
453 static ntl::RR lanczos_sum_near_1(const ntl::RR& z)
455 unsigned long p = ntl::RR::precision();
457 return lanczos13UDT::lanczos_sum_near_1(z);
459 return lanczos22UDT::lanczos_sum_near_1(z);
461 return lanczos31UDT::lanczos_sum_near_1(z);
462 else //if(p <= 370) approx 100 digit precision:
463 return lanczos61UDT::lanczos_sum_near_1(z);
465 static ntl::RR lanczos_sum_near_2(const ntl::RR& z)
467 unsigned long p = ntl::RR::precision();
469 return lanczos13UDT::lanczos_sum_near_2(z);
471 return lanczos22UDT::lanczos_sum_near_2(z);
473 return lanczos31UDT::lanczos_sum_near_2(z);
474 else //if(p <= 370) approx 100 digit precision:
475 return lanczos61UDT::lanczos_sum_near_2(z);
479 unsigned long p = ntl::RR::precision();
481 return lanczos13UDT::g();
483 return lanczos22UDT::g();
485 return lanczos31UDT::g();
486 else //if(p <= 370) approx 100 digit precision:
487 return lanczos61UDT::g();
491 template<class Policy>
492 struct lanczos<ntl::RR, Policy>
494 typedef ntl_lanczos type;
497 } // namespace lanczos
503 inline int digits<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
505 return ::NTL::RR::precision();
509 inline float real_cast<float, boost::math::ntl::RR>(boost::math::ntl::RR t)
513 return static_cast<float>(r);
516 inline double real_cast<double, boost::math::ntl::RR>(boost::math::ntl::RR t)
526 void convert_to_long_result(NTL::RR const& r, I& result)
535 last_result = result;
536 result += static_cast<I>(term);
538 }while(result != last_result);
544 inline long double real_cast<long double, boost::math::ntl::RR>(boost::math::ntl::RR t)
546 long double result(0);
547 detail::convert_to_long_result(t.value(), result);
551 inline boost::math::ntl::RR real_cast<boost::math::ntl::RR, boost::math::ntl::RR>(boost::math::ntl::RR t)
556 inline unsigned real_cast<unsigned, boost::math::ntl::RR>(boost::math::ntl::RR t)
559 detail::convert_to_long_result(t.value(), result);
563 inline int real_cast<int, boost::math::ntl::RR>(boost::math::ntl::RR t)
566 detail::convert_to_long_result(t.value(), result);
570 inline long real_cast<long, boost::math::ntl::RR>(boost::math::ntl::RR t)
573 detail::convert_to_long_result(t.value(), result);
577 inline long long real_cast<long long, boost::math::ntl::RR>(boost::math::ntl::RR t)
580 detail::convert_to_long_result(t.value(), result);
585 inline boost::math::ntl::RR max_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
587 static bool has_init = false;
592 val.e = NTL_OVFBND-20;
599 inline boost::math::ntl::RR min_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
601 static bool has_init = false;
606 val.e = -NTL_OVFBND+20;
613 inline boost::math::ntl::RR log_max_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
615 static bool has_init = false;
620 val.e = NTL_OVFBND-20;
628 inline boost::math::ntl::RR log_min_value<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
630 static bool has_init = false;
635 val.e = -NTL_OVFBND+20;
643 inline boost::math::ntl::RR epsilon<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
645 return ldexp(boost::math::ntl::RR(1), 1-boost::math::policies::digits<boost::math::ntl::RR, boost::math::policies::policy<> >());
651 // The number of digits precision in RR can vary with each call
652 // so we need to recalculate these with each call:
656 template<> inline boost::math::ntl::RR pi<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
662 template<> inline boost::math::ntl::RR e<boost::math::ntl::RR>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(boost::math::ntl::RR))
669 } // namespace constants
673 // These are some fairly brain-dead versions of the math
674 // functions that NTL fails to provide.
679 // Inverse trig functions:
683 asin_root(RR const& target) : t(target){}
685 boost::math::tuple<RR, RR, RR> operator()(RR const& p)
691 return boost::math::make_tuple(f0, f1, f2);
701 return boost::math::tools::halley_iterate(
704 RR(-boost::math::constants::pi<RR>()/2),
705 RR(boost::math::constants::pi<RR>()/2),
706 NTL::RR::precision());
711 acos_root(RR const& target) : t(target){}
713 boost::math::tuple<RR, RR, RR> operator()(RR const& p)
719 return boost::math::make_tuple(f0, f1, f2);
729 return boost::math::tools::halley_iterate(
732 RR(-boost::math::constants::pi<RR>()/2),
733 RR(boost::math::constants::pi<RR>()/2),
734 NTL::RR::precision());
739 atan_root(RR const& target) : t(target){}
741 boost::math::tuple<RR, RR, RR> operator()(RR const& p)
747 RR f2 = 2 * ta / (c * c);
748 return boost::math::make_tuple(f0, f1, f2);
758 return boost::math::tools::halley_iterate(
761 -boost::math::constants::pi<RR>()/2,
762 boost::math::constants::pi<RR>()/2,
763 NTL::RR::precision());
766 inline RR atan2(RR y, RR x)
772 return y < 0 ? atan(y / x) - boost::math::constants::pi<RR>() : atan(y / x) + boost::math::constants::pi<RR>();
774 return y < 0 ? -boost::math::constants::half_pi<RR>() : boost::math::constants::half_pi<RR>() ;
779 return (expm1(z.value()) - expm1(-z.value())) / 2;
784 return (exp(z) + exp(-z)) / 2;
789 return sinh(z) / cosh(z);
792 inline RR fmod(RR x, RR y)
794 // This is a really crummy version of fmod, we rely on lots
795 // of digits to get us out of trouble...
796 RR factor = floor(x/y);
797 return x - factor * y;
800 template <class Policy>
801 inline int iround(RR const& x, const Policy& pol)
803 return tools::real_cast<int>(round(x, pol));
806 template <class Policy>
807 inline long lround(RR const& x, const Policy& pol)
809 return tools::real_cast<long>(round(x, pol));
812 template <class Policy>
813 inline long long llround(RR const& x, const Policy& pol)
815 return tools::real_cast<long long>(round(x, pol));
818 template <class Policy>
819 inline int itrunc(RR const& x, const Policy& pol)
821 return tools::real_cast<int>(trunc(x, pol));
824 template <class Policy>
825 inline long ltrunc(RR const& x, const Policy& pol)
827 return tools::real_cast<long>(trunc(x, pol));
830 template <class Policy>
831 inline long long lltrunc(RR const& x, const Policy& pol)
833 return tools::real_cast<long long>(trunc(x, pol));
840 template <class Policy>
841 ntl::RR digamma_imp(ntl::RR x, const mpl::int_<0>* , const Policy& pol)
844 // This handles reflection of negative arguments, and all our
845 // error handling, then forwards to the T-specific approximation.
847 BOOST_MATH_STD_USING // ADL of std functions.
851 // Check for negative arguments and use reflection:
857 // Argument reduction for tan:
858 ntl::RR remainder = x - floor(x);
859 // Shift to negative if > 0.5:
865 // check for evaluation at a negative pole:
869 return policies::raise_pole_error<ntl::RR>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
871 result = constants::pi<ntl::RR>() / tan(constants::pi<ntl::RR>() * remainder);
873 result += big_digamma(x);
877 } // namespace detail
882 #endif // BOOST_MATH_REAL_CONCEPT_HPP