From 81ba9842e5d85aca7bc7b26b01626407899654ac Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Fri, 21 Jan 2011 03:24:14 +0000 Subject: [PATCH] MATH-489 Fix overflows in acos calculation git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1061609 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/commons/math/util/FastMath.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/math/util/FastMath.java b/src/main/java/org/apache/commons/math/util/FastMath.java index fdbed3eb4..d55342cd5 100644 --- a/src/main/java/org/apache/commons/math/util/FastMath.java +++ b/src/main/java/org/apache/commons/math/util/FastMath.java @@ -3135,7 +3135,18 @@ public class FastMath { // Compute ratio r = y/x double r = y/x; - temp = r * 1073741824.0; + + // Did r overflow? + if (Double.isInfinite(r)) { // x is effectively zero + return Math.PI/2; // so return the appropriate value + } + + if (abs(r) < Double.MAX_VALUE/1073741824.0){ // is it safe to split r ? + temp = r * 1073741824.0; + } else { + temp = 0.0; + } + double ra = r + temp - temp; double rb = r - ra;