You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/01/28 17:19:53 UTC
[commons-numbers] branch master updated (69cbbef -> 980d80a)
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git.
from 69cbbef Changed multiplier to use the value 2^27 + 1 as a double.
new 649fe64 Clean-up the constants in Complex.
new 3bb0f69 Clean-up getScale() method.
new 980d80a Remove methods for (a * c + b * d) and (b * c - a * d) in Complex divide
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/commons/numbers/complex/Complex.java | 93 ++++++----------------
1 file changed, 23 insertions(+), 70 deletions(-)
[commons-numbers] 03/03: Remove methods for (a * c + b * d) and (b
* c - a * d) in Complex divide
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 980d80a40e67d4513f809a74e433fa9d58abfc64
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Jan 28 17:19:48 2020 +0000
Remove methods for (a * c + b * d) and (b * c - a * d) in Complex divide
The scaling now reduces a and b before computing the division if there
is a chance of overflow. Thus the specialised methods to detect overflow
are redundant and cannot be hit by any input arguments, e.g.
divide(Double.MAX_VALUE, Double.MAX_VALUE, 1, 1).
This returns the listing for edge case handling to match ISO C99 G.5.1
(8).
---
.../apache/commons/numbers/complex/Complex.java | 70 ++++------------------
1 file changed, 13 insertions(+), 57 deletions(-)
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 a78fe8a..04c7a6e 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
@@ -658,29 +658,29 @@ public final class Complex implements Serializable {
d = Math.scalb(d, -ilogbw);
}
final double denom = c * c + d * d;
- // Divisor is in the range [1, 2).
- // Avoid overflow if a or b are very big. Here we do not need to
- // handle sub-normal exponents so just get the maximum exponent.
+
+ // Note: Modification from the listing in ISO C99 G.5.1 (8):
+ // Avoid overflow if a or b are very big.
+ // Since (c, d) in the range [1, 2) the sum (ac + bd) could overflow
+ // when (a, b) are both above (Double.MAX_VALUE / 4). The same applies to
+ // (bc - ad) with large negative values.
+ // Use the maximum exponent as an approximation to the magnitude.
if (getMaxExponent(a, b) > Double.MAX_EXPONENT - 2) {
ilogbw -= 2;
a /= 4;
b /= 4;
}
+
double x = Math.scalb((a * c + b * d) / denom, -ilogbw);
double y = Math.scalb((b * c - a * d) / denom, -ilogbw);
// Recover infinities and zeros that computed as NaN+iNaN
// the only cases are nonzero/zero, infinite/finite, and finite/infinite, ...
- // --------------
- // Modification from the listing in ISO C99 G.5.1 (8):
- // Prevent overflow in (a * c + b * d) and (b * c - a * d).
- // It is only the sign that is important. not the magnitude.
- // --------------
if (Double.isNaN(x) && Double.isNaN(y)) {
if ((denom == 0.0) &&
(!Double.isNaN(a) || !Double.isNaN(b))) {
// nonzero/zero
// This case produces the same result as divide by a real-only zero
- // using divide(+/-0.0).
+ // using Complex.divide(+/-0.0)
x = Math.copySign(Double.POSITIVE_INFINITY, c) * a;
y = Math.copySign(Double.POSITIVE_INFINITY, c) * b;
} else if ((Double.isInfinite(a) || Double.isInfinite(b)) &&
@@ -688,65 +688,21 @@ public final class Complex implements Serializable {
// infinite/finite
a = boxInfinity(a);
b = boxInfinity(b);
- x = Double.POSITIVE_INFINITY * computeACplusBD(a, b, c, d);
- y = Double.POSITIVE_INFINITY * computeBCminusAD(a, b, c, d);
+ x = Double.POSITIVE_INFINITY * (a * c + b * d);
+ y = Double.POSITIVE_INFINITY * (b * c - a * d);
} else if ((Double.isInfinite(c) || Double.isInfinite(d)) &&
Double.isFinite(a) && Double.isFinite(b)) {
// finite/infinite
c = boxInfinity(c);
d = boxInfinity(d);
- x = 0.0 * computeACplusBD(a, b, c, d);
- y = 0.0 * computeBCminusAD(a, b, c, d);
+ x = 0.0 * (a * c + b * d);
+ y = 0.0 * (b * c - a * d);
}
}
return new Complex(x, y);
}
/**
- * Compute {@code a*c + b*d} without overflow.
- * It is assumed: either {@code a} an\( b \)b} or {@code c} and {@code d} are
- * either zero or one (i.e. a boxed infinity); and the sign of the result is important,
- * not the value.
- *
- * @param a the a
- * @param b the b
- * @param c the c
- * @param d the d
- * @return The result
- */
- private static double computeACplusBD(double a, double b, double c, double d) {
- final double ac = a * c;
- final double bd = b * d;
- final double result = ac + bd;
- return Double.isFinite(result) ?
- result :
- // Overflow. Just divide by 2 as it is the sign of the result that matters.
- ac * 0.5 + bd * 0.5;
- }
-
- /**
- * Compute {@code b*c - a*d} without overflow.
- * It is assumed: either {@code a} and {@code b} or {@code c} and {@code d} are
- * either zero or one (i.e. a boxed infinity); and the sign of the result is important,
- * not the value.
- *
- * @param a the a
- * @param b the b
- * @param c the c
- * @param d the d
- * @return The result
- */
- private static double computeBCminusAD(double a, double b, double c, double d) {
- final double bc = b * c;
- final double ad = a * d;
- final double result = bc - ad;
- return Double.isFinite(result) ?
- result :
- // Overflow. Just divide by 2 as it is the sign of the result that matters.
- bc * 0.5 - ad * 0.5;
- }
-
- /**
* Returns a {@code Complex} whose value is {@code (this / divisor)},
* with {@code divisor} interpreted as a real number.
* Implements the formula:
[commons-numbers] 01/03: Clean-up the constants in Complex.
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 649fe6418f55a7535eae2667d4d077269176ac1a
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Jan 28 16:42:20 2020 +0000
Clean-up the constants in Complex.
Use underscore notation for hex representations.
---
.../main/java/org/apache/commons/numbers/complex/Complex.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
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 b44bc20..1b7eefc 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
@@ -99,7 +99,7 @@ public final class Complex implements Serializable {
/** The bit representation of {@code -0.0}. */
private static final long NEGATIVE_ZERO_LONG_BITS = Double.doubleToLongBits(-0.0);
/** Exponent offset in IEEE754 representation. */
- private static final long EXPONENT_OFFSET = 1023L;
+ private static final int EXPONENT_OFFSET = 1023;
/**
* Largest double-precision floating-point number such that
* {@code 1 + EPSILON} is numerically equal to 1. This value is an upper
@@ -113,9 +113,9 @@ public final class Complex implements Serializable {
*/
private static final double EPSILON = Double.longBitsToDouble((EXPONENT_OFFSET - 53L) << 52);
/** Mask to remove the sign bit from a long. */
- private static final long UNSIGN_MASK = 0x7fffffffffffffffL;
+ private static final long UNSIGN_MASK = 0x7fff_ffff_ffff_ffffL;
/** Mask to extract the 52-bit mantissa from a long representation of a double. */
- private static final long MANTISSA_MASK = 0x000fffffffffffffL;
+ private static final long MANTISSA_MASK = 0x000f_ffff_ffff_ffffL;
/** The multiplier used to split the double value into hi and low parts. This must be odd
* and a value of 2^s + 1 in the range {@code p/2 <= s <= p-1} where p is the number of
* bits of precision of the floating point number. Here {@code s = 27}.*/
@@ -127,7 +127,7 @@ public final class Complex implements Serializable {
* as used in boost::math::complex.
* @see <a href="https://svn.boost.org/trac/boost/ticket/7290">Boost ticket 7290</a>
*/
- private static final double A_CROSSOVER = 10;
+ private static final double A_CROSSOVER = 10.0;
/** Crossover point to switch computation for asin/acos factor B. */
private static final double B_CROSSOVER = 0.6471;
/**
@@ -3312,7 +3312,7 @@ public final class Complex implements Serializable {
// Only interested in the maximum
final long max = Math.max(x, y);
// Get the unbiased exponent
- int exp = (int) ((max >>> 52) - EXPONENT_OFFSET);
+ int exp = ((int) (max >>> 52)) - EXPONENT_OFFSET;
// Do not distinguish nan/inf
if (exp == Double.MAX_EXPONENT + 1) {
[commons-numbers] 02/03: Clean-up getScale() method.
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 3bb0f6952fb5990b8d71c691190c991741838428
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Jan 28 16:44:34 2020 +0000
Clean-up getScale() method.
Rename max to bits for clarity it is the bit representation.
Drop nan/inf case.
---
.../java/org/apache/commons/numbers/complex/Complex.java | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
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 1b7eefc..a78fe8a 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
@@ -3278,7 +3278,7 @@ public final class Complex implements Serializable {
* two numbers. In contrast to {@link Math#getExponent(double)} this handles
* sub-normal numbers by computing the number of leading zeros in the mantissa
* and shifting the unbiased exponent. The result is that for all finite, non-zero,
- * numbers {@code a, b}, the magnitude of {@code scalb(x, -getScale(x))} is
+ * numbers {@code a, b}, the magnitude of {@code scalb(x, -getScale(a, b))} is
* always in the range {@code [1, 2)}, where {@code x = max(|a|, |b|)}.
*
* <p>This method is a functional equivalent of the c function ilogb(double) adapted for
@@ -3310,25 +3310,22 @@ public final class Complex implements Serializable {
final long x = Double.doubleToRawLongBits(a) & UNSIGN_MASK;
final long y = Double.doubleToRawLongBits(b) & UNSIGN_MASK;
// Only interested in the maximum
- final long max = Math.max(x, y);
+ final long bits = Math.max(x, y);
// Get the unbiased exponent
- int exp = ((int) (max >>> 52)) - EXPONENT_OFFSET;
+ int exp = ((int) (bits >>> 52)) - EXPONENT_OFFSET;
- // Do not distinguish nan/inf
- if (exp == Double.MAX_EXPONENT + 1) {
- return exp;
- }
+ // No case to distinguish nan/inf
// Handle sub-normal numbers
if (exp == Double.MIN_EXPONENT - 1) {
// Special case for zero, return as nan/inf to indicate scaling is not possible
- if (max == 0) {
+ if (bits == 0) {
return Double.MAX_EXPONENT + 1;
}
// A sub-normal number has an exponent below -1022. The amount below
// is defined by the number of shifts of the most significant bit in
// the mantissa that is required to get a 1 at position 53 (i.e. as
// if it were a normal number with assumed leading bit)
- final long mantissa = max & MANTISSA_MASK;
+ final long mantissa = bits & MANTISSA_MASK;
exp -= Long.numberOfLeadingZeros(mantissa << 12);
}
return exp;