trig.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // Copyright Christopher Kormanyos 2002 - 2011.
  2. // Copyright 2011 John Maddock. Distributed under the Boost
  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. #endif
  16. template <class T>
  17. void hyp0F1(T& result, const T& b, const T& x)
  18. {
  19. typedef typename boost::multiprecision::detail::canonical<boost::int32_t, T>::type si_type;
  20. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  21. // Compute the series representation of Hypergeometric0F1 taken from
  22. // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F1/06/01/01/
  23. // There are no checks on input range or parameter boundaries.
  24. T x_pow_n_div_n_fact(x);
  25. T pochham_b (b);
  26. T bp (b);
  27. eval_divide(result, x_pow_n_div_n_fact, pochham_b);
  28. eval_add(result, ui_type(1));
  29. si_type n;
  30. T tol;
  31. tol = ui_type(1);
  32. eval_ldexp(tol, tol, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value);
  33. eval_multiply(tol, result);
  34. if(eval_get_sign(tol) < 0)
  35. tol.negate();
  36. T term;
  37. static const int series_limit =
  38. boost::multiprecision::detail::digits2<number<T, et_on> >::value < 100
  39. ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value;
  40. // Series expansion of hyperg_0f1(; b; x).
  41. for(n = 2; n < series_limit; ++n)
  42. {
  43. eval_multiply(x_pow_n_div_n_fact, x);
  44. eval_divide(x_pow_n_div_n_fact, n);
  45. eval_increment(bp);
  46. eval_multiply(pochham_b, bp);
  47. eval_divide(term, x_pow_n_div_n_fact, pochham_b);
  48. eval_add(result, term);
  49. bool neg_term = eval_get_sign(term) < 0;
  50. if(neg_term)
  51. term.negate();
  52. if(term.compare(tol) <= 0)
  53. break;
  54. }
  55. if(n >= series_limit)
  56. BOOST_THROW_EXCEPTION(std::runtime_error("H0F1 Failed to Converge"));
  57. }
  58. template <class T>
  59. void eval_sin(T& result, const T& x)
  60. {
  61. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The sin function is only valid for floating point types.");
  62. if(&result == &x)
  63. {
  64. T temp;
  65. eval_sin(temp, x);
  66. result = temp;
  67. return;
  68. }
  69. typedef typename boost::multiprecision::detail::canonical<boost::int32_t, T>::type si_type;
  70. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  71. typedef typename mpl::front<typename T::float_types>::type fp_type;
  72. switch(eval_fpclassify(x))
  73. {
  74. case FP_INFINITE:
  75. case FP_NAN:
  76. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  77. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  78. else
  79. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  80. return;
  81. case FP_ZERO:
  82. result = ui_type(0);
  83. return;
  84. default: ;
  85. }
  86. // Local copy of the argument
  87. T xx = x;
  88. // Analyze and prepare the phase of the argument.
  89. // Make a local, positive copy of the argument, xx.
  90. // The argument xx will be reduced to 0 <= xx <= pi/2.
  91. bool b_negate_sin = false;
  92. if(eval_get_sign(x) < 0)
  93. {
  94. xx.negate();
  95. b_negate_sin = !b_negate_sin;
  96. }
  97. T n_pi, t;
  98. // Remove even multiples of pi.
  99. if(xx.compare(get_constant_pi<T>()) > 0)
  100. {
  101. eval_divide(n_pi, xx, get_constant_pi<T>());
  102. eval_trunc(n_pi, n_pi);
  103. t = ui_type(2);
  104. eval_fmod(t, n_pi, t);
  105. const bool b_n_pi_is_even = eval_get_sign(t) == 0;
  106. eval_multiply(n_pi, get_constant_pi<T>());
  107. eval_subtract(xx, n_pi);
  108. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  109. BOOST_MATH_INSTRUMENT_CODE(n_pi.str(0, std::ios_base::scientific));
  110. // Adjust signs if the multiple of pi is not even.
  111. if(!b_n_pi_is_even)
  112. {
  113. b_negate_sin = !b_negate_sin;
  114. }
  115. }
  116. // Reduce the argument to 0 <= xx <= pi/2.
  117. eval_ldexp(t, get_constant_pi<T>(), -1);
  118. if(xx.compare(t) > 0)
  119. {
  120. eval_subtract(xx, get_constant_pi<T>(), xx);
  121. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  122. }
  123. eval_subtract(t, xx);
  124. const bool b_zero = eval_get_sign(xx) == 0;
  125. const bool b_pi_half = eval_get_sign(t) == 0;
  126. // Check if the reduced argument is very close to 0 or pi/2.
  127. const bool b_near_zero = xx.compare(fp_type(1e-1)) < 0;
  128. const bool b_near_pi_half = t.compare(fp_type(1e-1)) < 0;;
  129. if(b_zero)
  130. {
  131. result = ui_type(0);
  132. }
  133. else if(b_pi_half)
  134. {
  135. result = ui_type(1);
  136. }
  137. else if(b_near_zero)
  138. {
  139. eval_multiply(t, xx, xx);
  140. eval_divide(t, si_type(-4));
  141. T t2;
  142. t2 = fp_type(1.5);
  143. hyp0F1(result, t2, t);
  144. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  145. eval_multiply(result, xx);
  146. }
  147. else if(b_near_pi_half)
  148. {
  149. eval_multiply(t, t);
  150. eval_divide(t, si_type(-4));
  151. T t2;
  152. t2 = fp_type(0.5);
  153. hyp0F1(result, t2, t);
  154. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  155. }
  156. else
  157. {
  158. // Scale to a small argument for an efficient Taylor series,
  159. // implemented as a hypergeometric function. Use a standard
  160. // divide by three identity a certain number of times.
  161. // Here we use division by 3^9 --> (19683 = 3^9).
  162. static const si_type n_scale = 9;
  163. static const si_type n_three_pow_scale = static_cast<si_type>(19683L);
  164. eval_divide(xx, n_three_pow_scale);
  165. // Now with small arguments, we are ready for a series expansion.
  166. eval_multiply(t, xx, xx);
  167. eval_divide(t, si_type(-4));
  168. T t2;
  169. t2 = fp_type(1.5);
  170. hyp0F1(result, t2, t);
  171. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  172. eval_multiply(result, xx);
  173. // Convert back using multiple angle identity.
  174. for(boost::int32_t k = static_cast<boost::int32_t>(0); k < n_scale; k++)
  175. {
  176. // Rescale the cosine value using the multiple angle identity.
  177. eval_multiply(t2, result, ui_type(3));
  178. eval_multiply(t, result, result);
  179. eval_multiply(t, result);
  180. eval_multiply(t, ui_type(4));
  181. eval_subtract(result, t2, t);
  182. }
  183. }
  184. if(b_negate_sin)
  185. result.negate();
  186. }
  187. template <class T>
  188. void eval_cos(T& result, const T& x)
  189. {
  190. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The cos function is only valid for floating point types.");
  191. if(&result == &x)
  192. {
  193. T temp;
  194. eval_cos(temp, x);
  195. result = temp;
  196. return;
  197. }
  198. typedef typename boost::multiprecision::detail::canonical<boost::int32_t, T>::type si_type;
  199. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  200. typedef typename mpl::front<typename T::float_types>::type fp_type;
  201. switch(eval_fpclassify(x))
  202. {
  203. case FP_INFINITE:
  204. case FP_NAN:
  205. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  206. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  207. else
  208. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  209. return;
  210. case FP_ZERO:
  211. result = ui_type(1);
  212. return;
  213. default: ;
  214. }
  215. // Local copy of the argument
  216. T xx = x;
  217. // Analyze and prepare the phase of the argument.
  218. // Make a local, positive copy of the argument, xx.
  219. // The argument xx will be reduced to 0 <= xx <= pi/2.
  220. bool b_negate_cos = false;
  221. if(eval_get_sign(x) < 0)
  222. {
  223. xx.negate();
  224. }
  225. T n_pi, t;
  226. // Remove even multiples of pi.
  227. if(xx.compare(get_constant_pi<T>()) > 0)
  228. {
  229. eval_divide(t, xx, get_constant_pi<T>());
  230. eval_trunc(n_pi, t);
  231. BOOST_MATH_INSTRUMENT_CODE(n_pi.str(0, std::ios_base::scientific));
  232. eval_multiply(t, n_pi, get_constant_pi<T>());
  233. BOOST_MATH_INSTRUMENT_CODE(t.str(0, std::ios_base::scientific));
  234. eval_subtract(xx, t);
  235. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  236. // Adjust signs if the multiple of pi is not even.
  237. t = ui_type(2);
  238. eval_fmod(t, n_pi, t);
  239. const bool b_n_pi_is_even = eval_get_sign(t) == 0;
  240. if(!b_n_pi_is_even)
  241. {
  242. b_negate_cos = !b_negate_cos;
  243. }
  244. }
  245. // Reduce the argument to 0 <= xx <= pi/2.
  246. eval_ldexp(t, get_constant_pi<T>(), -1);
  247. int com = xx.compare(t);
  248. if(com > 0)
  249. {
  250. eval_subtract(xx, get_constant_pi<T>(), xx);
  251. b_negate_cos = !b_negate_cos;
  252. BOOST_MATH_INSTRUMENT_CODE(xx.str(0, std::ios_base::scientific));
  253. }
  254. const bool b_zero = eval_get_sign(xx) == 0;
  255. const bool b_pi_half = com == 0;
  256. // Check if the reduced argument is very close to 0.
  257. const bool b_near_zero = xx.compare(fp_type(1e-1)) < 0;
  258. if(b_zero)
  259. {
  260. result = si_type(1);
  261. }
  262. else if(b_pi_half)
  263. {
  264. result = si_type(0);
  265. }
  266. else if(b_near_zero)
  267. {
  268. eval_multiply(t, xx, xx);
  269. eval_divide(t, si_type(-4));
  270. n_pi = fp_type(0.5f);
  271. hyp0F1(result, n_pi, t);
  272. BOOST_MATH_INSTRUMENT_CODE(result.str(0, std::ios_base::scientific));
  273. }
  274. else
  275. {
  276. eval_subtract(t, xx);
  277. eval_sin(result, t);
  278. }
  279. if(b_negate_cos)
  280. result.negate();
  281. }
  282. template <class T>
  283. void eval_tan(T& result, const T& x)
  284. {
  285. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The tan function is only valid for floating point types.");
  286. if(&result == &x)
  287. {
  288. T temp;
  289. eval_tan(temp, x);
  290. result = temp;
  291. return;
  292. }
  293. T t;
  294. eval_sin(result, x);
  295. eval_cos(t, x);
  296. eval_divide(result, t);
  297. }
  298. template <class T>
  299. void hyp2F1(T& result, const T& a, const T& b, const T& c, const T& x)
  300. {
  301. // Compute the series representation of hyperg_2f1 taken from
  302. // Abramowitz and Stegun 15.1.1.
  303. // There are no checks on input range or parameter boundaries.
  304. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  305. T x_pow_n_div_n_fact(x);
  306. T pochham_a (a);
  307. T pochham_b (b);
  308. T pochham_c (c);
  309. T ap (a);
  310. T bp (b);
  311. T cp (c);
  312. eval_multiply(result, pochham_a, pochham_b);
  313. eval_divide(result, pochham_c);
  314. eval_multiply(result, x_pow_n_div_n_fact);
  315. eval_add(result, ui_type(1));
  316. T lim;
  317. eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value);
  318. if(eval_get_sign(lim) < 0)
  319. lim.negate();
  320. ui_type n;
  321. T term;
  322. static const unsigned series_limit =
  323. boost::multiprecision::detail::digits2<number<T, et_on> >::value < 100
  324. ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value;
  325. // Series expansion of hyperg_2f1(a, b; c; x).
  326. for(n = 2; n < series_limit; ++n)
  327. {
  328. eval_multiply(x_pow_n_div_n_fact, x);
  329. eval_divide(x_pow_n_div_n_fact, n);
  330. eval_increment(ap);
  331. eval_multiply(pochham_a, ap);
  332. eval_increment(bp);
  333. eval_multiply(pochham_b, bp);
  334. eval_increment(cp);
  335. eval_multiply(pochham_c, cp);
  336. eval_multiply(term, pochham_a, pochham_b);
  337. eval_divide(term, pochham_c);
  338. eval_multiply(term, x_pow_n_div_n_fact);
  339. eval_add(result, term);
  340. if(eval_get_sign(term) < 0)
  341. term.negate();
  342. if(lim.compare(term) >= 0)
  343. break;
  344. }
  345. if(n > series_limit)
  346. BOOST_THROW_EXCEPTION(std::runtime_error("H2F1 failed to converge."));
  347. }
  348. template <class T>
  349. void eval_asin(T& result, const T& x)
  350. {
  351. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The asin function is only valid for floating point types.");
  352. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  353. typedef typename mpl::front<typename T::float_types>::type fp_type;
  354. if(&result == &x)
  355. {
  356. T t(x);
  357. eval_asin(result, t);
  358. return;
  359. }
  360. switch(eval_fpclassify(x))
  361. {
  362. case FP_NAN:
  363. case FP_INFINITE:
  364. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  365. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  366. else
  367. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  368. return;
  369. case FP_ZERO:
  370. result = ui_type(0);
  371. return;
  372. default: ;
  373. }
  374. const bool b_neg = eval_get_sign(x) < 0;
  375. T xx(x);
  376. if(b_neg)
  377. xx.negate();
  378. int c = xx.compare(ui_type(1));
  379. if(c > 0)
  380. {
  381. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  382. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  383. else
  384. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  385. return;
  386. }
  387. else if(c == 0)
  388. {
  389. result = get_constant_pi<T>();
  390. eval_ldexp(result, result, -1);
  391. if(b_neg)
  392. result.negate();
  393. return;
  394. }
  395. if(xx.compare(fp_type(1e-4)) < 0)
  396. {
  397. // http://functions.wolfram.com/ElementaryFunctions/ArcSin/26/01/01/
  398. eval_multiply(xx, xx);
  399. T t1, t2;
  400. t1 = fp_type(0.5f);
  401. t2 = fp_type(1.5f);
  402. hyp2F1(result, t1, t1, t2, xx);
  403. eval_multiply(result, x);
  404. return;
  405. }
  406. else if(xx.compare(fp_type(1 - 1e-4f)) > 0)
  407. {
  408. T dx1;
  409. T t1, t2;
  410. eval_subtract(dx1, ui_type(1), xx);
  411. t1 = fp_type(0.5f);
  412. t2 = fp_type(1.5f);
  413. eval_ldexp(dx1, dx1, -1);
  414. hyp2F1(result, t1, t1, t2, dx1);
  415. eval_ldexp(dx1, dx1, 2);
  416. eval_sqrt(t1, dx1);
  417. eval_multiply(result, t1);
  418. eval_ldexp(t1, get_constant_pi<T>(), -1);
  419. result.negate();
  420. eval_add(result, t1);
  421. if(b_neg)
  422. result.negate();
  423. return;
  424. }
  425. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  426. typedef typename boost::multiprecision::detail::canonical<long double, T>::type guess_type;
  427. #else
  428. typedef fp_type guess_type;
  429. #endif
  430. // Get initial estimate using standard math function asin.
  431. guess_type dd;
  432. eval_convert_to(&dd, xx);
  433. result = (guess_type)(std::asin(dd));
  434. unsigned current_digits = std::numeric_limits<guess_type>::digits - 5;
  435. unsigned target_precision = boost::multiprecision::detail::digits2<number<T, et_on> >::value;
  436. // Newton-Raphson iteration
  437. while(current_digits < target_precision)
  438. {
  439. T sine, cosine;
  440. eval_sin(sine, result);
  441. eval_cos(cosine, result);
  442. eval_subtract(sine, xx);
  443. eval_divide(sine, cosine);
  444. eval_subtract(result, sine);
  445. current_digits *= 2;
  446. /*
  447. T lim;
  448. eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value);
  449. if(eval_get_sign(s) < 0)
  450. s.negate();
  451. if(eval_get_sign(lim) < 0)
  452. lim.negate();
  453. if(lim.compare(s) >= 0)
  454. break;
  455. */
  456. }
  457. if(b_neg)
  458. result.negate();
  459. }
  460. template <class T>
  461. inline void eval_acos(T& result, const T& x)
  462. {
  463. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The acos function is only valid for floating point types.");
  464. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  465. switch(eval_fpclassify(x))
  466. {
  467. case FP_NAN:
  468. case FP_INFINITE:
  469. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  470. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  471. else
  472. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  473. return;
  474. case FP_ZERO:
  475. result = get_constant_pi<T>();
  476. eval_ldexp(result, result, -1); // divide by two.
  477. return;
  478. }
  479. eval_abs(result, x);
  480. int c = result.compare(ui_type(1));
  481. if(c > 0)
  482. {
  483. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  484. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  485. else
  486. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  487. return;
  488. }
  489. else if(c == 0)
  490. {
  491. if(eval_get_sign(x) < 0)
  492. result = get_constant_pi<T>();
  493. else
  494. result = ui_type(0);
  495. return;
  496. }
  497. eval_asin(result, x);
  498. T t;
  499. eval_ldexp(t, get_constant_pi<T>(), -1);
  500. eval_subtract(result, t);
  501. result.negate();
  502. }
  503. template <class T>
  504. void eval_atan(T& result, const T& x)
  505. {
  506. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The atan function is only valid for floating point types.");
  507. typedef typename boost::multiprecision::detail::canonical<boost::int32_t, T>::type si_type;
  508. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  509. typedef typename mpl::front<typename T::float_types>::type fp_type;
  510. switch(eval_fpclassify(x))
  511. {
  512. case FP_NAN:
  513. result = x;
  514. return;
  515. case FP_ZERO:
  516. result = ui_type(0);
  517. return;
  518. case FP_INFINITE:
  519. if(eval_get_sign(x) < 0)
  520. {
  521. eval_ldexp(result, get_constant_pi<T>(), -1);
  522. result.negate();
  523. }
  524. else
  525. eval_ldexp(result, get_constant_pi<T>(), -1);
  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. if(xx.compare(fp_type(0.1)) < 0)
  534. {
  535. T t1, t2, t3;
  536. t1 = ui_type(1);
  537. t2 = fp_type(0.5f);
  538. t3 = fp_type(1.5f);
  539. eval_multiply(xx, xx);
  540. xx.negate();
  541. hyp2F1(result, t1, t2, t3, xx);
  542. eval_multiply(result, x);
  543. return;
  544. }
  545. if(xx.compare(fp_type(10)) > 0)
  546. {
  547. T t1, t2, t3;
  548. t1 = fp_type(0.5f);
  549. t2 = ui_type(1u);
  550. t3 = fp_type(1.5f);
  551. eval_multiply(xx, xx);
  552. eval_divide(xx, si_type(-1), xx);
  553. hyp2F1(result, t1, t2, t3, xx);
  554. eval_divide(result, x);
  555. if(!b_neg)
  556. result.negate();
  557. eval_ldexp(t1, get_constant_pi<T>(), -1);
  558. eval_add(result, t1);
  559. if(b_neg)
  560. result.negate();
  561. return;
  562. }
  563. // Get initial estimate using standard math function atan.
  564. fp_type d;
  565. eval_convert_to(&d, xx);
  566. result = fp_type(std::atan(d));
  567. // Newton-Raphson iteration
  568. static const boost::int32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  569. T s, c, t;
  570. for(boost::int32_t digits = double_digits10_minus_a_few; digits <= std::numeric_limits<number<T, et_on> >::digits10; digits *= 2)
  571. {
  572. eval_sin(s, result);
  573. eval_cos(c, result);
  574. eval_multiply(t, xx, c);
  575. eval_subtract(t, s);
  576. eval_multiply(s, t, c);
  577. eval_add(result, s);
  578. }
  579. if(b_neg)
  580. result.negate();
  581. }
  582. template <class T>
  583. void eval_atan2(T& result, const T& y, const T& x)
  584. {
  585. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The atan2 function is only valid for floating point types.");
  586. if(&result == &y)
  587. {
  588. T temp(y);
  589. eval_atan2(result, temp, x);
  590. return;
  591. }
  592. else if(&result == &x)
  593. {
  594. T temp(x);
  595. eval_atan2(result, y, temp);
  596. return;
  597. }
  598. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  599. switch(eval_fpclassify(y))
  600. {
  601. case FP_NAN:
  602. result = y;
  603. return;
  604. case FP_ZERO:
  605. {
  606. int c = eval_get_sign(x);
  607. if(c < 0)
  608. result = get_constant_pi<T>();
  609. else if(c >= 0)
  610. result = ui_type(0); // Note we allow atan2(0,0) to be zero, even though it's mathematically undefined
  611. return;
  612. }
  613. case FP_INFINITE:
  614. {
  615. if(eval_fpclassify(x) == FP_INFINITE)
  616. {
  617. if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  618. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  619. else
  620. BOOST_THROW_EXCEPTION(std::domain_error("Result is undefined or complex and there is no NaN for this number type."));
  621. }
  622. else
  623. {
  624. eval_ldexp(result, get_constant_pi<T>(), -1);
  625. if(eval_get_sign(y) < 0)
  626. result.negate();
  627. }
  628. return;
  629. }
  630. }
  631. switch(eval_fpclassify(x))
  632. {
  633. case FP_NAN:
  634. result = x;
  635. return;
  636. case FP_ZERO:
  637. {
  638. eval_ldexp(result, get_constant_pi<T>(), -1);
  639. if(eval_get_sign(y) < 0)
  640. result.negate();
  641. return;
  642. }
  643. case FP_INFINITE:
  644. if(eval_get_sign(x) > 0)
  645. result = ui_type(0);
  646. else
  647. result = get_constant_pi<T>();
  648. if(eval_get_sign(y) < 0)
  649. result.negate();
  650. return;
  651. }
  652. T xx;
  653. eval_divide(xx, y, x);
  654. if(eval_get_sign(xx) < 0)
  655. xx.negate();
  656. eval_atan(result, xx);
  657. // Determine quadrant (sign) based on signs of x, y
  658. const bool y_neg = eval_get_sign(y) < 0;
  659. const bool x_neg = eval_get_sign(x) < 0;
  660. if(y_neg != x_neg)
  661. result.negate();
  662. if(x_neg)
  663. {
  664. if(y_neg)
  665. eval_subtract(result, get_constant_pi<T>());
  666. else
  667. eval_add(result, get_constant_pi<T>());
  668. }
  669. }
  670. template<class T, class A>
  671. inline typename enable_if<is_arithmetic<A>, void>::type eval_atan2(T& result, const T& x, const A& a)
  672. {
  673. typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
  674. typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
  675. cast_type c;
  676. c = a;
  677. eval_atan2(result, x, c);
  678. }
  679. template<class T, class A>
  680. inline typename enable_if<is_arithmetic<A>, void>::type eval_atan2(T& result, const A& x, const T& a)
  681. {
  682. typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
  683. typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
  684. cast_type c;
  685. c = x;
  686. eval_atan2(result, c, a);
  687. }
  688. #ifdef BOOST_MSVC
  689. #pragma warning(pop)
  690. #endif