Hi. On Thu, Aug 09, 2012 at 08:07:25AM -0000, l...@apache.org wrote: > Author: luc > Date: Thu Aug 9 08:07:25 2012 > New Revision: 1371082 > > URL: http://svn.apache.org/viewvc?rev=1371082&view=rev > Log: > added a function to raise a double to an int power > > Modified: > > commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java > > Modified: > commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java > URL: > http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java?rev=1371082&r1=1371081&r2=1371082&view=diff > ============================================================================== > --- > commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java > (original) > +++ > commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java > Thu Aug 9 08:07:25 2012 > @@ -1582,6 +1582,34 @@ public class FastMath { > > > /** > + * Raise a double to an int power. > + * > + * @param d Number to raise. > + * @param e Exponent. > + * @return d<sup>e</sup> > + */ > + public static double pow(double d, int e) { > + if (e == 0) { > + return 1.0; > + } else if (e < 0) { > + e = -e; > + d = 1.0 / d; > + } > + > + double result = 1; > + double d2p = d; > + while (e != 0) { > + if ((e & 0x1) != 0) { > + result *= d2p; > + } > + d2p *= d2p; > + e = e >> 1; > + } > + > + return result; > + }
I've added a unit test for this function. It shows that the result is not the same as with "pow(double, double)" (cf. tolerance in "assertEquals"). I didn't check which one is more accurate, but I thought that we should be aware of the discrepancy. Regards, Gilles --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org