transcendental.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2013 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_MP_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
  6. #define BOOST_MP_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
  7. #include <boost/multiprecision/detail/assert.hpp>
  8. namespace boost { namespace multiprecision { namespace backends {
  9. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  10. void eval_exp_taylor(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  11. {
  12. constexpr std::ptrdiff_t bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
  13. //
  14. // Taylor series for small argument, note returns exp(x) - 1:
  15. //
  16. res = limb_type(0);
  17. cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> num(arg), denom, t;
  18. denom = limb_type(1);
  19. eval_add(res, num);
  20. for (std::size_t k = 2;; ++k)
  21. {
  22. eval_multiply(denom, k);
  23. eval_multiply(num, arg);
  24. eval_divide(t, num, denom);
  25. eval_add(res, t);
  26. if (eval_is_zero(t) || (res.exponent() - bits > t.exponent()))
  27. break;
  28. }
  29. }
  30. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  31. void eval_exp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  32. {
  33. //
  34. // This is based on MPFR's method, let:
  35. //
  36. // n = floor(x / ln(2))
  37. //
  38. // Then:
  39. //
  40. // r = x - n ln(2) : 0 <= r < ln(2)
  41. //
  42. // We can reduce r further by dividing by 2^k, with k ~ sqrt(n),
  43. // so if:
  44. //
  45. // e0 = exp(r / 2^k) - 1
  46. //
  47. // With e0 evaluated by taylor series for small arguments, then:
  48. //
  49. // exp(x) = 2^n (1 + e0)^2^k
  50. //
  51. // Note that to preserve precision we actually square (1 + e0) k times, calculating
  52. // the result less one each time, i.e.
  53. //
  54. // (1 + e0)^2 - 1 = e0^2 + 2e0
  55. //
  56. // Then add the final 1 at the end, given that e0 is small, this effectively wipes
  57. // out the error in the last step.
  58. //
  59. using default_ops::eval_add;
  60. using default_ops::eval_convert_to;
  61. using default_ops::eval_increment;
  62. using default_ops::eval_multiply;
  63. using default_ops::eval_subtract;
  64. int type = eval_fpclassify(arg);
  65. bool isneg = eval_get_sign(arg) < 0;
  66. if (type == static_cast<int>(FP_NAN))
  67. {
  68. res = arg;
  69. errno = EDOM;
  70. return;
  71. }
  72. else if (type == static_cast<int>(FP_INFINITE))
  73. {
  74. res = arg;
  75. if (isneg)
  76. res = limb_type(0u);
  77. else
  78. res = arg;
  79. return;
  80. }
  81. else if (type == static_cast<int>(FP_ZERO))
  82. {
  83. res = limb_type(1);
  84. return;
  85. }
  86. cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> t, n;
  87. if (isneg)
  88. {
  89. t = arg;
  90. t.negate();
  91. eval_exp(res, t);
  92. t.swap(res);
  93. res = limb_type(1);
  94. eval_divide(res, t);
  95. return;
  96. }
  97. eval_divide(n, arg, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
  98. eval_floor(n, n);
  99. eval_multiply(t, n, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
  100. eval_subtract(t, arg);
  101. t.negate();
  102. if (t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) > 0)
  103. {
  104. // There are some rare cases where the multiply rounds down leaving a remainder > ln2
  105. // See https://github.com/boostorg/multiprecision/issues/120
  106. eval_increment(n);
  107. t = limb_type(0);
  108. }
  109. if (eval_get_sign(t) < 0)
  110. {
  111. // There are some very rare cases where arg/ln2 is an integer, and the subsequent multiply
  112. // rounds up, in that situation t ends up negative at this point which breaks our invariants below:
  113. t = limb_type(0);
  114. }
  115. Exponent k, nn;
  116. eval_convert_to(&nn, n);
  117. if (nn == (std::numeric_limits<Exponent>::max)())
  118. {
  119. // The result will necessarily oveflow:
  120. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  121. return;
  122. }
  123. BOOST_MP_ASSERT(t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) < 0);
  124. k = nn ? Exponent(1) << (msb(nn) / 2) : 0;
  125. k = (std::min)(k, (Exponent)(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count / 4));
  126. eval_ldexp(t, t, -k);
  127. eval_exp_taylor(res, t);
  128. //
  129. // Square 1 + res k times:
  130. //
  131. for (Exponent s = 0; s < k; ++s)
  132. {
  133. t.swap(res);
  134. eval_multiply(res, t, t);
  135. eval_ldexp(t, t, 1);
  136. eval_add(res, t);
  137. }
  138. eval_add(res, limb_type(1));
  139. eval_ldexp(res, res, nn);
  140. }
  141. }}} // namespace boost::multiprecision::backends
  142. #endif