added FastMath.hypot

JIRA: MATH-478

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1062558 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-01-23 22:37:15 +00:00
parent 483954b005
commit f55eb717d2
2 changed files with 36 additions and 5 deletions

View File

@ -3847,8 +3847,39 @@ public class FastMath {
* @param y a value
* @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
*/
public static double hypot(double x, double y) {
return StrictMath.hypot(x, y); // TODO provide our own implementation
public static double hypot(final double x, final double y) {
if (Double.isInfinite(x) || Double.isInfinite(y)) {
return Double.POSITIVE_INFINITY;
} else if (Double.isNaN(x) || Double.isNaN(y)) {
return Double.NaN;
} else {
final int expX = getExponent(x);
final int expY = getExponent(y);
if (expX > expY + 27) {
// y is neglectible with respect to x
return abs(x);
} else if (expY > expX + 27) {
// x is neglectible with respect to y
return abs(y);
} else {
// find an intermediate scale to avoid both overflow and underflow
final int middleExp = (expX + expY) / 2;
// scale parameters without losing precision
final double scaledX = scalb(x, -middleExp);
final double scaledY = scalb(y, -middleExp);
// compute scaled hypotenuse
final double scaledH = sqrt(scaledX * scaledX + scaledY * scaledY);
// remove scaling
return scalb(scaledH, middleExp);
}
}
}
/**

View File

@ -58,9 +58,9 @@ The <action> type attribute can be add,update,fix,remove.
</action>
<action dev="luc" type="fix" issue="MATH-478">
FastMath is not an exact replacement for StrictMath
(partially fixed) added nextAfter(double, double) and nextAfter(float,double)
(beware of the strange double second argument) so that they handle
special values in the way as StrictMath
(partially fixed) added hypot(double, double), nextAfter(double, double)
and nextAfter(float,double) (beware of the strange double second argument)
so that they handle special values in the way as StrictMath
</action>
<action dev="luc" type="fix" issue="MATH-497">
FastMath is not an exact replacement for StrictMath