trig.hpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. }
  644. T xx;
  645. eval_abs(xx, x);
  646. int c = xx.compare(ui_type(1));
  647. if (c > 0)
  648. {
  649. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  650. {
  651. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  652. errno = EDOM;
  653. }
  654. else
  655. BOOST_MP_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  656. return;
  657. }
  658. else if (c == 0)
  659. {
  660. if (eval_get_sign(x) < 0)
  661. result = get_constant_pi<T>();
  662. else
  663. result = ui_type(0);
  664. return;
  665. }
  666. using fp_type = typename std::tuple_element<0, typename T::float_types>::type;
  667. if (xx.compare(fp_type(1e-3)) < 0)
  668. {
  669. // https://functions.wolfram.com/ElementaryFunctions/ArcCos/26/01/01/
  670. eval_multiply(xx, xx);
  671. T t1, t2;
  672. t1 = fp_type(0.5f);
  673. t2 = fp_type(1.5f);
  674. hyp2F1(result, t1, t1, t2, xx);
  675. eval_multiply(result, x);
  676. eval_ldexp(t1, get_constant_pi<T>(), -1);
  677. result.negate();
  678. eval_add(result, t1);
  679. return;
  680. }
  681. if (eval_get_sign(x) < 0)
  682. {
  683. eval_acos(result, xx);
  684. result.negate();
  685. eval_add(result, get_constant_pi<T>());
  686. return;
  687. }
  688. else if (xx.compare(fp_type(0.85)) > 0)
  689. {
  690. // https://functions.wolfram.com/ElementaryFunctions/ArcCos/26/01/01/
  691. // This branch is simlilar in complexity to Newton iterations down to
  692. // the above limit. It is *much* more accurate.
  693. T dx1;
  694. T t1, t2;
  695. eval_subtract(dx1, ui_type(1), xx);
  696. t1 = fp_type(0.5f);
  697. t2 = fp_type(1.5f);
  698. eval_ldexp(dx1, dx1, -1);
  699. hyp2F1(result, t1, t1, t2, dx1);
  700. eval_ldexp(dx1, dx1, 2);
  701. eval_sqrt(t1, dx1);
  702. eval_multiply(result, t1);
  703. return;
  704. }
  705. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  706. using guess_type = typename boost::multiprecision::detail::canonical<long double, T>::type;
  707. #else
  708. using guess_type = fp_type;
  709. #endif
  710. // Get initial estimate using standard math function asin.
  711. guess_type dd;
  712. eval_convert_to(&dd, xx);
  713. result = (guess_type)(std::acos(dd));
  714. // Newton-Raphson iteration, we should double our precision with each iteration,
  715. // in practice this seems to not quite work in all cases... so terminate when we
  716. // have at least 2/3 of the digits correct on the assumption that the correction
  717. // we've just added will finish the job...
  718. std::intmax_t current_precision = eval_ilogb(result);
  719. std::intmax_t target_precision = std::numeric_limits<number<T> >::is_specialized ?
  720. current_precision - 1 - (std::numeric_limits<number<T> >::digits * 2) / 3
  721. : current_precision - 1 - (boost::multiprecision::detail::digits2<number<T> >::value() * 2) / 3;
  722. // Newton-Raphson iteration
  723. while (current_precision > target_precision)
  724. {
  725. T sine, cosine;
  726. eval_sin(sine, result);
  727. eval_cos(cosine, result);
  728. eval_subtract(cosine, xx);
  729. cosine.negate();
  730. eval_divide(cosine, sine);
  731. eval_subtract(result, cosine);
  732. current_precision = eval_ilogb(cosine);
  733. if (current_precision <= (std::numeric_limits<typename T::exponent_type>::min)() + 1)
  734. break;
  735. }
  736. }
  737. template <class T>
  738. void eval_atan(T& result, const T& x)
  739. {
  740. static_assert(number_category<T>::value == number_kind_floating_point, "The atan function is only valid for floating point types.");
  741. using si_type = typename boost::multiprecision::detail::canonical<std::int32_t, T>::type ;
  742. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  743. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  744. switch (eval_fpclassify(x))
  745. {
  746. case FP_NAN:
  747. result = x;
  748. errno = EDOM;
  749. return;
  750. case FP_ZERO:
  751. result = x;
  752. return;
  753. case FP_INFINITE:
  754. if (eval_get_sign(x) < 0)
  755. {
  756. eval_ldexp(result, get_constant_pi<T>(), -1);
  757. result.negate();
  758. }
  759. else
  760. eval_ldexp(result, get_constant_pi<T>(), -1);
  761. return;
  762. default:;
  763. }
  764. const bool b_neg = eval_get_sign(x) < 0;
  765. T xx(x);
  766. if (b_neg)
  767. xx.negate();
  768. if (xx.compare(fp_type(0.1)) < 0)
  769. {
  770. T t1, t2, t3;
  771. t1 = ui_type(1);
  772. t2 = fp_type(0.5f);
  773. t3 = fp_type(1.5f);
  774. eval_multiply(xx, xx);
  775. xx.negate();
  776. hyp2F1(result, t1, t2, t3, xx);
  777. eval_multiply(result, x);
  778. return;
  779. }
  780. if (xx.compare(fp_type(10)) > 0)
  781. {
  782. T t1, t2, t3;
  783. t1 = fp_type(0.5f);
  784. t2 = ui_type(1u);
  785. t3 = fp_type(1.5f);
  786. eval_multiply(xx, xx);
  787. eval_divide(xx, si_type(-1), xx);
  788. hyp2F1(result, t1, t2, t3, xx);
  789. eval_divide(result, x);
  790. if (!b_neg)
  791. result.negate();
  792. eval_ldexp(t1, get_constant_pi<T>(), -1);
  793. eval_add(result, t1);
  794. if (b_neg)
  795. result.negate();
  796. return;
  797. }
  798. // Get initial estimate using standard math function atan.
  799. fp_type d;
  800. eval_convert_to(&d, xx);
  801. result = fp_type(std::atan(d));
  802. // Newton-Raphson iteration, we should double our precision with each iteration,
  803. // in practice this seems to not quite work in all cases... so terminate when we
  804. // have at least 2/3 of the digits correct on the assumption that the correction
  805. // we've just added will finish the job...
  806. std::intmax_t current_precision = eval_ilogb(result);
  807. std::intmax_t target_precision = std::numeric_limits<number<T> >::is_specialized ?
  808. current_precision - 1 - (std::numeric_limits<number<T> >::digits * 2) / 3
  809. : current_precision - 1 - (boost::multiprecision::detail::digits2<number<T> >::value() * 2) / 3;
  810. T s, c, t;
  811. while (current_precision > target_precision)
  812. {
  813. eval_sin(s, result);
  814. eval_cos(c, result);
  815. eval_multiply(t, xx, c);
  816. eval_subtract(t, s);
  817. eval_multiply(s, t, c);
  818. eval_add(result, s);
  819. current_precision = eval_ilogb(s);
  820. if (current_precision <= (std::numeric_limits<typename T::exponent_type>::min)() + 1)
  821. break;
  822. }
  823. if (b_neg)
  824. result.negate();
  825. }
  826. template <class T>
  827. void eval_atan2(T& result, const T& y, const T& x)
  828. {
  829. static_assert(number_category<T>::value == number_kind_floating_point, "The atan2 function is only valid for floating point types.");
  830. if (&result == &y)
  831. {
  832. T temp(y);
  833. eval_atan2(result, temp, x);
  834. return;
  835. }
  836. else if (&result == &x)
  837. {
  838. T temp(x);
  839. eval_atan2(result, y, temp);
  840. return;
  841. }
  842. using ui_type = typename boost::multiprecision::detail::canonical<std::uint32_t, T>::type;
  843. switch (eval_fpclassify(y))
  844. {
  845. case FP_NAN:
  846. result = y;
  847. errno = EDOM;
  848. return;
  849. case FP_ZERO:
  850. {
  851. if (eval_signbit(x))
  852. {
  853. result = get_constant_pi<T>();
  854. if (eval_signbit(y))
  855. result.negate();
  856. }
  857. else
  858. {
  859. result = y; // Note we allow atan2(0,0) to be +-zero, even though it's mathematically undefined
  860. }
  861. return;
  862. }
  863. case FP_INFINITE:
  864. {
  865. if (eval_fpclassify(x) == FP_INFINITE)
  866. {
  867. if (eval_signbit(x))
  868. {
  869. // 3Pi/4
  870. eval_ldexp(result, get_constant_pi<T>(), -2);
  871. eval_subtract(result, get_constant_pi<T>());
  872. if (eval_get_sign(y) >= 0)
  873. result.negate();
  874. }
  875. else
  876. {
  877. // Pi/4
  878. eval_ldexp(result, get_constant_pi<T>(), -2);
  879. if (eval_get_sign(y) < 0)
  880. result.negate();
  881. }
  882. }
  883. else
  884. {
  885. eval_ldexp(result, get_constant_pi<T>(), -1);
  886. if (eval_get_sign(y) < 0)
  887. result.negate();
  888. }
  889. return;
  890. }
  891. }
  892. switch (eval_fpclassify(x))
  893. {
  894. case FP_NAN:
  895. result = x;
  896. errno = EDOM;
  897. return;
  898. case FP_ZERO:
  899. {
  900. eval_ldexp(result, get_constant_pi<T>(), -1);
  901. if (eval_get_sign(y) < 0)
  902. result.negate();
  903. return;
  904. }
  905. case FP_INFINITE:
  906. if (eval_get_sign(x) > 0)
  907. result = ui_type(0);
  908. else
  909. result = get_constant_pi<T>();
  910. if (eval_get_sign(y) < 0)
  911. result.negate();
  912. return;
  913. }
  914. T xx;
  915. eval_divide(xx, y, x);
  916. if (eval_get_sign(xx) < 0)
  917. xx.negate();
  918. eval_atan(result, xx);
  919. // Determine quadrant (sign) based on signs of x, y
  920. const bool y_neg = eval_get_sign(y) < 0;
  921. const bool x_neg = eval_get_sign(x) < 0;
  922. if (y_neg != x_neg)
  923. result.negate();
  924. if (x_neg)
  925. {
  926. if (y_neg)
  927. eval_subtract(result, get_constant_pi<T>());
  928. else
  929. eval_add(result, get_constant_pi<T>());
  930. }
  931. }
  932. template <class T, class A>
  933. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_atan2(T& result, const T& x, const A& a)
  934. {
  935. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  936. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  937. cast_type c;
  938. c = a;
  939. eval_atan2(result, x, c);
  940. }
  941. template <class T, class A>
  942. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_atan2(T& result, const A& x, const T& a)
  943. {
  944. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  945. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  946. cast_type c;
  947. c = x;
  948. eval_atan2(result, c, a);
  949. }
  950. #ifdef BOOST_MSVC
  951. #pragma warning(pop)
  952. #endif