Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 98170 invoked from network); 23 Jan 2011 22:38:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Jan 2011 22:38:11 -0000 Received: (qmail 67053 invoked by uid 500); 23 Jan 2011 22:38:11 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 66962 invoked by uid 500); 23 Jan 2011 22:38:11 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 66955 invoked by uid 99); 23 Jan 2011 22:38:11 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Jan 2011 22:38:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Jan 2011 22:38:10 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D70DB23889FD; Sun, 23 Jan 2011 22:37:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1062559 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/FastMath.java site/xdoc/changes.xml Date: Sun, 23 Jan 2011 22:37:50 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110123223750.D70DB23889FD@eris.apache.org> Author: luc Date: Sun Jan 23 22:37:50 2011 New Revision: 1062559 URL: http://svn.apache.org/viewvc?rev=1062559&view=rev Log: added FastMath.hypot JIRA: MATH-478 Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/FastMath.java commons/proper/math/trunk/src/site/xdoc/changes.xml Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/FastMath.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/FastMath.java?rev=1062559&r1=1062558&r2=1062559&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/FastMath.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/FastMath.java Sun Jan 23 22:37:50 2011 @@ -3847,8 +3847,39 @@ public class FastMath { * @param y a value * @return sqrt(x2 +y2) */ - 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); + + } + + } } /** Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1062559&r1=1062558&r2=1062559&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Jan 23 22:37:50 2011 @@ -192,9 +192,9 @@ The type attribute can be add,u 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 FastMath is not an exact replacement for StrictMath