pow.qbk 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. [section:ct_pow Compile Time Power of a Runtime Base]
  2. The `pow` function effectively computes the compile-time integral
  3. power of a run-time base.
  4. [h4 Synopsis]
  5. [@../../../../boost/math/special_functions/pow.hpp `#include <boost/math/special_functions/pow.hpp>`]
  6. namespace boost { namespace math {
  7. template <int N, typename T>
  8. ``__sf_result`` pow(T base);
  9. template <int N, typename T, class Policy>
  10. ``__sf_result`` pow(T base, const Policy& policy);
  11. }}
  12. [h4 Rationale and Usage]
  13. Computing the power of a number with an exponent that is known
  14. at compile time is a common need for programmers. In such cases,
  15. the usual method is to avoid the overhead implied by
  16. the `pow`, `powf` and `powl` C functions by hardcoding an expression
  17. such as:
  18. // Hand-written 8th power of a 'base' variable
  19. double result = base*base*base*base*base*base*base*base;
  20. However, this kind of expression is not really readable (knowing the
  21. value of the exponent involves counting the number of occurrences of /base/),
  22. error-prone (it's easy to forget an occurrence), syntactically bulky, and
  23. non-optimal in terms of performance.
  24. The `pow` function of Boost.Math helps writing this kind expression along
  25. with solving all the problems listed above:
  26. // 8th power of a 'base' variable using math::pow
  27. double result = pow<8>(base);
  28. The expression is now shorter, easier to read, safer, and even faster.
  29. Indeed, `pow` will compute the expression such that only log2(N)
  30. products are made for a power of N. For instance in the
  31. example above, the resulting expression will be the same as if we had
  32. written this, with only one computation of each identical subexpression:
  33. // Internal effect of pow<8>(base)
  34. double result = ((base*base)*(base*base))*((base*base)*(base*base));
  35. Only 3 different products were actually computed.
  36. [h4 Return Type]
  37. The return type of these functions is computed using the __arg_promotion_rules.
  38. For example:
  39. * If T is a `float`, the return type is a `float`.
  40. * If T is a `long double`, the return type is a `long double`.
  41. * Otherwise, the return type is a `double`.
  42. [h4 Policies]
  43. [optional_policy]
  44. [h4 Error Handling]
  45. Two cases of errors can occur when using `pow`:
  46. * In case of null base and negative exponent, an __overflow_error occurs since
  47. this operation is a division by 0 (it equals to 1/0).
  48. * In case of null base and null exponent, an __indeterminate_result_error
  49. occurs since the result of this operation is indeterminate.
  50. Those errors follow the
  51. [link math_toolkit.error_handling
  52. general policies of error handling in Boost.Math].
  53. The default overflow error policy is `throw_on_error`. A call like `pow<-2>(0)`
  54. will thus throw a `std::overflow_error` exception. As shown in the
  55. link given above, other error handling policies can be used:
  56. * `errno_on_error`: Sets `::errno` to `ERANGE` and returns `std::numeric_limits<T>::infinity()`.
  57. * `ignore_error`: Returns `std::numeric_limits<T>::infinity()`.
  58. * `user_error`: Returns the result of `boost::math::policies::user_overflow_error`:
  59. this function must be defined by the user.
  60. The default indeterminate result error policy is `ignore_error`, which for this
  61. function returns 1 since it's the most commonly chosen result for a power of 0.
  62. Here again, other error handling policies can be used:
  63. * `throw_on_error`: Throws `std::domain_error`
  64. * `errno_on_error`: Sets `::errno` to `EDOM` and returns 1.
  65. * `user_error`: Returns the result of `boost::math::policies::user_indeterminate_result_error`:
  66. this function must be defined by the user.
  67. Here is an example of error handling customization where we want to
  68. specify the result that has to be returned in case of error. We will
  69. thus use the `user_error` policy, by passing as second argument an
  70. instance of an overflow_error policy templated with `user_error`:
  71. // First we open the boost::math::policies namespace and define the `user_overflow_error`
  72. // by making it return the value we want in case of error (-1 here)
  73. namespace boost { namespace math { namespace policies {
  74. template <class T>
  75. T user_overflow_error(const char*, const char*, const T&)
  76. { return -1; }
  77. }}}
  78. // Then we invoke pow and indicate that we want to use the user_error policy
  79. using boost::math::policies;
  80. double result = pow<-5>(base, policy<overflow_error<user_error> >());
  81. // We can now test the returned value and treat the special case if needed:
  82. if (result == -1)
  83. {
  84. // there was an error, do something...
  85. }
  86. Another way is to redefine the default `overflow_error` policy by using the
  87. BOOST_MATH_OVERFLOW_ERROR_POLICY macro. Once the `user_overflow_error` function
  88. is defined as above, we can achieve the same result like this:
  89. // Redefine the default error_overflow policy
  90. #define BOOST_MATH_OVERFLOW_ERROR_POLICY user_error
  91. #include <boost/math/special_functions/pow.hpp>
  92. // From this point, passing a policy in argument is no longer needed, a call like this one
  93. // will return -1 in case of error:
  94. double result = pow<-5>(base);
  95. [h4 Acknowledgements]
  96. Bruno Lalande submitted this addition to Boost.Math.
  97. '''
  98. Thanks to Joaqu&#xed;n L&#xf3;pez Mu&#xf1;oz and Scott McMurray for their help in
  99. improving the implementation.
  100. '''
  101. [h4 References]
  102. D.E. Knuth, ['The Art of Computer Programming, Vol. 2: Seminumerical Algorithms], 2nd ed., Addison-Wesley, Reading, MA, 1981
  103. [endsect] [/section:ct_pow Compile Time Power of a Runtime Base]
  104. [/
  105. Copyright 2008 Bruno Lalande.
  106. Distributed under the Boost Software License, Version 1.0.
  107. (See accompanying file LICENSE_1_0.txt or copy at
  108. http://www.boost.org/LICENSE_1_0.txt).
  109. ]