factorial_example.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // TestFactorial.cpp
  2. //
  3. // Factorials and Binomial Coefficients.
  4. //
  5. // Copyright Datasim Education BV 2009-2010
  6. // Copyright John Maddock and Paul A. Bristow 2010
  7. // Use, modification and distribution are subject to the
  8. // Boost Software License, Version 1.0.
  9. // (See accompanying file LICENSE_1_0.txt
  10. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. #include <boost/math/special_functions/factorials.hpp>
  12. #include <boost/math/special_functions.hpp>
  13. #include <iostream>
  14. using namespace std;
  15. int main()
  16. {
  17. using namespace boost::math;
  18. // Factorials
  19. unsigned int n = 3;
  20. try
  21. {
  22. cout << "Factorial: " << factorial<double>(n) << endl;
  23. // Caution: You must provide a return type template value, so this will not compile
  24. // unsigned int nfac = factorial(n); // could not deduce template argument for 'T'
  25. // You must provide an explicit floating-point (not integer) return type.
  26. // If you do provide an integer type, like this:
  27. // unsigned int uintfac = factorial<unsigned int>(n);
  28. // you will also get a compile error, for MSVC C2338.
  29. // If you really want an integer type, you can convert from double:
  30. unsigned int intfac = static_cast<unsigned int>(factorial<double>(n));
  31. // this will be exact, until the result of the factorial overflows the integer type.
  32. cout << "Unchecked factorial: " << boost::math::unchecked_factorial<float>(n) << endl;
  33. // Note:
  34. // unsigned int unfac = boost::math::unchecked_factorial<unsigned int>(n);
  35. // also fails to compile for the same reasons.
  36. }
  37. catch(exception& e)
  38. {
  39. cout << e.what() << endl;
  40. }
  41. // Double factorial n!!
  42. try
  43. {
  44. //cout << "Double factorial: " << boost::math::double_factorial<unsigned>(n);
  45. }
  46. catch(exception& e)
  47. {
  48. cout << e.what() << endl;
  49. }
  50. // Rising and falling factorials
  51. try
  52. {
  53. int i = 2; double x = 8;
  54. cout << "Rising factorial: " << rising_factorial(x,i) << endl;
  55. cout << "Falling factorial: " << falling_factorial(x,i) << endl;
  56. }
  57. catch(exception& e)
  58. {
  59. cout << e.what() << endl;
  60. }
  61. // Binomial coefficients
  62. try
  63. {
  64. unsigned n = 10; unsigned k = 2;
  65. // cout << "Binomial coefficient: " << boost::math::binomial_coefficient<unsigned>(n,k) << endl;
  66. }
  67. catch(exception& e)
  68. {
  69. cout << e.what() << endl;
  70. }
  71. return 0;
  72. }
  73. /*
  74. Output:
  75. factorial_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\factorial_example.exe
  76. Factorial: 6
  77. Unchecked factorial: 6
  78. Rising factorial: 72
  79. Falling factorial: 56
  80. */