Developer documentation
Version 3.0.3-105-gd3941f44
factorial.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __math_factorial_h__
18#define __math_factorial_h__
19
20#include <functional>
21#include <limits>
22
23namespace MR
24{
25 namespace Math
26 {
27
28
29 template <typename T>
30 T factorial (const T i) {
31 if (i < 2) {
32 return T(1);
33 } else if (i == std::numeric_limits<T>::max()) {
34 return i;
35 } else {
36 const T multiplier = factorial<T>(i-1);
37 const T result = i * multiplier;
38 if (result / multiplier == i)
39 return result;
40 else
41 return std::numeric_limits<T>::max();
42 }
43 };
44
45
46 }
47}
48
49#endif
T factorial(const T i)
Definition: factorial.h:30
Definition: base.h:24