trig.hpp 24 KB

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