trig.hpp 31 KB

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