transcendental.hpp 4.9 KB

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