(svn r9113) [gamebalance] -Add: Implemented a function that computes positive, integral powers of a FixedT gamebalance
authorcelestar
Sun, 11 Mar 2007 13:57:34 +0000
branchgamebalance
changeset 9880 bd97cc28b569
parent 9879 c552b8cedd3d
child 9881 fbb3eab0e186
(svn r9113) [gamebalance] -Add: Implemented a function that computes positive, integral powers of a FixedT
src/fixedt.h
--- a/src/fixedt.h	Sun Mar 11 10:21:12 2007 +0000
+++ b/src/fixedt.h	Sun Mar 11 13:57:34 2007 +0000
@@ -313,6 +313,28 @@
 
 };
 
+/**
+ * Computes a integral, positive power of a FixedT. Uses an optimized algorithm
+ * to keep computational requirement at bay, by decomposing the power into powers
+ * of two itself.
+ *
+ * @param arg   The number to compute the power for
+ * @param pow   the power
+ * @returns     arg^pow
+ * @todo        Add a nice LaTeX forumla to the documentation
+ * @todo        Implement negative powers
+ */
+template <typename Tstorage, int Tdec_bits>
+FixedT<Tstorage, Tdec_bits> pow(const FixedT<Tstorage, Tdec_bits> &arg, int pow)
+{
+	FixedT<int64, Tdec_bits> temp = 1;
+	FixedT<int64, Tdec_bits> mul = arg;
+	if (pow == 0) return (FixedT<Tstorage, Tdec_bits>)1;
+	for (int i = 1; pow > 0; pow >>= 1, i <<= 1, mul *= mul) if (pow & 1) temp *= mul;
+
+	return temp;
+}
+
 /** The value of \f$\pi\f$ with ample precision for our computations */
 static const FixedT<int64, 16> PI(3141592, 1000000);
 /** The number of elements used in Taylor approximations */