facf.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* facf.c
  2. *
  3. * Factorial function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float y, facf();
  10. * int i;
  11. *
  12. * y = facf( i );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns factorial of i = 1 * 2 * 3 * ... * i.
  19. * fac(0) = 1.0.
  20. *
  21. * Due to machine arithmetic bounds the largest value of
  22. * i accepted is 33 in single precision arithmetic.
  23. * Greater values, or negative ones,
  24. * produce an error message and return MAXNUM.
  25. *
  26. *
  27. *
  28. * ACCURACY:
  29. *
  30. * For i < 34 the values are simply tabulated, and have
  31. * full machine accuracy.
  32. *
  33. */
  34. /*
  35. Cephes Math Library Release 2.0: April, 1987
  36. Copyright 1984, 1987 by Stephen L. Moshier
  37. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  38. */
  39. #include <math.h>
  40. /* Factorials of integers from 0 through 33 */
  41. static float factbl[] = {
  42. 1.00000000000000000000E0,
  43. 1.00000000000000000000E0,
  44. 2.00000000000000000000E0,
  45. 6.00000000000000000000E0,
  46. 2.40000000000000000000E1,
  47. 1.20000000000000000000E2,
  48. 7.20000000000000000000E2,
  49. 5.04000000000000000000E3,
  50. 4.03200000000000000000E4,
  51. 3.62880000000000000000E5,
  52. 3.62880000000000000000E6,
  53. 3.99168000000000000000E7,
  54. 4.79001600000000000000E8,
  55. 6.22702080000000000000E9,
  56. 8.71782912000000000000E10,
  57. 1.30767436800000000000E12,
  58. 2.09227898880000000000E13,
  59. 3.55687428096000000000E14,
  60. 6.40237370572800000000E15,
  61. 1.21645100408832000000E17,
  62. 2.43290200817664000000E18,
  63. 5.10909421717094400000E19,
  64. 1.12400072777760768000E21,
  65. 2.58520167388849766400E22,
  66. 6.20448401733239439360E23,
  67. 1.55112100433309859840E25,
  68. 4.03291461126605635584E26,
  69. 1.0888869450418352160768E28,
  70. 3.04888344611713860501504E29,
  71. 8.841761993739701954543616E30,
  72. 2.6525285981219105863630848E32,
  73. 8.22283865417792281772556288E33,
  74. 2.6313083693369353016721801216E35,
  75. 8.68331761881188649551819440128E36
  76. };
  77. #define MAXFACF 33
  78. extern float MAXNUMF;
  79. #ifdef ANSIC
  80. float facf( int i )
  81. #else
  82. float facf(i)
  83. int i;
  84. #endif
  85. {
  86. if( i < 0 )
  87. {
  88. mtherr( "facf", SING );
  89. return( MAXNUMF );
  90. }
  91. if( i > MAXFACF )
  92. {
  93. mtherr( "facf", OVERFLOW );
  94. return( MAXNUMF );
  95. }
  96. /* Get answer from table for small i. */
  97. return( factbl[i] );
  98. }