first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / mathematics / math_ops.h
1 #ifndef MATH_OPS_GROUP
2 #define MATH_OPS_GROUP
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : mathematical operations                                           *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include <basis/definitions.h>
19
20 namespace mathematics {
21
22 //! A grab-bag of mathematical functions that are frequently useful.
23
24 class math_ops
25 {
26 public:
27   //! returns the rounded integer value for "to_round".
28   static int round_it(float to_round)
29   {
30     int to_return = int(to_round);
31     // this uses a simplistic view of rounding.
32     if (to_round - float(to_return) > 0.5) to_return++;
33     return to_return;
34   }
35
36   //! returns the rounded integer value for "to_round".
37   static int round_it(double to_round)
38   {
39     int to_return = int(to_round);
40     // this uses a simplistic view of rounding.
41     if (to_round - double(to_return) > 0.5) to_return++;
42     return to_return;
43   }
44
45 /*
46   //! returns the number two to the power "raise_to" (i.e. 2^raise_to).
47   static basis::u_int pow_2(const basis::u_int raise_to)
48   {
49     if (!raise_to) return 1;
50     basis::u_int to_return = 2;
51     for (basis::u_int i = 1; i < raise_to; i++)
52       to_return *= 2;
53     return to_return;
54   }
55 */
56
57   //! returns n! (factorial), which is n * (n - 1) * (n - 2) ...
58   static basis::un_int factorial(int n)
59   { return (n < 2)? 1 : n * factorial(n - 1); }
60 };
61
62 } //namespace.
63
64 #endif
65