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:
parent
483954b005
commit
f55eb717d2
|
@ -3847,8 +3847,39 @@ public class FastMath {
|
||||||
* @param y a value
|
* @param y a value
|
||||||
* @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
|
* @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
|
||||||
*/
|
*/
|
||||||
public static double hypot(double x, double y) {
|
public static double hypot(final double x, final double y) {
|
||||||
return StrictMath.hypot(x, y); // TODO provide our own implementation
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -58,9 +58,9 @@ The <action> type attribute can be add,update,fix,remove.
|
||||||
</action>
|
</action>
|
||||||
<action dev="luc" type="fix" issue="MATH-478">
|
<action dev="luc" type="fix" issue="MATH-478">
|
||||||
FastMath is not an exact replacement for StrictMath
|
FastMath is not an exact replacement for StrictMath
|
||||||
(partially fixed) added nextAfter(double, double) and nextAfter(float,double)
|
(partially fixed) added hypot(double, double), nextAfter(double, double)
|
||||||
(beware of the strange double second argument) so that they handle
|
and nextAfter(float,double) (beware of the strange double second argument)
|
||||||
special values in the way as StrictMath
|
so that they handle special values in the way as StrictMath
|
||||||
</action>
|
</action>
|
||||||
<action dev="luc" type="fix" issue="MATH-497">
|
<action dev="luc" type="fix" issue="MATH-497">
|
||||||
FastMath is not an exact replacement for StrictMath
|
FastMath is not an exact replacement for StrictMath
|
||||||
|
|
Loading…
Reference in New Issue