00001
00002
00003
00004
00005
00006 namespace NewGamePhysics.Mathematics
00007 {
00008 using System;
00009
00013 public static class Factorial
00014 {
00020 public static ulong Calc(uint x)
00021 {
00022 if (x > 21)
00023 {
00024 throw new ArgumentOutOfRangeException(
00025 "x",
00026 "value must be in the range [0,21]");
00027 }
00028
00029
00030 if (x == 0)
00031 {
00032 return 1;
00033 }
00034
00035 ulong f = x;
00036 for (ulong i = 2; i < x; i++)
00037 {
00038 ulong ui = (ulong)i;
00039 f *= ui;
00040 }
00041
00042 return f;
00043 }
00044
00053 public static ulong CalcDouble(int x)
00054 {
00055 if ((x < -1) || (x > 32))
00056 {
00057 throw new ArgumentOutOfRangeException(
00058 "x",
00059 "value must be in the range [-1,32]");
00060 }
00061
00062
00063 if ((x == -1) || (x == 0))
00064 {
00065 return 1;
00066 }
00067
00068 ulong f = 1;
00069 for (ulong i = (ulong)x; i >= 2; i -= 2)
00070 {
00071 f *= i;
00072 }
00073
00074 return f;
00075 }
00076 }
00077 }