mirror of
https://github.com/apache/commons-math.git
synced 2025-02-08 02:59:36 +00:00
added a function to raise a double to an int power
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1371082 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e85e1c837
commit
0ee65b7169
@ -1581,6 +1581,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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes sin(x) - x, where |x| < 1/16.
|
||||
* Use a Remez polynomial approximation.
|
||||
|
Loading…
x
Reference in New Issue
Block a user