mpfi.hpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2011 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_BN_MPFI_HPP
  6. #define BOOST_MATH_BN_MPFI_HPP
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <cstdint>
  10. #include <type_traits>
  11. #include <boost/multiprecision/number.hpp>
  12. #include <boost/math/special_functions/fpclassify.hpp>
  13. #include <boost/multiprecision/detail/big_lanczos.hpp>
  14. #include <boost/multiprecision/detail/digits.hpp>
  15. #include <boost/multiprecision/detail/atomic.hpp>
  16. #include <boost/multiprecision/traits/max_digits10.hpp>
  17. #include <boost/multiprecision/mpfr.hpp>
  18. #include <boost/multiprecision/logged_adaptor.hpp>
  19. #include <boost/math/constants/constants.hpp>
  20. #include <boost/functional/hash_fwd.hpp>
  21. #include <mpfi.h>
  22. #ifndef BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION
  23. #define BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION 20
  24. #endif
  25. namespace boost {
  26. namespace multiprecision {
  27. namespace backends {
  28. template <unsigned digits10>
  29. struct mpfi_float_backend;
  30. } // namespace backends
  31. template <unsigned digits10>
  32. struct number_category<backends::mpfi_float_backend<digits10> > : public std::integral_constant<int, number_kind_floating_point>
  33. {};
  34. struct interval_error : public std::runtime_error
  35. {
  36. interval_error(const std::string& s) : std::runtime_error(s) {}
  37. };
  38. namespace backends {
  39. namespace detail {
  40. inline int mpfi_sgn(mpfi_srcptr p)
  41. {
  42. if (mpfi_is_zero(p))
  43. return 0;
  44. if (mpfi_is_strictly_pos(p))
  45. return 1;
  46. if (mpfi_is_strictly_neg(p))
  47. return -1;
  48. BOOST_THROW_EXCEPTION(interval_error("Sign of interval is ambiguous."));
  49. }
  50. template <unsigned digits10>
  51. struct mpfi_float_imp;
  52. template <unsigned digits10>
  53. struct mpfi_float_imp
  54. {
  55. #ifdef BOOST_HAS_LONG_LONG
  56. using signed_types = std::tuple<long, boost::long_long_type> ;
  57. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  58. #else
  59. using signed_types = std::tuple<long> ;
  60. using unsigned_types = std::tuple<unsigned long>;
  61. #endif
  62. using float_types = std::tuple<double, long double>;
  63. using exponent_type = long ;
  64. mpfi_float_imp()
  65. {
  66. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  67. mpfi_set_ui(m_data, 0u);
  68. }
  69. mpfi_float_imp(unsigned prec)
  70. {
  71. mpfi_init2(m_data, prec);
  72. mpfi_set_ui(m_data, 0u);
  73. }
  74. mpfi_float_imp(const mpfi_float_imp& o)
  75. {
  76. mpfi_init2(m_data, mpfi_get_prec(o.data()));
  77. if (o.m_data[0].left._mpfr_d)
  78. mpfi_set(m_data, o.m_data);
  79. }
  80. // rvalue copy
  81. mpfi_float_imp(mpfi_float_imp&& o) noexcept
  82. {
  83. m_data[0] = o.m_data[0];
  84. o.m_data[0].left._mpfr_d = 0;
  85. }
  86. mpfi_float_imp& operator=(const mpfi_float_imp& o)
  87. {
  88. if (m_data[0].left._mpfr_d == 0)
  89. mpfi_init2(m_data, mpfi_get_prec(o.data()));
  90. if (mpfi_get_prec(o.data()) != mpfi_get_prec(data()))
  91. {
  92. mpfi_float_imp t(mpfi_get_prec(o.data()));
  93. t = o;
  94. t.swap(*this);
  95. }
  96. else
  97. {
  98. if (o.m_data[0].left._mpfr_d)
  99. mpfi_set(m_data, o.m_data);
  100. }
  101. return *this;
  102. }
  103. // rvalue assign
  104. mpfi_float_imp& operator=(mpfi_float_imp&& o) noexcept
  105. {
  106. mpfi_swap(m_data, o.m_data);
  107. return *this;
  108. }
  109. #ifdef BOOST_HAS_LONG_LONG
  110. #ifdef _MPFR_H_HAVE_INTMAX_T
  111. mpfi_float_imp& operator=(boost::ulong_long_type i)
  112. {
  113. if (m_data[0].left._mpfr_d == 0)
  114. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  115. mpfr_set_uj(left_data(), i, GMP_RNDD);
  116. mpfr_set_uj(right_data(), i, GMP_RNDU);
  117. return *this;
  118. }
  119. mpfi_float_imp& operator=(boost::long_long_type i)
  120. {
  121. if (m_data[0].left._mpfr_d == 0)
  122. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  123. mpfr_set_sj(left_data(), i, GMP_RNDD);
  124. mpfr_set_sj(right_data(), i, GMP_RNDU);
  125. return *this;
  126. }
  127. #else
  128. mpfi_float_imp& operator=(boost::ulong_long_type i)
  129. {
  130. if (m_data[0].left._mpfr_d == 0)
  131. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  132. boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1u);
  133. unsigned shift = 0;
  134. mpfi_t t;
  135. mpfi_init2(t, (std::max)(static_cast<mpfr_prec_t>(std::numeric_limits<boost::ulong_long_type>::digits), static_cast<mpfr_prec_t>(multiprecision::detail::digits10_2_2(digits10))));
  136. mpfi_set_ui(m_data, 0);
  137. while (i)
  138. {
  139. mpfi_set_ui(t, static_cast<unsigned long>(i & mask));
  140. if (shift)
  141. mpfi_mul_2exp(t, t, shift);
  142. mpfi_add(m_data, m_data, t);
  143. shift += std::numeric_limits<unsigned long>::digits;
  144. i >>= std::numeric_limits<unsigned long>::digits;
  145. }
  146. mpfi_clear(t);
  147. return *this;
  148. }
  149. mpfi_float_imp& operator=(boost::long_long_type i)
  150. {
  151. if (m_data[0].left._mpfr_d == 0)
  152. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  153. bool neg = i < 0;
  154. *this = boost::multiprecision::detail::unsigned_abs(i);
  155. if (neg)
  156. mpfi_neg(m_data, m_data);
  157. return *this;
  158. }
  159. #endif
  160. #endif
  161. mpfi_float_imp& operator=(unsigned long i)
  162. {
  163. if (m_data[0].left._mpfr_d == 0)
  164. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  165. mpfi_set_ui(m_data, i);
  166. return *this;
  167. }
  168. mpfi_float_imp& operator=(long i)
  169. {
  170. if (m_data[0].left._mpfr_d == 0)
  171. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  172. mpfi_set_si(m_data, i);
  173. return *this;
  174. }
  175. mpfi_float_imp& operator=(double d)
  176. {
  177. if (m_data[0].left._mpfr_d == 0)
  178. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  179. mpfi_set_d(m_data, d);
  180. return *this;
  181. }
  182. mpfi_float_imp& operator=(long double a)
  183. {
  184. if (m_data[0].left._mpfr_d == 0)
  185. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  186. mpfr_set_ld(left_data(), a, GMP_RNDD);
  187. mpfr_set_ld(right_data(), a, GMP_RNDU);
  188. return *this;
  189. }
  190. mpfi_float_imp& operator=(const char* s)
  191. {
  192. using default_ops::eval_fpclassify;
  193. if (m_data[0].left._mpfr_d == 0)
  194. mpfi_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  195. if (s && (*s == '{'))
  196. {
  197. mpfr_float_backend<digits10> a, b;
  198. std::string part;
  199. const char* p = ++s;
  200. while (*p && (*p != ',') && (*p != '}'))
  201. ++p;
  202. part.assign(s + 1, p);
  203. a = part.c_str();
  204. s = p;
  205. if (*p && (*p != '}'))
  206. {
  207. ++p;
  208. while (*p && (*p != ',') && (*p != '}'))
  209. ++p;
  210. part.assign(s + 1, p);
  211. }
  212. else
  213. part.erase();
  214. b = part.c_str();
  215. if (eval_fpclassify(a) == (int)FP_NAN)
  216. {
  217. mpfi_set_fr(this->data(), a.data());
  218. }
  219. else if (eval_fpclassify(b) == (int)FP_NAN)
  220. {
  221. mpfi_set_fr(this->data(), b.data());
  222. }
  223. else
  224. {
  225. if (a.compare(b) > 0)
  226. {
  227. BOOST_THROW_EXCEPTION(std::runtime_error("Attempt to create interval with invalid range (start is greater than end)."));
  228. }
  229. mpfi_interv_fr(m_data, a.data(), b.data());
  230. }
  231. }
  232. else if (mpfi_set_str(m_data, s, 10) != 0)
  233. {
  234. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse string \"") + s + std::string("\"as a valid floating point number.")));
  235. }
  236. return *this;
  237. }
  238. void swap(mpfi_float_imp& o) noexcept
  239. {
  240. mpfi_swap(m_data, o.m_data);
  241. }
  242. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  243. {
  244. BOOST_ASSERT(m_data[0].left._mpfr_d);
  245. mpfr_float_backend<digits10> a, b;
  246. mpfi_get_left(a.data(), m_data);
  247. mpfi_get_right(b.data(), m_data);
  248. if (a.compare(b) == 0)
  249. return a.str(digits, f);
  250. return "{" + a.str(digits, f) + "," + b.str(digits, f) + "}";
  251. }
  252. ~mpfi_float_imp() noexcept
  253. {
  254. if (m_data[0].left._mpfr_d)
  255. mpfi_clear(m_data);
  256. }
  257. void negate() noexcept
  258. {
  259. BOOST_ASSERT(m_data[0].left._mpfr_d);
  260. mpfi_neg(m_data, m_data);
  261. }
  262. int compare(const mpfi_float_imp& o) const noexcept
  263. {
  264. BOOST_ASSERT(m_data[0].left._mpfr_d && o.m_data[0].left._mpfr_d);
  265. if (mpfr_cmp(right_data(), o.left_data()) < 0)
  266. return -1;
  267. if (mpfr_cmp(left_data(), o.right_data()) > 0)
  268. return 1;
  269. if ((mpfr_cmp(left_data(), o.left_data()) == 0) && (mpfr_cmp(right_data(), o.right_data()) == 0))
  270. return 0;
  271. BOOST_THROW_EXCEPTION(interval_error("Ambiguous comparison between two values."));
  272. return 0;
  273. }
  274. template <class V>
  275. int compare(V v) const noexcept
  276. {
  277. mpfi_float_imp d;
  278. d = v;
  279. return compare(d);
  280. }
  281. mpfi_t& data() noexcept
  282. {
  283. BOOST_ASSERT(m_data[0].left._mpfr_d);
  284. return m_data;
  285. }
  286. const mpfi_t& data() const noexcept
  287. {
  288. BOOST_ASSERT(m_data[0].left._mpfr_d);
  289. return m_data;
  290. }
  291. mpfr_ptr left_data() noexcept
  292. {
  293. BOOST_ASSERT(m_data[0].left._mpfr_d);
  294. return &(m_data[0].left);
  295. }
  296. mpfr_srcptr left_data() const noexcept
  297. {
  298. BOOST_ASSERT(m_data[0].left._mpfr_d);
  299. return &(m_data[0].left);
  300. }
  301. mpfr_ptr right_data() noexcept
  302. {
  303. BOOST_ASSERT(m_data[0].left._mpfr_d);
  304. return &(m_data[0].right);
  305. }
  306. mpfr_srcptr right_data() const noexcept
  307. {
  308. BOOST_ASSERT(m_data[0].left._mpfr_d);
  309. return &(m_data[0].right);
  310. }
  311. protected:
  312. mpfi_t m_data;
  313. static boost::multiprecision::detail::precision_type& get_default_precision() noexcept
  314. {
  315. static boost::multiprecision::detail::precision_type val(BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION);
  316. return val;
  317. }
  318. };
  319. } // namespace detail
  320. template <unsigned digits10>
  321. struct mpfi_float_backend : public detail::mpfi_float_imp<digits10>
  322. {
  323. mpfi_float_backend() : detail::mpfi_float_imp<digits10>() {}
  324. mpfi_float_backend(const mpfi_float_backend& o) : detail::mpfi_float_imp<digits10>(o) {}
  325. // rvalue copy
  326. mpfi_float_backend(mpfi_float_backend&& o) : detail::mpfi_float_imp<digits10>(static_cast<detail::mpfi_float_imp<digits10>&&>(o))
  327. {}
  328. template <unsigned D>
  329. mpfi_float_backend(const mpfi_float_backend<D>& val, typename std::enable_if<D <= digits10>::type* = 0)
  330. : detail::mpfi_float_imp<digits10>()
  331. {
  332. mpfi_set(this->m_data, val.data());
  333. }
  334. template <unsigned D>
  335. explicit mpfi_float_backend(const mpfi_float_backend<D>& val, typename std::enable_if<!(D <= digits10)>::type* = 0)
  336. : detail::mpfi_float_imp<digits10>()
  337. {
  338. mpfi_set(this->m_data, val.data());
  339. }
  340. mpfi_float_backend(const mpfi_t val)
  341. : detail::mpfi_float_imp<digits10>()
  342. {
  343. mpfi_set(this->m_data, val);
  344. }
  345. mpfi_float_backend& operator=(const mpfi_float_backend& o)
  346. {
  347. *static_cast<detail::mpfi_float_imp<digits10>*>(this) = static_cast<detail::mpfi_float_imp<digits10> const&>(o);
  348. return *this;
  349. }
  350. template <unsigned D>
  351. mpfi_float_backend(const mpfr_float_backend<D>& val, typename std::enable_if<D <= digits10>::type* = 0)
  352. : detail::mpfi_float_imp<digits10>()
  353. {
  354. mpfi_set_fr(this->m_data, val.data());
  355. }
  356. template <unsigned D>
  357. mpfi_float_backend& operator=(const mpfr_float_backend<D>& val)
  358. {
  359. mpfi_set_fr(this->m_data, val.data());
  360. return *this;
  361. }
  362. template <unsigned D>
  363. explicit mpfi_float_backend(const mpfr_float_backend<D>& val, typename std::enable_if<!(D <= digits10)>::type* = 0)
  364. : detail::mpfi_float_imp<digits10>()
  365. {
  366. mpfi_set_fr(this->m_data, val.data());
  367. }
  368. // rvalue copy
  369. mpfi_float_backend& operator=(mpfi_float_backend&& o) noexcept
  370. {
  371. *static_cast<detail::mpfi_float_imp<digits10>*>(this) = static_cast<detail::mpfi_float_imp<digits10>&&>(o);
  372. return *this;
  373. }
  374. template <class V>
  375. mpfi_float_backend& operator=(const V& v)
  376. {
  377. *static_cast<detail::mpfi_float_imp<digits10>*>(this) = v;
  378. return *this;
  379. }
  380. mpfi_float_backend& operator=(const mpfi_t val)
  381. {
  382. mpfi_set(this->m_data, val);
  383. return *this;
  384. }
  385. // We don't change our precision here, this is a fixed precision type:
  386. template <unsigned D>
  387. mpfi_float_backend& operator=(const mpfi_float_backend<D>& val)
  388. {
  389. mpfi_set(this->m_data, val.data());
  390. return *this;
  391. }
  392. };
  393. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  394. void assign_components(mpfi_float_backend<D1>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b);
  395. template <unsigned Digits10, class V>
  396. typename std::enable_if<std::is_constructible<number<mpfr_float_backend<Digits10, allocate_dynamic>, et_on>, V>::value || std::is_convertible<V, const char*>::value>::type
  397. assign_components(mpfi_float_backend<Digits10>& result, const V& a, const V& b);
  398. template <>
  399. struct mpfi_float_backend<0> : public detail::mpfi_float_imp<0>
  400. {
  401. mpfi_float_backend() : detail::mpfi_float_imp<0>() {}
  402. mpfi_float_backend(const mpfi_t val)
  403. : detail::mpfi_float_imp<0>(mpfi_get_prec(val))
  404. {
  405. mpfi_set(this->m_data, val);
  406. }
  407. mpfi_float_backend(const mpfi_float_backend& o) : detail::mpfi_float_imp<0>(o) {}
  408. // rvalue copy
  409. mpfi_float_backend(mpfi_float_backend&& o) noexcept : detail::mpfi_float_imp<0>(static_cast<detail::mpfi_float_imp<0>&&>(o))
  410. {}
  411. mpfi_float_backend(const mpfi_float_backend& o, unsigned digits10)
  412. : detail::mpfi_float_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  413. {
  414. mpfi_set(this->m_data, o.data());
  415. }
  416. template <class V>
  417. mpfi_float_backend(const V& a, const V& b, unsigned digits10)
  418. : detail::mpfi_float_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  419. {
  420. assign_components(*this, a, b);
  421. }
  422. template <unsigned D>
  423. mpfi_float_backend(const mpfi_float_backend<D>& val)
  424. : detail::mpfi_float_imp<0>(mpfi_get_prec(val.data()))
  425. {
  426. mpfi_set(this->m_data, val.data());
  427. }
  428. mpfi_float_backend& operator=(const mpfi_float_backend& o)
  429. {
  430. mpfi_set_prec(this->m_data, mpfi_get_prec(o.data()));
  431. mpfi_set(this->m_data, o.data());
  432. return *this;
  433. }
  434. // rvalue assign
  435. mpfi_float_backend& operator=(mpfi_float_backend&& o) noexcept
  436. {
  437. *static_cast<detail::mpfi_float_imp<0>*>(this) = static_cast<detail::mpfi_float_imp<0>&&>(o);
  438. return *this;
  439. }
  440. template <class V>
  441. mpfi_float_backend& operator=(const V& v)
  442. {
  443. *static_cast<detail::mpfi_float_imp<0>*>(this) = v;
  444. return *this;
  445. }
  446. mpfi_float_backend& operator=(const mpfi_t val)
  447. {
  448. mpfi_set_prec(this->m_data, mpfi_get_prec(val));
  449. mpfi_set(this->m_data, val);
  450. return *this;
  451. }
  452. template <unsigned D>
  453. mpfi_float_backend& operator=(const mpfi_float_backend<D>& val)
  454. {
  455. mpfi_set_prec(this->m_data, mpfi_get_prec(val.data()));
  456. mpfi_set(this->m_data, val.data());
  457. return *this;
  458. }
  459. static unsigned default_precision() noexcept
  460. {
  461. return get_default_precision();
  462. }
  463. static void default_precision(unsigned v) noexcept
  464. {
  465. get_default_precision() = v;
  466. }
  467. unsigned precision() const noexcept
  468. {
  469. return multiprecision::detail::digits2_2_10(mpfi_get_prec(this->m_data));
  470. }
  471. void precision(unsigned digits10) noexcept
  472. {
  473. mpfi_float_backend t(*this, digits10);
  474. this->swap(t);
  475. }
  476. };
  477. template <unsigned digits10, class T>
  478. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const mpfi_float_backend<digits10>& a, const T& b) noexcept
  479. {
  480. return a.compare(b) == 0;
  481. }
  482. template <unsigned digits10, class T>
  483. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const mpfi_float_backend<digits10>& a, const T& b) noexcept
  484. {
  485. return a.compare(b) < 0;
  486. }
  487. template <unsigned digits10, class T>
  488. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const mpfi_float_backend<digits10>& a, const T& b) noexcept
  489. {
  490. return a.compare(b) > 0;
  491. }
  492. template <unsigned D1, unsigned D2>
  493. inline void eval_add(mpfi_float_backend<D1>& result, const mpfi_float_backend<D2>& o)
  494. {
  495. mpfi_add(result.data(), result.data(), o.data());
  496. }
  497. template <unsigned D1, unsigned D2>
  498. inline void eval_subtract(mpfi_float_backend<D1>& result, const mpfi_float_backend<D2>& o)
  499. {
  500. mpfi_sub(result.data(), result.data(), o.data());
  501. }
  502. template <unsigned D1, unsigned D2>
  503. inline void eval_multiply(mpfi_float_backend<D1>& result, const mpfi_float_backend<D2>& o)
  504. {
  505. if ((void*)&result == (void*)&o)
  506. mpfi_sqr(result.data(), o.data());
  507. else
  508. mpfi_mul(result.data(), result.data(), o.data());
  509. }
  510. template <unsigned D1, unsigned D2>
  511. inline void eval_divide(mpfi_float_backend<D1>& result, const mpfi_float_backend<D2>& o)
  512. {
  513. mpfi_div(result.data(), result.data(), o.data());
  514. }
  515. template <unsigned digits10>
  516. inline void eval_add(mpfi_float_backend<digits10>& result, unsigned long i)
  517. {
  518. mpfi_add_ui(result.data(), result.data(), i);
  519. }
  520. template <unsigned digits10>
  521. inline void eval_subtract(mpfi_float_backend<digits10>& result, unsigned long i)
  522. {
  523. mpfi_sub_ui(result.data(), result.data(), i);
  524. }
  525. template <unsigned digits10>
  526. inline void eval_multiply(mpfi_float_backend<digits10>& result, unsigned long i)
  527. {
  528. mpfi_mul_ui(result.data(), result.data(), i);
  529. }
  530. template <unsigned digits10>
  531. inline void eval_divide(mpfi_float_backend<digits10>& result, unsigned long i)
  532. {
  533. mpfi_div_ui(result.data(), result.data(), i);
  534. }
  535. template <unsigned digits10>
  536. inline void eval_add(mpfi_float_backend<digits10>& result, long i)
  537. {
  538. if (i > 0)
  539. mpfi_add_ui(result.data(), result.data(), i);
  540. else
  541. mpfi_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  542. }
  543. template <unsigned digits10>
  544. inline void eval_subtract(mpfi_float_backend<digits10>& result, long i)
  545. {
  546. if (i > 0)
  547. mpfi_sub_ui(result.data(), result.data(), i);
  548. else
  549. mpfi_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  550. }
  551. template <unsigned digits10>
  552. inline void eval_multiply(mpfi_float_backend<digits10>& result, long i)
  553. {
  554. mpfi_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  555. if (i < 0)
  556. mpfi_neg(result.data(), result.data());
  557. }
  558. template <unsigned digits10>
  559. inline void eval_divide(mpfi_float_backend<digits10>& result, long i)
  560. {
  561. mpfi_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  562. if (i < 0)
  563. mpfi_neg(result.data(), result.data());
  564. }
  565. //
  566. // Specialised 3 arg versions of the basic operators:
  567. //
  568. template <unsigned D1, unsigned D2, unsigned D3>
  569. inline void eval_add(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, const mpfi_float_backend<D3>& y)
  570. {
  571. mpfi_add(a.data(), x.data(), y.data());
  572. }
  573. template <unsigned D1, unsigned D2>
  574. inline void eval_add(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, unsigned long y)
  575. {
  576. mpfi_add_ui(a.data(), x.data(), y);
  577. }
  578. template <unsigned D1, unsigned D2>
  579. inline void eval_add(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, long y)
  580. {
  581. if (y < 0)
  582. mpfi_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  583. else
  584. mpfi_add_ui(a.data(), x.data(), y);
  585. }
  586. template <unsigned D1, unsigned D2>
  587. inline void eval_add(mpfi_float_backend<D1>& a, unsigned long x, const mpfi_float_backend<D2>& y)
  588. {
  589. mpfi_add_ui(a.data(), y.data(), x);
  590. }
  591. template <unsigned D1, unsigned D2>
  592. inline void eval_add(mpfi_float_backend<D1>& a, long x, const mpfi_float_backend<D2>& y)
  593. {
  594. if (x < 0)
  595. {
  596. mpfi_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
  597. mpfi_neg(a.data(), a.data());
  598. }
  599. else
  600. mpfi_add_ui(a.data(), y.data(), x);
  601. }
  602. template <unsigned D1, unsigned D2, unsigned D3>
  603. inline void eval_subtract(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, const mpfi_float_backend<D3>& y)
  604. {
  605. mpfi_sub(a.data(), x.data(), y.data());
  606. }
  607. template <unsigned D1, unsigned D2>
  608. inline void eval_subtract(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, unsigned long y)
  609. {
  610. mpfi_sub_ui(a.data(), x.data(), y);
  611. }
  612. template <unsigned D1, unsigned D2>
  613. inline void eval_subtract(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, long y)
  614. {
  615. if (y < 0)
  616. mpfi_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  617. else
  618. mpfi_sub_ui(a.data(), x.data(), y);
  619. }
  620. template <unsigned D1, unsigned D2>
  621. inline void eval_subtract(mpfi_float_backend<D1>& a, unsigned long x, const mpfi_float_backend<D2>& y)
  622. {
  623. mpfi_ui_sub(a.data(), x, y.data());
  624. }
  625. template <unsigned D1, unsigned D2>
  626. inline void eval_subtract(mpfi_float_backend<D1>& a, long x, const mpfi_float_backend<D2>& y)
  627. {
  628. if (x < 0)
  629. {
  630. mpfi_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
  631. mpfi_neg(a.data(), a.data());
  632. }
  633. else
  634. mpfi_ui_sub(a.data(), x, y.data());
  635. }
  636. template <unsigned D1, unsigned D2, unsigned D3>
  637. inline void eval_multiply(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, const mpfi_float_backend<D3>& y)
  638. {
  639. if ((void*)&x == (void*)&y)
  640. mpfi_sqr(a.data(), x.data());
  641. else
  642. mpfi_mul(a.data(), x.data(), y.data());
  643. }
  644. template <unsigned D1, unsigned D2>
  645. inline void eval_multiply(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, unsigned long y)
  646. {
  647. mpfi_mul_ui(a.data(), x.data(), y);
  648. }
  649. template <unsigned D1, unsigned D2>
  650. inline void eval_multiply(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, long y)
  651. {
  652. if (y < 0)
  653. {
  654. mpfi_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  655. a.negate();
  656. }
  657. else
  658. mpfi_mul_ui(a.data(), x.data(), y);
  659. }
  660. template <unsigned D1, unsigned D2>
  661. inline void eval_multiply(mpfi_float_backend<D1>& a, unsigned long x, const mpfi_float_backend<D2>& y)
  662. {
  663. mpfi_mul_ui(a.data(), y.data(), x);
  664. }
  665. template <unsigned D1, unsigned D2>
  666. inline void eval_multiply(mpfi_float_backend<D1>& a, long x, const mpfi_float_backend<D2>& y)
  667. {
  668. if (x < 0)
  669. {
  670. mpfi_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
  671. mpfi_neg(a.data(), a.data());
  672. }
  673. else
  674. mpfi_mul_ui(a.data(), y.data(), x);
  675. }
  676. template <unsigned D1, unsigned D2, unsigned D3>
  677. inline void eval_divide(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, const mpfi_float_backend<D3>& y)
  678. {
  679. mpfi_div(a.data(), x.data(), y.data());
  680. }
  681. template <unsigned D1, unsigned D2>
  682. inline void eval_divide(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, unsigned long y)
  683. {
  684. mpfi_div_ui(a.data(), x.data(), y);
  685. }
  686. template <unsigned D1, unsigned D2>
  687. inline void eval_divide(mpfi_float_backend<D1>& a, const mpfi_float_backend<D2>& x, long y)
  688. {
  689. if (y < 0)
  690. {
  691. mpfi_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  692. a.negate();
  693. }
  694. else
  695. mpfi_div_ui(a.data(), x.data(), y);
  696. }
  697. template <unsigned D1, unsigned D2>
  698. inline void eval_divide(mpfi_float_backend<D1>& a, unsigned long x, const mpfi_float_backend<D2>& y)
  699. {
  700. mpfi_ui_div(a.data(), x, y.data());
  701. }
  702. template <unsigned D1, unsigned D2>
  703. inline void eval_divide(mpfi_float_backend<D1>& a, long x, const mpfi_float_backend<D2>& y)
  704. {
  705. if (x < 0)
  706. {
  707. mpfi_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
  708. mpfi_neg(a.data(), a.data());
  709. }
  710. else
  711. mpfi_ui_div(a.data(), x, y.data());
  712. }
  713. template <unsigned digits10>
  714. inline bool eval_is_zero(const mpfi_float_backend<digits10>& val) noexcept
  715. {
  716. return 0 != mpfi_is_zero(val.data());
  717. }
  718. template <unsigned digits10>
  719. inline int eval_get_sign(const mpfi_float_backend<digits10>& val)
  720. {
  721. return detail::mpfi_sgn(val.data());
  722. }
  723. template <unsigned digits10>
  724. inline void eval_convert_to(unsigned long* result, const mpfi_float_backend<digits10>& val)
  725. {
  726. mpfr_float_backend<digits10> t;
  727. mpfi_mid(t.data(), val.data());
  728. eval_convert_to(result, t);
  729. }
  730. template <unsigned digits10>
  731. inline void eval_convert_to(long* result, const mpfi_float_backend<digits10>& val)
  732. {
  733. mpfr_float_backend<digits10> t;
  734. mpfi_mid(t.data(), val.data());
  735. eval_convert_to(result, t);
  736. }
  737. #ifdef _MPFR_H_HAVE_INTMAX_T
  738. template <unsigned digits10>
  739. inline void eval_convert_to(boost::ulong_long_type* result, const mpfi_float_backend<digits10>& val)
  740. {
  741. mpfr_float_backend<digits10> t;
  742. mpfi_mid(t.data(), val.data());
  743. eval_convert_to(result, t);
  744. }
  745. template <unsigned digits10>
  746. inline void eval_convert_to(boost::long_long_type* result, const mpfi_float_backend<digits10>& val)
  747. {
  748. mpfr_float_backend<digits10> t;
  749. mpfi_mid(t.data(), val.data());
  750. eval_convert_to(result, t);
  751. }
  752. #endif
  753. template <unsigned digits10>
  754. inline void eval_convert_to(double* result, const mpfi_float_backend<digits10>& val) noexcept
  755. {
  756. *result = mpfi_get_d(val.data());
  757. }
  758. template <unsigned digits10>
  759. inline void eval_convert_to(long double* result, const mpfi_float_backend<digits10>& val) noexcept
  760. {
  761. mpfr_float_backend<digits10> t;
  762. mpfi_mid(t.data(), val.data());
  763. eval_convert_to(result, t);
  764. }
  765. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  766. inline void assign_components(mpfi_float_backend<D1>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b)
  767. {
  768. using default_ops::eval_fpclassify;
  769. if (eval_fpclassify(a) == (int)FP_NAN)
  770. {
  771. mpfi_set_fr(result.data(), a.data());
  772. }
  773. else if (eval_fpclassify(b) == (int)FP_NAN)
  774. {
  775. mpfi_set_fr(result.data(), b.data());
  776. }
  777. else
  778. {
  779. if (a.compare(b) > 0)
  780. {
  781. BOOST_THROW_EXCEPTION(std::runtime_error("Attempt to create interval with invalid range (start is greater than end)."));
  782. }
  783. mpfi_interv_fr(result.data(), a.data(), b.data());
  784. }
  785. }
  786. template <unsigned Digits10, class V>
  787. inline typename std::enable_if<std::is_constructible<number<mpfr_float_backend<Digits10, allocate_dynamic>, et_on>, V>::value || std::is_convertible<V, const char*>::value>::type
  788. assign_components(mpfi_float_backend<Digits10>& result, const V& a, const V& b)
  789. {
  790. number<mpfr_float_backend<Digits10, allocate_dynamic>, et_on> x(a), y(b);
  791. assign_components(result, x.backend(), y.backend());
  792. }
  793. //
  794. // Native non-member operations:
  795. //
  796. template <unsigned Digits10>
  797. inline void eval_sqrt(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val)
  798. {
  799. mpfi_sqrt(result.data(), val.data());
  800. }
  801. template <unsigned Digits10>
  802. inline void eval_abs(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val)
  803. {
  804. mpfi_abs(result.data(), val.data());
  805. }
  806. template <unsigned Digits10>
  807. inline void eval_fabs(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val)
  808. {
  809. mpfi_abs(result.data(), val.data());
  810. }
  811. template <unsigned Digits10>
  812. inline void eval_ceil(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val)
  813. {
  814. mpfr_float_backend<Digits10> a, b;
  815. mpfr_set(a.data(), val.left_data(), GMP_RNDN);
  816. mpfr_set(b.data(), val.right_data(), GMP_RNDN);
  817. eval_ceil(a, a);
  818. eval_ceil(b, b);
  819. if (a.compare(b) != 0)
  820. {
  821. BOOST_THROW_EXCEPTION(interval_error("Attempt to take the ceil of a value that straddles an integer boundary."));
  822. }
  823. mpfi_set_fr(result.data(), a.data());
  824. }
  825. template <unsigned Digits10>
  826. inline void eval_floor(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val)
  827. {
  828. mpfr_float_backend<Digits10> a, b;
  829. mpfr_set(a.data(), val.left_data(), GMP_RNDN);
  830. mpfr_set(b.data(), val.right_data(), GMP_RNDN);
  831. eval_floor(a, a);
  832. eval_floor(b, b);
  833. if (a.compare(b) != 0)
  834. {
  835. BOOST_THROW_EXCEPTION(interval_error("Attempt to take the floor of a value that straddles an integer boundary."));
  836. }
  837. mpfi_set_fr(result.data(), a.data());
  838. }
  839. template <unsigned Digits10>
  840. inline void eval_ldexp(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val, long e)
  841. {
  842. if (e > 0)
  843. mpfi_mul_2exp(result.data(), val.data(), e);
  844. else if (e < 0)
  845. mpfi_div_2exp(result.data(), val.data(), -e);
  846. else
  847. result = val;
  848. }
  849. template <unsigned Digits10>
  850. inline void eval_frexp(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val, int* e)
  851. {
  852. mpfr_float_backend<Digits10> t, rt;
  853. mpfi_mid(t.data(), val.data());
  854. eval_frexp(rt, t, e);
  855. eval_ldexp(result, val, -*e);
  856. }
  857. template <unsigned Digits10>
  858. inline void eval_frexp(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& val, long* e)
  859. {
  860. mpfr_float_backend<Digits10> t, rt;
  861. mpfi_mid(t.data(), val.data());
  862. eval_frexp(rt, t, e);
  863. eval_ldexp(result, val, -*e);
  864. }
  865. template <unsigned Digits10>
  866. inline int eval_fpclassify(const mpfi_float_backend<Digits10>& val) noexcept
  867. {
  868. return mpfi_inf_p(val.data()) ? FP_INFINITE : mpfi_nan_p(val.data()) ? FP_NAN : mpfi_is_zero(val.data()) ? FP_ZERO : FP_NORMAL;
  869. }
  870. template <unsigned Digits10>
  871. inline void eval_pow(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& b, const mpfi_float_backend<Digits10>& e)
  872. {
  873. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, mpfi_float_backend<Digits10> >::type;
  874. using default_ops::eval_get_sign;
  875. int s = eval_get_sign(b);
  876. if (s == 0)
  877. {
  878. if (eval_get_sign(e) == 0)
  879. {
  880. result = ui_type(1);
  881. }
  882. else
  883. {
  884. result = ui_type(0);
  885. }
  886. return;
  887. }
  888. if (s < 0)
  889. {
  890. if (eval_get_sign(e) < 0)
  891. {
  892. mpfi_float_backend<Digits10> t1, t2;
  893. t1 = e;
  894. t1.negate();
  895. eval_pow(t2, b, t1);
  896. t1 = ui_type(1);
  897. eval_divide(result, t1, t2);
  898. return;
  899. }
  900. typename boost::multiprecision::detail::canonical<std::uintmax_t, mpfi_float_backend<Digits10> >::type an;
  901. #ifndef BOOST_NO_EXCEPTIONS
  902. try
  903. {
  904. #endif
  905. using default_ops::eval_convert_to;
  906. eval_convert_to(&an, e);
  907. if (e.compare(an) == 0)
  908. {
  909. mpfi_float_backend<Digits10> pb(b);
  910. pb.negate();
  911. eval_pow(result, pb, e);
  912. if (an & 1u)
  913. result.negate();
  914. return;
  915. }
  916. #ifndef BOOST_NO_EXCEPTIONS
  917. }
  918. catch (const std::exception&)
  919. {
  920. // conversion failed, just fall through, value is not an integer.
  921. }
  922. #endif
  923. result = std::numeric_limits<number<mpfi_float_backend<Digits10>, et_on> >::quiet_NaN().backend();
  924. return;
  925. }
  926. mpfi_log(result.data(), b.data());
  927. mpfi_mul(result.data(), result.data(), e.data());
  928. mpfi_exp(result.data(), result.data());
  929. }
  930. template <unsigned Digits10>
  931. inline void eval_exp(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  932. {
  933. mpfi_exp(result.data(), arg.data());
  934. }
  935. template <unsigned Digits10>
  936. inline void eval_exp2(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  937. {
  938. mpfi_exp2(result.data(), arg.data());
  939. }
  940. template <unsigned Digits10>
  941. inline void eval_log(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  942. {
  943. mpfi_log(result.data(), arg.data());
  944. }
  945. template <unsigned Digits10>
  946. inline void eval_log10(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  947. {
  948. mpfi_log10(result.data(), arg.data());
  949. }
  950. template <unsigned Digits10>
  951. inline void eval_sin(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  952. {
  953. mpfi_sin(result.data(), arg.data());
  954. }
  955. template <unsigned Digits10>
  956. inline void eval_cos(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  957. {
  958. mpfi_cos(result.data(), arg.data());
  959. }
  960. template <unsigned Digits10>
  961. inline void eval_tan(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  962. {
  963. mpfi_tan(result.data(), arg.data());
  964. }
  965. template <unsigned Digits10>
  966. inline void eval_asin(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  967. {
  968. mpfi_asin(result.data(), arg.data());
  969. }
  970. template <unsigned Digits10>
  971. inline void eval_acos(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  972. {
  973. mpfi_acos(result.data(), arg.data());
  974. }
  975. template <unsigned Digits10>
  976. inline void eval_atan(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  977. {
  978. mpfi_atan(result.data(), arg.data());
  979. }
  980. template <unsigned Digits10>
  981. inline void eval_atan2(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg1, const mpfi_float_backend<Digits10>& arg2)
  982. {
  983. mpfi_atan2(result.data(), arg1.data(), arg2.data());
  984. }
  985. template <unsigned Digits10>
  986. inline void eval_sinh(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  987. {
  988. mpfi_sinh(result.data(), arg.data());
  989. }
  990. template <unsigned Digits10>
  991. inline void eval_cosh(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  992. {
  993. mpfi_cosh(result.data(), arg.data());
  994. }
  995. template <unsigned Digits10>
  996. inline void eval_tanh(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  997. {
  998. mpfi_tanh(result.data(), arg.data());
  999. }
  1000. template <unsigned Digits10>
  1001. inline void eval_log2(mpfi_float_backend<Digits10>& result, const mpfi_float_backend<Digits10>& arg)
  1002. {
  1003. mpfi_log2(result.data(), arg.data());
  1004. }
  1005. template <unsigned Digits10>
  1006. inline std::size_t hash_value(const mpfi_float_backend<Digits10>& val)
  1007. {
  1008. std::size_t result = 0;
  1009. std::size_t len = val.left_data()[0]._mpfr_prec / mp_bits_per_limb;
  1010. if (val.left_data()[0]._mpfr_prec % mp_bits_per_limb)
  1011. ++len;
  1012. for (std::size_t i = 0; i < len; ++i)
  1013. boost::hash_combine(result, val.left_data()[0]._mpfr_d[i]);
  1014. boost::hash_combine(result, val.left_data()[0]._mpfr_exp);
  1015. boost::hash_combine(result, val.left_data()[0]._mpfr_sign);
  1016. len = val.right_data()[0]._mpfr_prec / mp_bits_per_limb;
  1017. if (val.right_data()[0]._mpfr_prec % mp_bits_per_limb)
  1018. ++len;
  1019. for (std::size_t i = 0; i < len; ++i)
  1020. boost::hash_combine(result, val.right_data()[0]._mpfr_d[i]);
  1021. boost::hash_combine(result, val.right_data()[0]._mpfr_exp);
  1022. boost::hash_combine(result, val.right_data()[0]._mpfr_sign);
  1023. return result;
  1024. }
  1025. template <class To, unsigned D>
  1026. void generic_interconvert(To& to, const mpfi_float_backend<D>& from, const std::integral_constant<int, number_kind_integer>& to_type, const std::integral_constant<int, number_kind_floating_point>& from_type)
  1027. {
  1028. using boost::multiprecision::detail::generic_interconvert;
  1029. mpfr_float_backend<D> t;
  1030. mpfi_mid(t.data(), from.data());
  1031. generic_interconvert(to, t, to_type, from_type);
  1032. }
  1033. template <class To, unsigned D>
  1034. void generic_interconvert(To& to, const mpfi_float_backend<D>& from, const std::integral_constant<int, number_kind_rational>& to_type, const std::integral_constant<int, number_kind_floating_point>& from_type)
  1035. {
  1036. using boost::multiprecision::detail::generic_interconvert;
  1037. mpfr_float_backend<D> t;
  1038. mpfi_mid(t.data(), from.data());
  1039. generic_interconvert(to, t, to_type, from_type);
  1040. }
  1041. template <class To, unsigned D>
  1042. void generic_interconvert(To& to, const mpfi_float_backend<D>& from, const std::integral_constant<int, number_kind_floating_point>& to_type, const std::integral_constant<int, number_kind_floating_point>& from_type)
  1043. {
  1044. using boost::multiprecision::detail::generic_interconvert;
  1045. mpfr_float_backend<D> t;
  1046. mpfi_mid(t.data(), from.data());
  1047. generic_interconvert(to, t, to_type, from_type);
  1048. }
  1049. } // namespace backends
  1050. namespace detail {
  1051. template <>
  1052. struct is_variable_precision<backends::mpfi_float_backend<0> > : public std::integral_constant<bool, true>
  1053. {};
  1054. } // namespace detail
  1055. template <>
  1056. struct number_category<detail::canonical<mpfi_t, backends::mpfi_float_backend<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  1057. {};
  1058. template <unsigned Digits10>
  1059. struct is_interval_number<backends::mpfi_float_backend<Digits10> > : public std::integral_constant<bool, true>
  1060. {};
  1061. using boost::multiprecision::backends::mpfi_float_backend;
  1062. using mpfi_float_50 = number<mpfi_float_backend<50> > ;
  1063. using mpfi_float_100 = number<mpfi_float_backend<100> > ;
  1064. using mpfi_float_500 = number<mpfi_float_backend<500> > ;
  1065. using mpfi_float_1000 = number<mpfi_float_backend<1000> >;
  1066. using mpfi_float = number<mpfi_float_backend<0> > ;
  1067. //
  1068. // Special interval specific functions:
  1069. //
  1070. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1071. inline number<mpfr_float_backend<Digits10>, ExpressionTemplates> lower(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val)
  1072. {
  1073. boost::multiprecision::detail::scoped_default_precision<number<mpfr_float_backend<Digits10>, ExpressionTemplates> > precision_guard(val);
  1074. number<mpfr_float_backend<Digits10> > result;
  1075. mpfr_set(result.backend().data(), val.backend().left_data(), GMP_RNDN);
  1076. return result;
  1077. }
  1078. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1079. inline number<mpfr_float_backend<Digits10>, ExpressionTemplates> upper(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val)
  1080. {
  1081. boost::multiprecision::detail::scoped_default_precision<number<mpfr_float_backend<Digits10>, ExpressionTemplates> > precision_guard(val);
  1082. number<mpfr_float_backend<Digits10> > result;
  1083. mpfr_set(result.backend().data(), val.backend().right_data(), GMP_RNDN);
  1084. return result;
  1085. }
  1086. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1087. inline number<mpfr_float_backend<Digits10>, ExpressionTemplates> median(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val)
  1088. {
  1089. boost::multiprecision::detail::scoped_default_precision<number<mpfr_float_backend<Digits10>, ExpressionTemplates> > precision_guard(val);
  1090. number<mpfr_float_backend<Digits10> > result;
  1091. mpfi_mid(result.backend().data(), val.backend().data());
  1092. return result;
  1093. }
  1094. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1095. inline number<mpfr_float_backend<Digits10>, ExpressionTemplates> width(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val)
  1096. {
  1097. boost::multiprecision::detail::scoped_default_precision<number<mpfr_float_backend<Digits10>, ExpressionTemplates> > precision_guard(val);
  1098. number<mpfr_float_backend<Digits10> > result;
  1099. mpfi_diam_abs(result.backend().data(), val.backend().data());
  1100. return result;
  1101. }
  1102. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1103. inline number<mpfi_float_backend<Digits10>, ExpressionTemplates> intersect(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b)
  1104. {
  1105. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(a, b);
  1106. number<mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1107. mpfi_intersect(result.backend().data(), a.backend().data(), b.backend().data());
  1108. return result;
  1109. }
  1110. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1111. inline number<mpfi_float_backend<Digits10>, ExpressionTemplates> hull(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b)
  1112. {
  1113. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(a, b);
  1114. number<mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1115. mpfi_union(result.backend().data(), a.backend().data(), b.backend().data());
  1116. return result;
  1117. }
  1118. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1119. inline bool overlap(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b)
  1120. {
  1121. return (lower(a) <= lower(b) && lower(b) <= upper(a)) ||
  1122. (lower(b) <= lower(a) && lower(a) <= upper(b));
  1123. }
  1124. template <unsigned Digits10, expression_template_option ExpressionTemplates1, expression_template_option ExpressionTemplates2>
  1125. inline bool in(const number<mpfr_float_backend<Digits10>, ExpressionTemplates1>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates2>& b)
  1126. {
  1127. return mpfi_is_inside_fr(a.backend().data(), b.backend().data()) != 0;
  1128. }
  1129. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1130. inline bool zero_in(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a)
  1131. {
  1132. return mpfi_has_zero(a.backend().data()) != 0;
  1133. }
  1134. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1135. inline bool subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b)
  1136. {
  1137. return mpfi_is_inside(a.backend().data(), b.backend().data()) != 0;
  1138. }
  1139. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1140. inline bool proper_subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a, const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b)
  1141. {
  1142. return mpfi_is_strictly_inside(a.backend().data(), b.backend().data()) != 0;
  1143. }
  1144. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1145. inline bool empty(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a)
  1146. {
  1147. return mpfi_is_empty(a.backend().data()) != 0;
  1148. }
  1149. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1150. inline bool singleton(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a)
  1151. {
  1152. return mpfr_cmp(a.backend().left_data(), a.backend().right_data()) == 0;
  1153. }
  1154. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1155. struct component_type<number<mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1156. {
  1157. using type = number<mpfr_float_backend<Digits10>, ExpressionTemplates>;
  1158. };
  1159. //
  1160. // Overloaded special functions which call native mpfr routines:
  1161. //
  1162. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1163. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1164. {
  1165. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1166. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1167. mpfi_asinh(result.backend().data(), arg.backend().data());
  1168. return result;
  1169. }
  1170. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1171. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1172. {
  1173. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1174. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1175. mpfi_acosh(result.backend().data(), arg.backend().data());
  1176. return result;
  1177. }
  1178. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1179. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1180. {
  1181. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1182. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1183. mpfi_atanh(result.backend().data(), arg.backend().data());
  1184. return result;
  1185. }
  1186. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1187. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1188. {
  1189. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1190. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1191. mpfi_cbrt(result.backend().data(), arg.backend().data());
  1192. return result;
  1193. }
  1194. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1195. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1196. {
  1197. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1198. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1199. mpfi_expm1(result.backend().data(), arg.backend().data());
  1200. return result;
  1201. }
  1202. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1203. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>& arg)
  1204. {
  1205. boost::multiprecision::detail::scoped_default_precision<number<mpfi_float_backend<Digits10>, ExpressionTemplates> > precision_guard(arg);
  1206. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> result;
  1207. mpfi_log1p(result.backend().data(), arg.backend().data());
  1208. return result;
  1209. }
  1210. } // namespace multiprecision
  1211. namespace math {
  1212. namespace tools {
  1213. inline void set_output_precision(const boost::multiprecision::mpfi_float& val, std::ostream& os)
  1214. {
  1215. os << std::setprecision(val.precision());
  1216. }
  1217. template <>
  1218. inline int digits<boost::multiprecision::mpfi_float>()
  1219. #ifdef BOOST_MATH_NOEXCEPT
  1220. noexcept
  1221. #endif
  1222. {
  1223. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpfi_float::default_precision());
  1224. }
  1225. template <>
  1226. inline int digits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off> >()
  1227. #ifdef BOOST_MATH_NOEXCEPT
  1228. noexcept
  1229. #endif
  1230. {
  1231. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpfi_float::default_precision());
  1232. }
  1233. template <>
  1234. inline boost::multiprecision::mpfi_float
  1235. max_value<boost::multiprecision::mpfi_float>()
  1236. {
  1237. boost::multiprecision::mpfi_float result(0.5);
  1238. mpfi_mul_2exp(result.backend().data(), result.backend().data(), mpfr_get_emax());
  1239. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1240. return result;
  1241. }
  1242. template <>
  1243. inline boost::multiprecision::mpfi_float
  1244. min_value<boost::multiprecision::mpfi_float>()
  1245. {
  1246. boost::multiprecision::mpfi_float result(0.5);
  1247. mpfi_div_2exp(result.backend().data(), result.backend().data(), -mpfr_get_emin());
  1248. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1249. return result;
  1250. }
  1251. template <>
  1252. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off>
  1253. max_value<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off> >()
  1254. {
  1255. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off> result(0.5);
  1256. mpfi_mul_2exp(result.backend().data(), result.backend().data(), mpfr_get_emax());
  1257. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1258. return result;
  1259. }
  1260. template <>
  1261. inline boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off>
  1262. min_value<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off> >()
  1263. {
  1264. boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, boost::multiprecision::et_off> result(0.5);
  1265. mpfi_div_2exp(result.backend().data(), result.backend().data(), -mpfr_get_emin());
  1266. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1267. return result;
  1268. }
  1269. // mpfi gets used with logged_adaptor fairly often, so specialize for that use case as well:
  1270. using logged_type1 = boost::multiprecision::number<boost::multiprecision::backends::logged_adaptor<boost::multiprecision::mpfi_float::backend_type>, boost::multiprecision::et_on> ;
  1271. using logged_type2 = boost::multiprecision::number<boost::multiprecision::backends::logged_adaptor<boost::multiprecision::mpfi_float::backend_type>, boost::multiprecision::et_off>;
  1272. template <>
  1273. inline int digits<logged_type1>()
  1274. #ifdef BOOST_MATH_NOEXCEPT
  1275. noexcept
  1276. #endif
  1277. {
  1278. return multiprecision::detail::digits10_2_2(logged_type1::default_precision());
  1279. }
  1280. template <>
  1281. inline int digits<logged_type2>()
  1282. #ifdef BOOST_MATH_NOEXCEPT
  1283. noexcept
  1284. #endif
  1285. {
  1286. return multiprecision::detail::digits10_2_2(logged_type1::default_precision());
  1287. }
  1288. template <>
  1289. inline logged_type1
  1290. max_value<logged_type1>()
  1291. {
  1292. logged_type1 result(0.5);
  1293. mpfi_mul_2exp(result.backend().value().data(), result.backend().value().data(), mpfr_get_emax());
  1294. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1295. return result;
  1296. }
  1297. template <>
  1298. inline logged_type1
  1299. min_value<logged_type1>()
  1300. {
  1301. logged_type1 result(0.5);
  1302. mpfi_div_2exp(result.backend().value().data(), result.backend().value().data(), -mpfr_get_emin());
  1303. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1304. return result;
  1305. }
  1306. template <>
  1307. inline logged_type2
  1308. max_value<logged_type2>()
  1309. {
  1310. logged_type2 result(0.5);
  1311. mpfi_mul_2exp(result.backend().value().data(), result.backend().value().data(), mpfr_get_emax());
  1312. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1313. return result;
  1314. }
  1315. template <>
  1316. inline logged_type2
  1317. min_value<logged_type2>()
  1318. {
  1319. logged_type2 result(0.5);
  1320. mpfi_div_2exp(result.backend().value().data(), result.backend().value().data(), -mpfr_get_emin());
  1321. //BOOST_ASSERT(mpfi_number_p(result.backend().data()));
  1322. return result;
  1323. }
  1324. } // namespace tools
  1325. namespace constants { namespace detail {
  1326. template <class T>
  1327. struct constant_pi;
  1328. template <class T>
  1329. struct constant_ln_two;
  1330. template <class T>
  1331. struct constant_euler;
  1332. template <class T>
  1333. struct constant_catalan;
  1334. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1335. struct constant_pi<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1336. {
  1337. using result_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>;
  1338. template <int N>
  1339. static inline const result_type& get(const std::integral_constant<int, N>&)
  1340. {
  1341. static result_type result;
  1342. static bool has_init = false;
  1343. if (!has_init)
  1344. {
  1345. has_init = true;
  1346. mpfi_const_pi(result.backend().data());
  1347. }
  1348. return result;
  1349. }
  1350. static inline result_type get(const std::integral_constant<int, 0>&)
  1351. {
  1352. result_type result;
  1353. mpfi_const_pi(result.backend().data());
  1354. return result;
  1355. }
  1356. };
  1357. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1358. struct constant_ln_two<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1359. {
  1360. using result_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>;
  1361. template <int N>
  1362. static inline const result_type& get(const std::integral_constant<int, N>&)
  1363. {
  1364. static result_type result;
  1365. static bool has_init = false;
  1366. if (!has_init)
  1367. {
  1368. has_init = true;
  1369. mpfi_const_log2(result.backend().data());
  1370. }
  1371. return result;
  1372. }
  1373. static inline result_type get(const std::integral_constant<int, 0>&)
  1374. {
  1375. result_type result;
  1376. mpfi_const_log2(result.backend().data());
  1377. return result;
  1378. }
  1379. };
  1380. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1381. struct constant_euler<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1382. {
  1383. using result_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>;
  1384. template <int N>
  1385. static inline const result_type& get(const std::integral_constant<int, N>&)
  1386. {
  1387. static result_type result;
  1388. static bool has_init = false;
  1389. if (!has_init)
  1390. {
  1391. has_init = true;
  1392. mpfi_const_euler(result.backend().data());
  1393. }
  1394. return result;
  1395. }
  1396. static inline result_type get(const std::integral_constant<int, 0>&)
  1397. {
  1398. result_type result;
  1399. mpfi_const_euler(result.backend().data());
  1400. return result;
  1401. }
  1402. };
  1403. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1404. struct constant_catalan<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1405. {
  1406. using result_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>;
  1407. template <int N>
  1408. static inline const result_type& get(const std::integral_constant<int, N>&)
  1409. {
  1410. static result_type result;
  1411. static bool has_init = false;
  1412. if (!has_init)
  1413. {
  1414. has_init = true;
  1415. mpfi_const_catalan(result.backend().data());
  1416. }
  1417. return result;
  1418. }
  1419. static inline result_type get(const std::integral_constant<int, 0>&)
  1420. {
  1421. result_type result;
  1422. mpfi_const_catalan(result.backend().data());
  1423. return result;
  1424. }
  1425. };
  1426. }} // namespace constants::detail
  1427. } // namespace math
  1428. } // namespace boost
  1429. namespace std {
  1430. //
  1431. // numeric_limits [partial] specializations for the types declared in this header:
  1432. //
  1433. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1434. class numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >
  1435. {
  1436. using number_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates>;
  1437. public:
  1438. static constexpr bool is_specialized = true;
  1439. static number_type(min)()
  1440. {
  1441. static std::pair<bool, number_type> value;
  1442. if (!value.first)
  1443. {
  1444. value.first = true;
  1445. value.second = 0.5;
  1446. mpfi_div_2exp(value.second.backend().data(), value.second.backend().data(), -mpfr_get_emin());
  1447. }
  1448. return value.second;
  1449. }
  1450. static number_type(max)()
  1451. {
  1452. static std::pair<bool, number_type> value;
  1453. if (!value.first)
  1454. {
  1455. value.first = true;
  1456. value.second = 0.5;
  1457. mpfi_mul_2exp(value.second.backend().data(), value.second.backend().data(), mpfr_get_emax());
  1458. }
  1459. return value.second;
  1460. }
  1461. static constexpr number_type lowest()
  1462. {
  1463. return -(max)();
  1464. }
  1465. static constexpr int digits = static_cast<int>((Digits10 * 1000L) / 301L + ((Digits10 * 1000L) % 301 ? 2 : 1));
  1466. static constexpr int digits10 = Digits10;
  1467. // Is this really correct???
  1468. static constexpr int max_digits10 = boost::multiprecision::detail::calc_max_digits10<digits>::value;
  1469. static constexpr bool is_signed = true;
  1470. static constexpr bool is_integer = false;
  1471. static constexpr bool is_exact = false;
  1472. static constexpr int radix = 2;
  1473. static number_type epsilon()
  1474. {
  1475. static std::pair<bool, number_type> value;
  1476. if (!value.first)
  1477. {
  1478. value.first = true;
  1479. value.second = 1;
  1480. mpfi_div_2exp(value.second.backend().data(), value.second.backend().data(), std::numeric_limits<number_type>::digits - 1);
  1481. }
  1482. return value.second;
  1483. }
  1484. // What value should this be????
  1485. static number_type round_error()
  1486. {
  1487. // returns epsilon/2
  1488. static std::pair<bool, number_type> value;
  1489. if (!value.first)
  1490. {
  1491. value.first = true;
  1492. value.second = 1;
  1493. mpfi_div_2exp(value.second.backend().data(), value.second.backend().data(), 1);
  1494. }
  1495. return value.second;
  1496. }
  1497. static constexpr long min_exponent = MPFR_EMIN_DEFAULT;
  1498. static constexpr long min_exponent10 = (MPFR_EMIN_DEFAULT / 1000) * 301L;
  1499. static constexpr long max_exponent = MPFR_EMAX_DEFAULT;
  1500. static constexpr long max_exponent10 = (MPFR_EMAX_DEFAULT / 1000) * 301L;
  1501. static constexpr bool has_infinity = true;
  1502. static constexpr bool has_quiet_NaN = true;
  1503. static constexpr bool has_signaling_NaN = false;
  1504. static constexpr float_denorm_style has_denorm = denorm_absent;
  1505. static constexpr bool has_denorm_loss = false;
  1506. static number_type infinity()
  1507. {
  1508. static std::pair<bool, number_type> value;
  1509. if (!value.first)
  1510. {
  1511. boost::multiprecision::mpfr_float_backend<Digits10> t;
  1512. mpfr_set_inf(t.data(), 1);
  1513. value.first = true;
  1514. mpfi_set_fr(value.second.backend().data(), t.data());
  1515. }
  1516. return value.second;
  1517. }
  1518. static number_type quiet_NaN()
  1519. {
  1520. static std::pair<bool, number_type> value;
  1521. if (!value.first)
  1522. {
  1523. boost::multiprecision::mpfr_float_backend<Digits10> t;
  1524. mpfr_set_nan(t.data());
  1525. value.first = true;
  1526. mpfi_set_fr(value.second.backend().data(), t.data());
  1527. }
  1528. return value.second;
  1529. }
  1530. static constexpr number_type signaling_NaN()
  1531. {
  1532. return number_type(0);
  1533. }
  1534. static constexpr number_type denorm_min() { return number_type(0); }
  1535. static constexpr bool is_iec559 = false;
  1536. static constexpr bool is_bounded = true;
  1537. static constexpr bool is_modulo = false;
  1538. static constexpr bool traps = true;
  1539. static constexpr bool tinyness_before = false;
  1540. static constexpr float_round_style round_style = round_to_nearest;
  1541. };
  1542. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1543. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::digits;
  1544. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1545. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::digits10;
  1546. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1547. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::max_digits10;
  1548. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1549. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_signed;
  1550. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1551. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_integer;
  1552. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1553. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_exact;
  1554. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1555. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::radix;
  1556. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1557. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::min_exponent;
  1558. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1559. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::min_exponent10;
  1560. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1561. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::max_exponent;
  1562. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1563. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::max_exponent10;
  1564. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1565. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::has_infinity;
  1566. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1567. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::has_quiet_NaN;
  1568. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1569. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::has_signaling_NaN;
  1570. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1571. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::has_denorm;
  1572. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1573. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::has_denorm_loss;
  1574. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1575. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_iec559;
  1576. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1577. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_bounded;
  1578. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1579. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::is_modulo;
  1580. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1581. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::traps;
  1582. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1583. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::tinyness_before;
  1584. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  1585. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<Digits10>, ExpressionTemplates> >::round_style;
  1586. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1587. class numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >
  1588. {
  1589. using number_type = boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates>;
  1590. public:
  1591. static constexpr bool is_specialized = false;
  1592. static number_type(min)() { return number_type(0); }
  1593. static number_type(max)() { return number_type(0); }
  1594. static number_type lowest() { return number_type(0); }
  1595. static constexpr int digits = 0;
  1596. static constexpr int digits10 = 0;
  1597. static constexpr int max_digits10 = 0;
  1598. static constexpr bool is_signed = false;
  1599. static constexpr bool is_integer = false;
  1600. static constexpr bool is_exact = false;
  1601. static constexpr int radix = 0;
  1602. static number_type epsilon() { return number_type(0); }
  1603. static number_type round_error() { return number_type(0); }
  1604. static constexpr int min_exponent = 0;
  1605. static constexpr int min_exponent10 = 0;
  1606. static constexpr int max_exponent = 0;
  1607. static constexpr int max_exponent10 = 0;
  1608. static constexpr bool has_infinity = false;
  1609. static constexpr bool has_quiet_NaN = false;
  1610. static constexpr bool has_signaling_NaN = false;
  1611. static constexpr float_denorm_style has_denorm = denorm_absent;
  1612. static constexpr bool has_denorm_loss = false;
  1613. static number_type infinity() { return number_type(0); }
  1614. static number_type quiet_NaN() { return number_type(0); }
  1615. static number_type signaling_NaN() { return number_type(0); }
  1616. static number_type denorm_min() { return number_type(0); }
  1617. static constexpr bool is_iec559 = false;
  1618. static constexpr bool is_bounded = false;
  1619. static constexpr bool is_modulo = false;
  1620. static constexpr bool traps = false;
  1621. static constexpr bool tinyness_before = false;
  1622. static constexpr float_round_style round_style = round_toward_zero;
  1623. };
  1624. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1625. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::digits;
  1626. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1627. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::digits10;
  1628. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1629. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::max_digits10;
  1630. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1631. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_signed;
  1632. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1633. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_integer;
  1634. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1635. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_exact;
  1636. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1637. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::radix;
  1638. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1639. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::min_exponent;
  1640. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1641. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::min_exponent10;
  1642. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1643. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::max_exponent;
  1644. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1645. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::max_exponent10;
  1646. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1647. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::has_infinity;
  1648. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1649. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::has_quiet_NaN;
  1650. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1651. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::has_signaling_NaN;
  1652. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1653. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::has_denorm;
  1654. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1655. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::has_denorm_loss;
  1656. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1657. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_iec559;
  1658. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1659. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_bounded;
  1660. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1661. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::is_modulo;
  1662. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1663. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::traps;
  1664. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1665. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::tinyness_before;
  1666. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  1667. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfi_float_backend<0>, ExpressionTemplates> >::round_style;
  1668. } // namespace std
  1669. #endif