4 /*****************************************************************************\
6 * Name : mathematical operations *
7 * Author : Chris Koeritz *
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 \*****************************************************************************/
18 #include <basis/definitions.h>
20 namespace mathematics {
22 //! A grab-bag of mathematical functions that are frequently useful.
27 //! returns the rounded integer value for "to_round".
28 static int round_it(float to_round)
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++;
36 //! returns the rounded integer value for "to_round".
37 static int round_it(double to_round)
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++;
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)
49 if (!raise_to) return 1;
50 basis::u_int to_return = 2;
51 for (basis::u_int i = 1; i < raise_to; i++)
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); }