Moved the check for the safe region to a method.
diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index b0583ff..c390ccf 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -1353,7 +1353,7 @@
final double xp1 = x + 1;
final double xm1 = x - 1;
- if ((x < SAFE_MAX) && (x > SAFE_MIN) && (y < SAFE_MAX) && (y > SAFE_MIN)) {
+ if (inRegion(x, y, SAFE_MIN, SAFE_MAX)) {
final double yy = y * y;
final double r = Math.sqrt(xp1 * xp1 + yy);
final double s = Math.sqrt(xm1 * xm1 + yy);
@@ -1524,7 +1524,7 @@
final double xp1 = x + 1;
final double xm1 = x - 1;
- if ((x < SAFE_MAX) && (x > SAFE_MIN) && (y < SAFE_MAX) && (y > SAFE_MIN)) {
+ if (inRegion(x, y, SAFE_MIN, SAFE_MAX)) {
final double yy = y * y;
final double r = Math.sqrt(xp1 * xp1 + yy);
final double s = Math.sqrt(xm1 * xm1 + yy);
@@ -1723,10 +1723,10 @@
// Check the safe region.
// The lower and upper bounds have been copied from boost::math::atanh.
// They are different from the safe region for asin and acos.
- // x >= SAFE_UPPER: (1-x) == x
+ // x >= SAFE_UPPER: (1-x) == -x
// x <= SAFE_LOWER: 1 - x^2 = 1
- if ((x > SAFE_LOWER) && (x < SAFE_UPPER) && (y > SAFE_LOWER) && (y < SAFE_UPPER)) {
+ if (inRegion(x, y, SAFE_LOWER, SAFE_UPPER)) {
// Normal computation within a safe region.
// minus x plus 1: (-x+1)
@@ -2689,6 +2689,19 @@
}
/**
+ * Checks if both x and y are in the region defined by the minimum and maximum.
+ *
+ * @param x x value.
+ * @param y y value.
+ * @param min the minimum (exclusive).
+ * @param max the maximum (exclusive).
+ * @return true if inside the region
+ */
+ private static boolean inRegion(double x, double y, double min, double max) {
+ return (x < max) && (x > min) && (y < max) && (y > min);
+ }
+
+ /**
* Creates an exception.
*
* @param message Message prefix.