1 // (C) Copyright John Maddock 2015.
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_REMEZ_MULTIPRECISION_HPP
7 #define BOOST_REMEZ_MULTIPRECISION_HPP
9 #include <boost/multiprecision/cpp_bin_float.hpp>
12 #include <boost/math/bindings/rr.hpp>
14 using boost::math::ntl::pow;
15 } // workaround for spirit parser.
17 typedef boost::math::ntl::RR mp_type;
19 inline void set_working_precision(int n)
21 NTL::RR::SetPrecision(working_precision);
24 inline int get_working_precision()
26 return mp_type::precision(working_precision);
29 inline void set_output_precision(int n)
31 NTL::RR::SetOutputPrecision(n);
34 inline mp_type round_to_precision(mp_type m, int bits)
36 return NTL::RoundToPrecision(m.value(), bits);
45 inline boost::multiprecision::cpp_bin_float_double_extended real_cast<boost::multiprecision::cpp_bin_float_double_extended, mp_type>(mp_type val)
47 unsigned p = NTL::RR::OutputPrecision();
48 NTL::RR::SetOutputPrecision(20);
49 boost::multiprecision::cpp_bin_float_double_extended r = boost::lexical_cast<boost::multiprecision::cpp_bin_float_double_extended>(val);
50 NTL::RR::SetOutputPrecision(p);
54 inline boost::multiprecision::cpp_bin_float_quad real_cast<boost::multiprecision::cpp_bin_float_quad, mp_type>(mp_type val)
56 unsigned p = NTL::RR::OutputPrecision();
57 NTL::RR::SetOutputPrecision(35);
58 boost::multiprecision::cpp_bin_float_quad r = boost::lexical_cast<boost::multiprecision::cpp_bin_float_quad>(val);
59 NTL::RR::SetOutputPrecision(p);
67 #elif defined(USE_CPP_BIN_FLOAT_100)
69 #include <boost/multiprecision/cpp_bin_float.hpp>
71 typedef boost::multiprecision::cpp_bin_float_100 mp_type;
73 inline void set_working_precision(int n)
77 inline void set_output_precision(int n)
79 std::cout << std::setprecision(n);
80 std::cerr << std::setprecision(n);
83 inline mp_type round_to_precision(mp_type m, int bits)
86 mp_type f = frexp(m, &i);
93 inline int get_working_precision()
95 return std::numeric_limits<mp_type>::digits;
103 inline boost::multiprecision::cpp_bin_float_double_extended real_cast<boost::multiprecision::cpp_bin_float_double_extended, mp_type>(mp_type val)
105 return boost::multiprecision::cpp_bin_float_double_extended(val);
108 inline boost::multiprecision::cpp_bin_float_quad real_cast<boost::multiprecision::cpp_bin_float_quad, mp_type>(mp_type val)
110 return boost::multiprecision::cpp_bin_float_quad(val);
118 #elif defined(USE_MPFR_100)
120 #include <boost/multiprecision/mpfr.hpp>
122 typedef boost::multiprecision::mpfr_float_100 mp_type;
124 inline void set_working_precision(int n)
128 inline void set_output_precision(int n)
130 std::cout << std::setprecision(n);
131 std::cerr << std::setprecision(n);
134 inline mp_type round_to_precision(mp_type m, int bits)
136 mpfr_prec_round(m.backend().data(), bits, MPFR_RNDN);
140 inline int get_working_precision()
142 return std::numeric_limits<mp_type>::digits;
150 inline boost::multiprecision::cpp_bin_float_double_extended real_cast<boost::multiprecision::cpp_bin_float_double_extended, mp_type>(mp_type val)
152 return boost::multiprecision::cpp_bin_float_double_extended(val);
155 inline boost::multiprecision::cpp_bin_float_quad real_cast<boost::multiprecision::cpp_bin_float_quad, mp_type>(mp_type val)
157 return boost::multiprecision::cpp_bin_float_quad(val);
167 #include <boost/multiprecision/mpfr.hpp>
169 typedef boost::multiprecision::mpfr_float mp_type;
171 inline void set_working_precision(int n)
173 boost::multiprecision::mpfr_float::default_precision(boost::multiprecision::detail::digits2_2_10(n));
176 inline void set_output_precision(int n)
178 std::cout << std::setprecision(n);
179 std::cerr << std::setprecision(n);
182 inline mp_type round_to_precision(mp_type m, int bits)
184 mpfr_prec_round(m.backend().data(), bits, MPFR_RNDN);
188 inline int get_working_precision()
190 return mp_type::default_precision();
198 inline boost::multiprecision::cpp_bin_float_double_extended real_cast<boost::multiprecision::cpp_bin_float_double_extended, mp_type>(mp_type val)
200 return boost::multiprecision::cpp_bin_float_double_extended(val);
203 inline boost::multiprecision::cpp_bin_float_quad real_cast<boost::multiprecision::cpp_bin_float_quad, mp_type>(mp_type val)
205 return boost::multiprecision::cpp_bin_float_quad(val);
219 #endif // BOOST_REMEZ_MULTIPRECISION_HPP