(svn r9150) [gamebalance] -Add: a function to compute the n-th root of a FixedT. gamebalance
authorcelestar
Tue, 13 Mar 2007 21:49:33 +0000
branchgamebalance
changeset 9887 3d32aa4ae67c
parent 9886 67b675b827c1
child 9888 7cf72895ca8c
(svn r9150) [gamebalance] -Add: a function to compute the n-th root of a FixedT.
src/fixedt.h
--- a/src/fixedt.h	Tue Mar 13 16:04:23 2007 +0000
+++ b/src/fixedt.h	Tue Mar 13 21:49:33 2007 +0000
@@ -139,7 +139,6 @@
 };
 
 
-
 /**
  * A class that defines a fixed-point data type, which a variable length and precision.
  * The data type that is defined is a fixed-point variable that has and number of Tdec_bits
@@ -310,8 +309,12 @@
 	 * @param value The Fixed-point variable we want to write in the stream
 	 */
 	friend std::ostream& operator << (std::ostream &os, const FixedT &value) { os << (double)value.m_data / (1ULL << Raw::dec_bits); return os; }
+};
 
-};
+/** 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 */
+static const int PRECISION = 5;
 
 /**
  * Computes a integral, positive power of a FixedT. Uses an optimized algorithm
@@ -320,7 +323,7 @@
  *
  * @param arg   The number to compute the power for
  * @param pow   the power
- * @returns     arg^pow
+ * @return      arg^pow
  * @todo        Add a nice LaTeX forumla to the documentation
  * @todo        Implement negative powers
  */
@@ -335,10 +338,23 @@
 	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 */
-static const int PRECISION = 5;
+/**
+ * Computes the n-th root of a number using an iterative scheme
+ * @param arg  the number to compute the root from
+ * @param root The "n" in the n-th root
+ * @return     the root
+ * @todo       Abort not after a fixed number of iterations but dynamically by step size
+ * @todo       Make some better first-guess.
+ */
+template <typename Tstorage, int Tdec_bits>
+FixedT<Tstorage, Tdec_bits> nroot(const FixedT<Tstorage, Tdec_bits> &arg, int root)
+{
+	FixedT<int64, 20> one = 1;
+	FixedT<int64, 20> guess(6, 5);
+	for (int i = 0; i < 150; i++) guess = (one / root) * ( guess * (root - 1) + arg / pow(guess, root - 1));
+
+	return guess;
+}
 
 /**
  * Computes a single element of the Taylor series for the approximation of the cosine.
@@ -376,7 +392,7 @@
 /**
  * Computes the cosine of an argument using a Taylor series.
  * @param arg The number to compute the cosine from, assumes arg to be in radians.
- * @returns   The cosine
+ * @return    The cosine
  * @todo      Optimize the two while loops.
  * @note      It is possible to factor out the series to optimize even further, but is it
  *            worth the effort? It could reduce the opcount a little.
@@ -404,7 +420,7 @@
  * Computes the sine of an argument using a Taylor series, uses the cosine
  * computation in fact, as \f$sin(x) = cos(x - \frac{\pi}{2}))\f$
  * @param arg The number to compute the cosine from, assumes arg to be in radians.
- * @returns   The sine
+ * @return    The sine
  */
 template <typename Tstorage, int Tdec_bits>
 FixedT<Tstorage, Tdec_bits> sin(const FixedT<Tstorage, Tdec_bits> &arg)