From 0e1ef339541fdaf46589e40168f91615ab77e83f Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 28 Jan 2015 17:15:26 +0000 Subject: [PATCH] integral factorial function --- lib/Math.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/Math.hpp b/lib/Math.hpp index bf94943..546ca2e 100644 --- a/lib/Math.hpp +++ b/lib/Math.hpp @@ -52,6 +52,22 @@ namespace MATH_NAMESPACE return x*pow(x); } + // integral factorial function + template + T factorial(const T n) + { + static_assert(std::is_integral::value, + "factorial must me used with an integral argument"); + + T res = n; + for (T i = n - 1; i != 0; --i) + { + res *= i; + } + + return res; + } + // Constants const double pi = 3.1415926535897932384626433832795028841970; const double e = 2.7182818284590452353602874713526624977572;