trig.hpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Copyright Christopher Kormanyos 2002 - 2011.
  2. // Copyright 2011 John Maddock.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // This work is based on an earlier work:
  7. // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
  8. // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
  9. //
  10. // This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
  11. //
  12. #include <boost/multiprecision/detail/standalone_config.hpp>
  13. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  14. #include <boost/multiprecision/detail/assert.hpp>
  15. #ifdef BOOST_MSVC
  16. #pragma warning(push)
  17. #pragma warning(disable : 6326) // comparison of two constants
  18. #pragma warning(disable : 4127) // conditional expression is constant
  19. #endif
  20. template <class T>
  21. void hyp0F1(T& result, const T& b, const T& x)
  22. {
  23. using si_type = typename boost::multiprecision::detail::canonical<std::int32_t, T>::type ;
  24. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  25. // Compute the series representation of Hypergeometric0F1 taken from
  26. // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F1/06/01/01/
  27. // There are no checks on input range or parameter boundaries.
  28. T x_pow_n_div_n_fact(x);
  29. T pochham_b(b);
  30. T bp(b);
  31. eval_divide(result, x_pow_n_div_n_fact, pochham_b);
  32. eval_add(result, ui_type(1));
  33. si_type n;
  34. T tol;
  35. tol = ui_type(1);
  36. eval_ldexp(tol, tol, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  37. eval_multiply(tol, result);
  38. if (eval_get_sign(tol) < 0)
  39. tol.negate();
  40. T term;
  41. const int series_limit =
  42. boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
  43. ? 100
  44. : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
  45. // Series expansion of hyperg_0f1(; b; x).
  46. for (n = 2; n < series_limit; ++n)
  47. {
  48. eval_multiply(x_pow_n_div_n_fact, x);
  49. eval_divide(x_pow_n_div_n_fact, n);
  50. eval_increment(bp);
  51. eval_multiply(pochham_b, bp);
  52. eval_divide(term, x_pow_n_div_n_fact, pochham_b);
  53. eval_add(result, term);
  54. bool neg_term = eval_get_sign(term) < 0;
  55. if (neg_term)
  56. term.negate();
  57. if (term.compare(tol) <= 0)
  58. break;
  59. }
  60. if (n >= series_limit)
  61. BOOST_MP_THROW_EXCEPTION(std::runtime_error("H0F1 Failed to Converge"));
  62. }
  63. template <class T, unsigned N, bool b = boost::multiprecision::detail::is_variable_precision<boost::multiprecision::number<T> >::value>
  64. struct scoped_N_precision
  65. {
  66. template <class U>
  67. scoped_N_precision(U const&) {}
  68. template <class U>
  69. void reduce(U&) {}
  70. };
  71. template <class T, unsigned N>
  72. struct scoped_N_precision<T, N, true>
  73. {
  74. unsigned old_precision, old_arg_precision;
  75. scoped_N_precision(T& arg)
  76. {
  77. old_precision = T::thread_default_precision();
  78. old_arg_precision = arg.precision();
  79. T::thread_default_precision(old_arg_precision * N);
  80. arg.precision(old_arg_precision * N);
  81. }
  82. ~scoped_N_precision()
  83. {
  84. T::thread_default_precision(old_precision);
  85. }
  86. void reduce(T& arg)
  87. {
  88. arg.precision(old_arg_precision);
  89. }
  90. };
  91. template <class T>
  92. void reduce_n_half_pi(T& arg, const T& n, bool go_down)
  93. {
  94. //
  95. // We need to perform argument reduction at 3 times the precision of arg
  96. // in order to ensure a correct result up to arg = 1/epsilon. Beyond that
  97. // the value of n will have been incorrectly calculated anyway since it will
  98. // have a value greater than 1/epsilon and no longer be an exact integer value.
  99. //
  100. // More information in ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  101. //
  102. // There are two mutually exclusive ways to achieve this, both of which are
  103. // supported here:
  104. // 1) To define a fixed precision type with 3 times the precision for the calculation.
  105. // 2) To dynamically increase the precision of the variables.
  106. //
  107. using reduction_type = typename boost::multiprecision::detail::transcendental_reduction_type<T>::type;
  108. //
  109. // Make a copy of the arg at higher precision:
  110. //
  111. reduction_type big_arg(arg);
  112. //
  113. // Dynamically increase precision when supported, this increases the default
  114. // and ups the precision of big_arg to match:
  115. //
  116. scoped_N_precision<T, 3> scoped_precision(big_arg);
  117. //
  118. // High precision PI:
  119. //
  120. reduction_type reduction = get_constant_pi<reduction_type>();
  121. eval_ldexp(reduction, reduction, -1); // divide by 2
  122. eval_multiply(reduction, n);
  123. BOOST_MATH_INSTRUMENT_CODE(big_arg.str(10, std::ios_base::scientific));
  124. BOOST_MATH_INSTRUMENT_CODE(reduction.str(10, std::ios_base::scientific));
  125. if (go_down)
  126. eval_subtract(big_arg, reduction, big_arg);
  127. else
  128. eval_subtract(big_arg, reduction);
  129. arg = T(big_arg);
  130. //
  131. // If arg is a variable precision type, then we have just copied the
  132. // precision of big_arg s well it's value. Reduce the precision now:
  133. //
  134. scoped_precision.reduce(arg);
  135. BOOST_MATH_INSTRUMENT_CODE(big_arg.str(10, std::ios_base::scientific));
  136. BOOST_MATH_INSTRUMENT_CODE(arg.str(10, std::ios_base::scientific));
  137. }
  138. template <class T>
  139. void eval_sin(T& result, const T& x)
  140. {
  141. static_assert(number_category<T>::value == number_kind_floating_point, "The sin function is only valid for floating point types.");
  142. BOOST_MATH_INSTRUMENT_CODE(x.str(0, std::ios_base::scientific));
  143. if (&result == &x)
  144. {
  145. T temp;
  146. eval_sin(temp, x);
  147. result = temp;
  148. return;
  149. }
  150. using si_type = typename boost::multiprecision::detail::canonical<std::int32_t, T>::type ;
  151. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  152. using fp_type = typename std::tuple_element<0, typename T::float_types>::type;
  153. switch (eval_fpclassify(x))
  154. {
  155. case FP_INFINITE:
  156. case FP_NAN:
  157. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  158. {
  159. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  160. errno = EDOM;
  161. }
  162. else
  163. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  164. return;
  165. case FP_ZERO:
  166. result = x;
  167. return;
  168. default:;
  169. }
  170. // Local copy of the argument
  171. T xx = x;
  172. // Analyze and prepare the phase of the argument.
  173. // Make a local, positive copy of the argument, xx.
  174. // The argument xx will be reduced to 0 <= xx <= pi/2.
  175. bool b_negate_sin = false;
  176. if (eval_get_sign(x) < 0)
  177. {
  178. xx.negate();
  179. b_negate_sin = !b_negate_sin;
  180. }
  181. T n_pi, t;
  182. T half_pi = get_constant_pi<T>();
  183. eval_ldexp(half_pi, half_pi, -1); // divide by 2
  184. // Remove multiples of pi/2.
  185. if (xx.compare(half_pi) > 0)
  186. {
  187. eval_divide(n_pi, xx, half_pi);
  188. eval_trunc(n_pi, n_pi);
  189. t = ui_type(4);
  190. eval_fmod(t, n_pi, t);
  191. bool b_go_down = false;
  192. if (t.compare(ui_type(1)) == 0)
  193. {
  194. b_go_down = true;
  195. }
  196. else if (t.compare(ui_type(2)) == 0)
  197. {
  198. b_negate_sin = !b_negate_sin;
  199. }
  200. else if (t.compare(ui_type(3)) == 0)
  201. {
  202. b_negate_sin = !b_negate_sin;
  203. b_go_down = true;
  204. }
  205. if (b_go_down)
  206. eval_increment(n_pi);
  207. //
  208. // If n_pi is > 1/epsilon, then it is no longer an exact integer value
  209. // but an approximation. As a result we can no longer reliably reduce
  210. // xx to 0 <= xx < pi/2, nor can we tell the sign of the result as we need
  211. // n_pi % 4 for that, but that will always be zero in this situation.
  212. // We could use a higher precision type for n_pi, along with division at
  213. // higher precision, but that's rather expensive. So for now we do not support
  214. // this, and will see if anyone complains and has a legitimate use case.
  215. //
  216. if (n_pi.compare(get_constant_one_over_epsilon<T>()) > 0)
  217. {
  218. result = ui_type(0);
  219. return;
  220. }
  221. reduce_n_half_pi(xx, n_pi, b_go_down);
  222. //
  223. // Post reduction we may be a few ulp below zero or above pi/2
  224. // given that n_pi was calculated at working precision and not
  225. // at the higher precision used for reduction. Correct that now:
  226. //
  227. if (eval_get_sign(xx) < 0)
  228. {
  229. xx.negate();
  230. b_negate_sin = !b_negate_sin;
  231. }
  232. if (xx.compare(half_pi) > 0)
  233. {
  234. eval_ldexp(half_pi, half_pi, 1);
  235. eval_subtract(xx, half_pi, xx);
  236. eval_ldexp(half_pi, half_pi, -1);
  237. b_go_down = !b_go_down;
  238. }
  239. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  240. BOOST_MATH_INSTRUMENT_CODE(n_pi.str(0, std::ios_base::scientific));
  241. BOOST_MP_ASSERT(xx.compare(half_pi) <= 0);
  242. BOOST_MP_ASSERT(xx.compare(ui_type(0)) >= 0);
  243. }
  244. t = half_pi;
  245. eval_subtract(t, xx);
  246. const bool b_zero = eval_get_sign(xx) == 0;
  247. const bool b_pi_half = eval_get_sign(t) == 0;
  248. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  249. BOOST_MATH_INSTRUMENT_CODE(t.str(0, std::ios_base::scientific));
  250. // Check if the reduced argument is very close to 0 or pi/2.
  251. const bool b_near_zero = xx.compare(fp_type(1e-1)) < 0;
  252. const bool b_near_pi_half = t.compare(fp_type(1e-1)) < 0;
  253. if (b_zero)
  254. {
  255. result = ui_type(0);
  256. }
  257. else if (b_pi_half)
  258. {
  259. result = ui_type(1);
  260. }
  261. else if (b_near_zero)
  262. {
  263. eval_multiply(t, xx, xx);
  264. eval_divide(t, si_type(-4));
  265. T t2;
  266. t2 = fp_type(1.5);
  267. hyp0F1(result, t2, t);
  268. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  269. eval_multiply(result, xx);
  270. }
  271. else if (b_near_pi_half)
  272. {
  273. eval_multiply(t, t);
  274. eval_divide(t, si_type(-4));
  275. T t2;
  276. t2 = fp_type(0.5);
  277. hyp0F1(result, t2, t);
  278. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  279. }
  280. else
  281. {
  282. // Scale to a small argument for an efficient Taylor series,
  283. // implemented as a hypergeometric function. Use a standard
  284. // divide by three identity a certain number of times.
  285. // Here we use division by 3^9 --> (19683 = 3^9).
  286. constexpr si_type n_scale = 9;
  287. constexpr si_type n_three_pow_scale = static_cast<si_type>(19683L);
  288. eval_divide(xx, n_three_pow_scale);
  289. // Now with small arguments, we are ready for a series expansion.
  290. eval_multiply(t, xx, xx);
  291. eval_divide(t, si_type(-4));
  292. T t2;
  293. t2 = fp_type(1.5);
  294. hyp0F1(result, t2, t);
  295. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  296. eval_multiply(result, xx);
  297. // Convert back using multiple angle identity.
  298. for (std::int32_t k = static_cast<std::int32_t>(0); k < n_scale; k++)
  299. {
  300. // Rescale the cosine value using the multiple angle identity.
  301. eval_multiply(t2, result, ui_type(3));
  302. eval_multiply(t, result, result);
  303. eval_multiply(t, result);
  304. eval_multiply(t, ui_type(4));
  305. eval_subtract(result, t2, t);
  306. }
  307. }
  308. if (b_negate_sin)
  309. result.negate();
  310. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  311. }
  312. template <class T>
  313. void eval_cos(T& result, const T& x)
  314. {
  315. static_assert(number_category<T>::value == number_kind_floating_point, "The cos function is only valid for floating point types.");
  316. if (&result == &x)
  317. {
  318. T temp;
  319. eval_cos(temp, x);
  320. result = temp;
  321. return;
  322. }
  323. using si_type = typename boost::multiprecision::detail::canonical<std::int32_t, T>::type ;
  324. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  325. switch (eval_fpclassify(x))
  326. {
  327. case FP_INFINITE:
  328. case FP_NAN:
  329. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  330. {
  331. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  332. errno = EDOM;
  333. }
  334. else
  335. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  336. return;
  337. case FP_ZERO:
  338. result = ui_type(1);
  339. return;
  340. default:;
  341. }
  342. // Local copy of the argument
  343. T xx = x;
  344. // Analyze and prepare the phase of the argument.
  345. // Make a local, positive copy of the argument, xx.
  346. // The argument xx will be reduced to 0 <= xx <= pi/2.
  347. bool b_negate_cos = false;
  348. if (eval_get_sign(x) < 0)
  349. {
  350. xx.negate();
  351. }
  352. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  353. T n_pi, t;
  354. T half_pi = get_constant_pi<T>();
  355. eval_ldexp(half_pi, half_pi, -1); // divide by 2
  356. // Remove even multiples of pi.
  357. if (xx.compare(half_pi) > 0)
  358. {
  359. eval_divide(t, xx, half_pi);
  360. eval_trunc(n_pi, t);
  361. //
  362. // If n_pi is > 1/epsilon, then it is no longer an exact integer value
  363. // but an approximation. As a result we can no longer reliably reduce
  364. // xx to 0 <= xx < pi/2, nor can we tell the sign of the result as we need
  365. // n_pi % 4 for that, but that will always be zero in this situation.
  366. // We could use a higher precision type for n_pi, along with division at
  367. // higher precision, but that's rather expensive. So for now we do not support
  368. // this, and will see if anyone complains and has a legitimate use case.
  369. //
  370. if (n_pi.compare(get_constant_one_over_epsilon<T>()) > 0)
  371. {
  372. result = ui_type(1);
  373. return;
  374. }
  375. BOOST_MATH_INSTRUMENT_CODE(n_pi.str(0, std::ios_base::scientific));
  376. t = ui_type(4);
  377. eval_fmod(t, n_pi, t);
  378. bool b_go_down = false;
  379. if (t.compare(ui_type(0)) == 0)
  380. {
  381. b_go_down = true;
  382. }
  383. else if (t.compare(ui_type(1)) == 0)
  384. {
  385. b_negate_cos = true;
  386. }
  387. else if (t.compare(ui_type(2)) == 0)
  388. {
  389. b_go_down = true;
  390. b_negate_cos = true;
  391. }
  392. else
  393. {
  394. BOOST_MP_ASSERT(t.compare(ui_type(3)) == 0);
  395. }
  396. if (b_go_down)
  397. eval_increment(n_pi);
  398. reduce_n_half_pi(xx, n_pi, b_go_down);
  399. //
  400. // Post reduction we may be a few ulp below zero or above pi/2
  401. // given that n_pi was calculated at working precision and not
  402. // at the higher precision used for reduction. Correct that now:
  403. //
  404. if (eval_get_sign(xx) < 0)
  405. {
  406. xx.negate();
  407. b_negate_cos = !b_negate_cos;
  408. }
  409. if (xx.compare(half_pi) > 0)
  410. {
  411. eval_ldexp(half_pi, half_pi, 1);
  412. eval_subtract(xx, half_pi, xx);
  413. eval_ldexp(half_pi, half_pi, -1);
  414. }
  415. BOOST_MP_ASSERT(xx.compare(half_pi) <= 0);
  416. BOOST_MP_ASSERT(xx.compare(ui_type(0)) >= 0);
  417. }
  418. else
  419. {
  420. n_pi = ui_type(1);
  421. reduce_n_half_pi(xx, n_pi, true);
  422. }
  423. const bool b_zero = eval_get_sign(xx) == 0;
  424. if (b_zero)
  425. {
  426. result = si_type(0);
  427. }
  428. else
  429. {
  430. eval_sin(result, xx);
  431. }
  432. if (b_negate_cos)
  433. result.negate();
  434. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  435. }
  436. template <class T>
  437. void eval_tan(T& result, const T& x)
  438. {
  439. static_assert(number_category<T>::value == number_kind_floating_point, "The tan function is only valid for floating point types.");
  440. if (&result == &x)
  441. {
  442. T temp;
  443. eval_tan(temp, x);
  444. result = temp;
  445. return;
  446. }
  447. T t;
  448. eval_sin(result, x);
  449. eval_cos(t, x);
  450. eval_divide(result, t);
  451. }
  452. template <class T>
  453. void hyp2F1(T& result, const T& a, const T& b, const T& c, const T& x)
  454. {
  455. // Compute the series representation of hyperg_2f1 taken from
  456. // Abramowitz and Stegun 15.1.1.
  457. // There are no checks on input range or parameter boundaries.
  458. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  459. T x_pow_n_div_n_fact(x);
  460. T pochham_a(a);
  461. T pochham_b(b);
  462. T pochham_c(c);
  463. T ap(a);
  464. T bp(b);
  465. T cp(c);
  466. eval_multiply(result, pochham_a, pochham_b);
  467. eval_divide(result, pochham_c);
  468. eval_multiply(result, x_pow_n_div_n_fact);
  469. eval_add(result, ui_type(1));
  470. T lim;
  471. eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  472. if (eval_get_sign(lim) < 0)
  473. lim.negate();
  474. ui_type n;
  475. T term;
  476. const unsigned series_limit =
  477. boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
  478. ? 100
  479. : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
  480. // Series expansion of hyperg_2f1(a, b; c; x).
  481. for (n = 2; n < series_limit; ++n)
  482. {
  483. eval_multiply(x_pow_n_div_n_fact, x);
  484. eval_divide(x_pow_n_div_n_fact, n);
  485. eval_increment(ap);
  486. eval_multiply(pochham_a, ap);
  487. eval_increment(bp);
  488. eval_multiply(pochham_b, bp);
  489. eval_increment(cp);
  490. eval_multiply(pochham_c, cp);
  491. eval_multiply(term, pochham_a, pochham_b);
  492. eval_divide(term, pochham_c);
  493. eval_multiply(term, x_pow_n_div_n_fact);
  494. eval_add(result, term);
  495. if (eval_get_sign(term) < 0)
  496. term.negate();
  497. if (lim.compare(term) >= 0)
  498. break;
  499. }
  500. if (n > series_limit)
  501. BOOST_MP_THROW_EXCEPTION(std::runtime_error("H2F1 failed to converge."));
  502. }
  503. template <class T>
  504. void eval_asin(T& result, const T& x)
  505. {
  506. static_assert(number_category<T>::value == number_kind_floating_point, "The asin function is only valid for floating point types.");
  507. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  508. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  509. if (&result == &x)
  510. {
  511. T t(x);
  512. eval_asin(result, t);
  513. return;
  514. }
  515. switch (eval_fpclassify(x))
  516. {
  517. case FP_NAN:
  518. case FP_INFINITE:
  519. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  520. {
  521. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  522. errno = EDOM;
  523. }
  524. else
  525. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  526. return;
  527. case FP_ZERO:
  528. result = x;
  529. return;
  530. default:;
  531. }
  532. const bool b_neg = eval_get_sign(x) < 0;
  533. T xx(x);
  534. if (b_neg)
  535. xx.negate();
  536. int c = xx.compare(ui_type(1));
  537. if (c > 0)
  538. {
  539. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  540. {
  541. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  542. errno = EDOM;
  543. }
  544. else
  545. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  546. return;
  547. }
  548. else if (c == 0)
  549. {
  550. result = get_constant_pi<T>();
  551. eval_ldexp(result, result, -1);
  552. if (b_neg)
  553. result.negate();
  554. return;
  555. }
  556. if (xx.compare(fp_type(1e-3)) < 0)
  557. {
  558. // http://functions.wolfram.com/ElementaryFunctions/ArcSin/26/01/01/
  559. eval_multiply(xx, xx);
  560. T t1, t2;
  561. t1 = fp_type(0.5f);
  562. t2 = fp_type(1.5f);
  563. hyp2F1(result, t1, t1, t2, xx);
  564. eval_multiply(result, x);
  565. return;
  566. }
  567. else if (xx.compare(fp_type(1 - 5e-2f)) > 0)
  568. {
  569. // http://functions.wolfram.com/ElementaryFunctions/ArcSin/26/01/01/
  570. // This branch is simlilar in complexity to Newton iterations down to
  571. // the above limit. It is *much* more accurate.
  572. T dx1;
  573. T t1, t2;
  574. eval_subtract(dx1, ui_type(1), xx);
  575. t1 = fp_type(0.5f);
  576. t2 = fp_type(1.5f);
  577. eval_ldexp(dx1, dx1, -1);
  578. hyp2F1(result, t1, t1, t2, dx1);
  579. eval_ldexp(dx1, dx1, 2);
  580. eval_sqrt(t1, dx1);
  581. eval_multiply(result, t1);
  582. eval_ldexp(t1, get_constant_pi<T>(), -1);
  583. result.negate();
  584. eval_add(result, t1);
  585. if (b_neg)
  586. result.negate();
  587. return;
  588. }
  589. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  590. using guess_type = typename boost::multiprecision::detail::canonical<long double, T>::type;
  591. #else
  592. using guess_type = fp_type;
  593. #endif
  594. // Get initial estimate using standard math function asin.
  595. guess_type dd;
  596. eval_convert_to(&dd, xx);
  597. result = (guess_type)(std::asin(dd));
  598. // Newton-Raphson iteration, we should double our precision with each iteration,
  599. // in practice this seems to not quite work in all cases... so terminate when we
  600. // have at least 2/3 of the digits correct on the assumption that the correction
  601. // we've just added will finish the job...
  602. std::intmax_t current_precision = eval_ilogb(result);
  603. std::intmax_t target_precision = std::numeric_limits<number<T> >::is_specialized ?
  604. current_precision - 1 - (std::numeric_limits<number<T> >::digits * 2) / 3
  605. : current_precision - 1 - (boost::multiprecision::detail::digits2<number<T> >::value() * 2) / 3;
  606. // Newton-Raphson iteration
  607. while (current_precision > target_precision)
  608. {
  609. T sine, cosine;
  610. eval_sin(sine, result);
  611. eval_cos(cosine, result);
  612. eval_subtract(sine, xx);
  613. eval_divide(sine, cosine);
  614. eval_subtract(result, sine);
  615. current_precision = eval_ilogb(sine);
  616. if (current_precision <= (std::numeric_limits<typename T::exponent_type>::min)() + 1)
  617. break;
  618. }
  619. if (b_neg)
  620. result.negate();
  621. }
  622. template <class T>
  623. inline void eval_acos(T& result, const T& x)
  624. {
  625. static_assert(number_category<T>::value == number_kind_floating_point, "The acos function is only valid for floating point types.");
  626. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  627. switch (eval_fpclassify(x))
  628. {
  629. case FP_NAN:
  630. case FP_INFINITE:
  631. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  632. {
  633. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  634. errno = EDOM;
  635. }
  636. else
  637. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  638. return;
  639. case FP_ZERO:
  640. result = get_constant_pi<T>();
  641. eval_ldexp(result, result, -1); // divide by two.
  642. return;
  643. default:
  644. break;
  645. }
  646. T xx;
  647. eval_abs(xx, x);
  648. int c = xx.compare(ui_type(1));
  649. if (c > 0)
  650. {
  651. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  652. {
  653. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  654. errno = EDOM;
  655. }
  656. else
  657. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  658. return;
  659. }
  660. else if (c == 0)
  661. {
  662. if (eval_get_sign(x) < 0)
  663. result = get_constant_pi<T>();
  664. else
  665. result = ui_type(0);
  666. return;
  667. }
  668. using fp_type = typename std::tuple_element<0, typename T::float_types>::type;
  669. if (xx.compare(fp_type(1e-3)) < 0)
  670. {
  671. // https://functions.wolfram.com/ElementaryFunctions/ArcCos/26/01/01/
  672. eval_multiply(xx, xx);
  673. T t1, t2;
  674. t1 = fp_type(0.5f);
  675. t2 = fp_type(1.5f);
  676. hyp2F1(result, t1, t1, t2, xx);
  677. eval_multiply(result, x);
  678. eval_ldexp(t1, get_constant_pi<T>(), -1);
  679. result.negate();
  680. eval_add(result, t1);
  681. return;
  682. }
  683. if (eval_get_sign(x) < 0)
  684. {
  685. eval_acos(result, xx);
  686. result.negate();
  687. eval_add(result, get_constant_pi<T>());
  688. return;
  689. }
  690. else if (xx.compare(fp_type(0.85)) > 0)
  691. {
  692. // https://functions.wolfram.com/ElementaryFunctions/ArcCos/26/01/01/
  693. // This branch is simlilar in complexity to Newton iterations down to
  694. // the above limit. It is *much* more accurate.
  695. T dx1;
  696. T t1, t2;
  697. eval_subtract(dx1, ui_type(1), xx);
  698. t1 = fp_type(0.5f);
  699. t2 = fp_type(1.5f);
  700. eval_ldexp(dx1, dx1, -1);
  701. hyp2F1(result, t1, t1, t2, dx1);
  702. eval_ldexp(dx1, dx1, 2);
  703. eval_sqrt(t1, dx1);
  704. eval_multiply(result, t1);
  705. return;
  706. }
  707. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  708. using guess_type = typename boost::multiprecision::detail::canonical<long double, T>::type;
  709. #else
  710. using guess_type = fp_type;
  711. #endif
  712. // Get initial estimate using standard math function asin.
  713. guess_type dd;
  714. eval_convert_to(&dd, xx);
  715. result = (guess_type)(std::acos(dd));
  716. // Newton-Raphson iteration, we should double our precision with each iteration,
  717. // in practice this seems to not quite work in all cases... so terminate when we
  718. // have at least 2/3 of the digits correct on the assumption that the correction
  719. // we've just added will finish the job...
  720. std::intmax_t current_precision = eval_ilogb(result);
  721. std::intmax_t target_precision = std::numeric_limits<number<T> >::is_specialized ?
  722. current_precision - 1 - (std::numeric_limits<number<T> >::digits * 2) / 3
  723. : current_precision - 1 - (boost::multiprecision::detail::digits2<number<T> >::value() * 2) / 3;
  724. // Newton-Raphson iteration
  725. while (current_precision > target_precision)
  726. {
  727. T sine, cosine;
  728. eval_sin(sine, result);
  729. eval_cos(cosine, result);
  730. eval_subtract(cosine, xx);
  731. cosine.negate();
  732. eval_divide(cosine, sine);
  733. eval_subtract(result, cosine);
  734. current_precision = eval_ilogb(cosine);
  735. if (current_precision <= (std::numeric_limits<typename T::exponent_type>::min)() + 1)
  736. break;
  737. }
  738. }
  739. template <class T>
  740. void eval_atan(T& result, const T& x)
  741. {
  742. static_assert(number_category<T>::value == number_kind_floating_point, "The atan function is only valid for floating point types.");
  743. using si_type = typename boost::multiprecision::detail::canonical<std::int32_t, T>::type ;
  744. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  745. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  746. switch (eval_fpclassify(x))
  747. {
  748. case FP_NAN:
  749. result = x;
  750. errno = EDOM;
  751. return;
  752. case FP_ZERO:
  753. result = x;
  754. return;
  755. case FP_INFINITE:
  756. if (eval_get_sign(x) < 0)
  757. {
  758. eval_ldexp(result, get_constant_pi<T>(), -1);
  759. result.negate();
  760. }
  761. else
  762. eval_ldexp(result, get_constant_pi<T>(), -1);
  763. return;
  764. default:;
  765. }
  766. const bool b_neg = eval_get_sign(x) < 0;
  767. T xx(x);
  768. if (b_neg)
  769. xx.negate();
  770. if (xx.compare(fp_type(0.1)) < 0)
  771. {
  772. T t1, t2, t3;
  773. t1 = ui_type(1);
  774. t2 = fp_type(0.5f);
  775. t3 = fp_type(1.5f);
  776. eval_multiply(xx, xx);
  777. xx.negate();
  778. hyp2F1(result, t1, t2, t3, xx);
  779. eval_multiply(result, x);
  780. return;
  781. }
  782. if (xx.compare(fp_type(10)) > 0)
  783. {
  784. T t1, t2, t3;
  785. t1 = fp_type(0.5f);
  786. t2 = ui_type(1u);
  787. t3 = fp_type(1.5f);
  788. eval_multiply(xx, xx);
  789. eval_divide(xx, si_type(-1), xx);
  790. hyp2F1(result, t1, t2, t3, xx);
  791. eval_divide(result, x);
  792. if (!b_neg)
  793. result.negate();
  794. eval_ldexp(t1, get_constant_pi<T>(), -1);
  795. eval_add(result, t1);
  796. if (b_neg)
  797. result.negate();
  798. return;
  799. }
  800. // Get initial estimate using standard math function atan.
  801. fp_type d;
  802. eval_convert_to(&d, xx);
  803. result = fp_type(std::atan(d));
  804. // Newton-Raphson iteration, we should double our precision with each iteration,
  805. // in practice this seems to not quite work in all cases... so terminate when we
  806. // have at least 2/3 of the digits correct on the assumption that the correction
  807. // we've just added will finish the job...
  808. std::intmax_t current_precision = eval_ilogb(result);
  809. std::intmax_t target_precision = std::numeric_limits<number<T> >::is_specialized ?
  810. current_precision - 1 - (std::numeric_limits<number<T> >::digits * 2) / 3
  811. : current_precision - 1 - (boost::multiprecision::detail::digits2<number<T> >::value() * 2) / 3;
  812. T s, c, t;
  813. while (current_precision > target_precision)
  814. {
  815. eval_sin(s, result);
  816. eval_cos(c, result);
  817. eval_multiply(t, xx, c);
  818. eval_subtract(t, s);
  819. eval_multiply(s, t, c);
  820. eval_add(result, s);
  821. current_precision = eval_ilogb(s);
  822. if (current_precision <= (std::numeric_limits<typename T::exponent_type>::min)() + 1)
  823. break;
  824. }
  825. if (b_neg)
  826. result.negate();
  827. }
  828. template <class T>
  829. void eval_atan2(T& result, const T& y, const T& x)
  830. {
  831. static_assert(number_category<T>::value == number_kind_floating_point, "The atan2 function is only valid for floating point types.");
  832. if (&result == &y)
  833. {
  834. T temp(y);
  835. eval_atan2(result, temp, x);
  836. return;
  837. }
  838. else if (&result == &x)
  839. {
  840. T temp(x);
  841. eval_atan2(result, y, temp);
  842. return;
  843. }
  844. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  845. switch (eval_fpclassify(y))
  846. {
  847. case FP_NAN:
  848. result = y;
  849. errno = EDOM;
  850. return;
  851. case FP_ZERO:
  852. {
  853. if (eval_signbit(x))
  854. {
  855. result = get_constant_pi<T>();
  856. if (eval_signbit(y))
  857. result.negate();
  858. }
  859. else
  860. {
  861. result = y; // Note we allow atan2(0,0) to be +-zero, even though it's mathematically undefined
  862. }
  863. return;
  864. }
  865. case FP_INFINITE:
  866. {
  867. if (eval_fpclassify(x) == FP_INFINITE)
  868. {
  869. if (eval_signbit(x))
  870. {
  871. // 3Pi/4
  872. eval_ldexp(result, get_constant_pi<T>(), -2);
  873. eval_subtract(result, get_constant_pi<T>());
  874. if (eval_get_sign(y) >= 0)
  875. result.negate();
  876. }
  877. else
  878. {
  879. // Pi/4
  880. eval_ldexp(result, get_constant_pi<T>(), -2);
  881. if (eval_get_sign(y) < 0)
  882. result.negate();
  883. }
  884. }
  885. else
  886. {
  887. eval_ldexp(result, get_constant_pi<T>(), -1);
  888. if (eval_get_sign(y) < 0)
  889. result.negate();
  890. }
  891. return;
  892. }
  893. default:
  894. break;
  895. }
  896. switch (eval_fpclassify(x))
  897. {
  898. case FP_NAN:
  899. result = x;
  900. errno = EDOM;
  901. return;
  902. case FP_ZERO:
  903. {
  904. eval_ldexp(result, get_constant_pi<T>(), -1);
  905. if (eval_get_sign(y) < 0)
  906. result.negate();
  907. return;
  908. }
  909. case FP_INFINITE:
  910. if (eval_get_sign(x) > 0)
  911. result = ui_type(0);
  912. else
  913. result = get_constant_pi<T>();
  914. if (eval_get_sign(y) < 0)
  915. result.negate();
  916. return;
  917. default:
  918. break;
  919. }
  920. T xx;
  921. eval_divide(xx, y, x);
  922. if (eval_get_sign(xx) < 0)
  923. xx.negate();
  924. eval_atan(result, xx);
  925. // Determine quadrant (sign) based on signs of x, y
  926. const bool y_neg = eval_get_sign(y) < 0;
  927. const bool x_neg = eval_get_sign(x) < 0;
  928. if (y_neg != x_neg)
  929. result.negate();
  930. if (x_neg)
  931. {
  932. if (y_neg)
  933. eval_subtract(result, get_constant_pi<T>());
  934. else
  935. eval_add(result, get_constant_pi<T>());
  936. }
  937. }
  938. template <class T, class A>
  939. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_atan2(T& result, const T& x, const A& a)
  940. {
  941. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  942. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  943. cast_type c;
  944. c = a;
  945. eval_atan2(result, x, c);
  946. }
  947. template <class T, class A>
  948. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_atan2(T& result, const A& x, const T& a)
  949. {
  950. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  951. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  952. cast_type c;
  953. c = x;
  954. eval_atan2(result, c, a);
  955. }
  956. #ifdef BOOST_MSVC
  957. #pragma warning(pop)
  958. #endif