public final class DD
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable, java.lang.Cloneable
A DoubleDouble uses a representation containing two double-precision values. A number x is represented as a pair of doubles, x.hi and x.lo, such that the number represented by x is x.hi + x.lo, where
|x.lo| <= 0.5*ulp(x.hi)and ulp(y) means "unit in the last place of y". The basic arithmetic operations are implemented using convenient properties of IEEE-754 floating-point arithmetic.
The range of values which can be represented is the same as in IEEE-754. The precision of the representable numbers is twice as great as IEEE-754 double precision.
The correctness of the arithmetic algorithms relies on operations being performed with standard IEEE-754 double precision and rounding. This is the Java standard arithmetic model, but for performance reasons Java implementations are not constrained to using this standard by default. Some processors (notably the Intel Pentium architecture) perform floating point operations in (non-IEEE-754-standard) extended-precision. A JVM implementation may choose to use the non-standard extended-precision as its default arithmetic mode. To prevent this from happening, this code uses the Java strictfp modifier, which forces all operations to take place in the standard IEEE-754 rounding model.
The API provides both a set of value-oriented operations and a set of mutating operations. Value-oriented operations treat DoubleDouble values as immutable; operations on them return new objects carrying the result of the operation. This provides a simple and safe semantics for writing DoubleDouble expressions. However, there is a performance penalty for the object allocations required. The mutable interface updates object values in-place. It provides optimum memory performance, but requires care to ensure that aliasing errors are not created and constant values are not changed.
For example, the following code example constructs three DD instances: two to hold the input values and one to hold the result of the addition.
DD a = new DD(2.0); DD b = new DD(3.0); DD c = a.add(b);In contrast, the following approach uses only one object:
DD a = new DD(2.0); a.selfAdd(3.0);
This implementation uses algorithms originally designed variously by Knuth, Kahan, Dekker, and Linnainmaa. Douglas Priest developed the first C implementation of these techniques. Other more recent C++ implementation are due to Keith M. Briggs and David Bailey et al.
Modifier and Type | Field and Description |
---|---|
static DD |
E
The value nearest to the constant e (the natural logarithm base).
|
static double |
EPS
The smallest representable relative difference between two {link @ DoubleDouble} values
|
private double |
hi
The high-order component of the double-double precision value.
|
private double |
lo
The low-order component of the double-double precision value.
|
private static int |
MAX_PRINT_DIGITS |
static DD |
NaN
A value representing the result of an operation which does not return a valid number.
|
private static DD |
ONE |
static DD |
PI
The value nearest to the constant Pi.
|
static DD |
PI_2
The value nearest to the constant Pi / 2.
|
private static java.lang.String |
SCI_NOT_EXPONENT_CHAR |
private static java.lang.String |
SCI_NOT_ZERO |
private static double |
SPLIT
The value to split a double-precision value on during multiplication
|
private static DD |
TEN |
static DD |
TWO_PI
The value nearest to the constant 2 * Pi.
|
Constructor and Description |
---|
DD()
Creates a new DoubleDouble with value 0.0.
|
DD(DD dd)
Creates a new DoubleDouble with value equal to the argument.
|
DD(double x)
Creates a new DoubleDouble with value x.
|
DD(double hi,
double lo)
Creates a new DoubleDouble with value (hi, lo).
|
DD(java.lang.String str)
Creates a new DoubleDouble with value equal to the argument.
|
Modifier and Type | Method and Description |
---|---|
DD |
abs()
Returns the absolute value of this value.
|
DD |
add(DD y)
Returns a new DoubleDouble whose value is (this + y).
|
DD |
add(double y)
Returns a new DoubleDouble whose value is (this + y).
|
DD |
ceil()
Returns the smallest (closest to negative infinity) value
that is not less than the argument and is equal to a mathematical integer.
|
java.lang.Object |
clone()
Creates and returns a copy of this value.
|
int |
compareTo(java.lang.Object o)
Compares two DoubleDouble objects numerically.
|
static DD |
copy(DD dd)
Creates a new DoubleDouble with the value of the argument.
|
private static DD |
createNaN() |
DD |
divide(DD y)
Computes a new DoubleDouble whose value is (this / y).
|
DD |
divide(double y)
Computes a new DoubleDouble whose value is (this / y).
|
double |
doubleValue()
Converts this value to the nearest double-precision number.
|
java.lang.String |
dump()
Dumps the components of this number to a string.
|
boolean |
equals(DD y)
Tests whether this value is equal to another DoubleDouble value.
|
private java.lang.String |
extractSignificantDigits(boolean insertDecimalPoint,
int[] magnitude)
Extracts the significant digits in the decimal representation of the argument.
|
DD |
floor()
Returns the largest (closest to positive infinity)
value that is not greater than the argument
and is equal to a mathematical integer.
|
boolean |
ge(DD y)
Tests whether this value is greater than or equals to another DoubleDouble value.
|
private java.lang.String |
getSpecialNumberString()
Returns the string for this value if it has a known representation.
|
boolean |
gt(DD y)
Tests whether this value is greater than another DoubleDouble value.
|
private void |
init(DD dd) |
private void |
init(double x) |
private void |
init(double hi,
double lo) |
int |
intValue()
Converts this value to the nearest integer.
|
boolean |
isNaN()
Tests whether this value is NaN.
|
boolean |
isNegative()
Tests whether this value is less than 0.
|
boolean |
isPositive()
Tests whether this value is greater than 0.
|
boolean |
isZero()
Tests whether this value is equal to 0.
|
boolean |
le(DD y)
Tests whether this value is less than or equal to another DoubleDouble value.
|
boolean |
lt(DD y)
Tests whether this value is less than another DoubleDouble value.
|
private static int |
magnitude(double x)
Determines the decimal magnitude of a number.
|
DD |
max(DD x)
Computes the maximum of this and another DD number.
|
DD |
min(DD x)
Computes the minimum of this and another DD number.
|
DD |
multiply(DD y)
Returns a new DoubleDouble whose value is (this * y).
|
DD |
multiply(double y)
Returns a new DoubleDouble whose value is (this * y).
|
DD |
negate()
Returns a new DoubleDouble whose value is -this.
|
static DD |
parse(java.lang.String str)
Converts a string representation of a real number into a DoubleDouble value.
|
DD |
pow(int exp)
Computes the value of this number raised to an integral power.
|
DD |
reciprocal()
Returns a DoubleDouble whose value is 1 / this.
|
DD |
rint()
Rounds this value to the nearest integer.
|
DD |
selfAdd(DD y)
Adds the argument to the value of this.
|
DD |
selfAdd(double y)
Adds the argument to the value of this.
|
private DD |
selfAdd(double yhi,
double ylo) |
DD |
selfDivide(DD y)
Divides this object by the argument, returning this.
|
DD |
selfDivide(double y)
Divides this object by the argument, returning this.
|
private DD |
selfDivide(double yhi,
double ylo) |
DD |
selfMultiply(DD y)
Multiplies this object by the argument, returning this.
|
DD |
selfMultiply(double y)
Multiplies this object by the argument, returning this.
|
private DD |
selfMultiply(double yhi,
double ylo) |
DD |
selfSqr()
Squares this object.
|
DD |
selfSubtract(DD y)
Subtracts the argument from the value of this.
|
DD |
selfSubtract(double y)
Subtracts the argument from the value of this.
|
DD |
setValue(DD value)
Set the value for the DD object.
|
DD |
setValue(double value)
Set the value for the DD object.
|
int |
signum()
Returns an integer indicating the sign of this value.
|
DD |
sqr()
Computes the square of this value.
|
static DD |
sqr(double x)
Computes the square of this value.
|
DD |
sqrt()
Computes the positive square root of this value.
|
static DD |
sqrt(double x) |
private static java.lang.String |
stringOfChar(char ch,
int len)
Creates a string of a given length containing the given character
|
DD |
subtract(DD y)
Computes a new DoubleDouble object whose value is (this - y).
|
DD |
subtract(double y)
Computes a new DoubleDouble object whose value is (this - y).
|
java.lang.String |
toSciNotation()
Returns the string representation of this value in scientific notation.
|
java.lang.String |
toStandardNotation()
Returns the string representation of this value in standard notation.
|
java.lang.String |
toString()
Returns a string representation of this number, in either standard or scientific notation.
|
DD |
trunc()
Returns the integer which is largest in absolute value and not further
from zero than this value.
|
static DD |
valueOf(double x)
Converts the double argument to a DoubleDouble number.
|
static DD |
valueOf(java.lang.String str)
Converts the string argument to a DoubleDouble number.
|
public static final DD PI
public static final DD TWO_PI
public static final DD PI_2
public static final DD E
public static final DD NaN
public static final double EPS
private static final double SPLIT
private double hi
private double lo
private static final int MAX_PRINT_DIGITS
private static final DD TEN
private static final DD ONE
private static final java.lang.String SCI_NOT_EXPONENT_CHAR
private static final java.lang.String SCI_NOT_ZERO
public DD()
public DD(double x)
x
- the value to initializepublic DD(double hi, double lo)
hi
- the high-order componentlo
- the high-order componentpublic DD(DD dd)
dd
- the value to initializepublic DD(java.lang.String str) throws java.lang.NumberFormatException
str
- the value to initialize byjava.lang.NumberFormatException
- if str is not a valid representation of a numberprivate static DD createNaN()
public static DD valueOf(java.lang.String str) throws java.lang.NumberFormatException
str
- a string containing a representation of a numeric valuejava.lang.NumberFormatException
- if s is not a valid representation of a numberpublic static DD valueOf(double x)
x
- a numeric valuepublic static DD copy(DD dd)
dd
- the DoubleDouble value to copypublic java.lang.Object clone()
clone
in class java.lang.Object
private final void init(double x)
private final void init(double hi, double lo)
private final void init(DD dd)
public DD setValue(DD value)
value
- a DD instance supplying an extended-precision value.public DD setValue(double value)
value
- a floating point value to be stored in the instance.public final DD add(DD y)
y
- the addendpublic final DD add(double y)
y
- the addendpublic final DD selfAdd(DD y)
y
- the addendpublic final DD selfAdd(double y)
y
- the addendprivate final DD selfAdd(double yhi, double ylo)
public final DD subtract(DD y)
y
- the subtrahendpublic final DD subtract(double y)
y
- the subtrahendpublic final DD selfSubtract(DD y)
y
- the addendpublic final DD selfSubtract(double y)
y
- the addendpublic final DD negate()
public final DD multiply(DD y)
y
- the multiplicandpublic final DD multiply(double y)
y
- the multiplicandpublic final DD selfMultiply(DD y)
y
- the value to multiply bypublic final DD selfMultiply(double y)
y
- the value to multiply byprivate final DD selfMultiply(double yhi, double ylo)
public final DD divide(DD y)
y
- the divisorpublic final DD divide(double y)
y
- the divisorpublic final DD selfDivide(DD y)
y
- the value to divide bypublic final DD selfDivide(double y)
y
- the value to divide byprivate final DD selfDivide(double yhi, double ylo)
public final DD reciprocal()
public DD floor()
public DD ceil()
public int signum()
public DD rint()
public DD trunc()
public DD abs()
public DD sqr()
public DD selfSqr()
public static DD sqr(double x)
public DD sqrt()
public static DD sqrt(double x)
public DD pow(int exp)
exp
- the integer exponentpublic DD min(DD x)
x
- a DD numberpublic DD max(DD x)
x
- a DD numberpublic double doubleValue()
public int intValue()
public boolean isZero()
public boolean isNegative()
public boolean isPositive()
public boolean isNaN()
public boolean equals(DD y)
y
- a DoubleDouble valuepublic boolean gt(DD y)
y
- a DoubleDouble valuepublic boolean ge(DD y)
y
- a DoubleDouble valuepublic boolean lt(DD y)
y
- a DoubleDouble valuepublic boolean le(DD y)
y
- a DoubleDouble valuepublic int compareTo(java.lang.Object o)
compareTo
in interface java.lang.Comparable
public java.lang.String dump()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toStandardNotation()
public java.lang.String toSciNotation()
private java.lang.String extractSignificantDigits(boolean insertDecimalPoint, int[] magnitude)
y
- the number to extract ( >= 0)decimalPointPos
- the position in which to insert a decimal pointprivate static java.lang.String stringOfChar(char ch, int len)
ch
- the character to be repeatedlen
- the len of the desired stringprivate java.lang.String getSpecialNumberString()
private static int magnitude(double x)
x
- the number to find the magnitude ofpublic static DD parse(java.lang.String str) throws java.lang.NumberFormatException
[+|-] {digit} [ . {digit} ] [ ( e | E ) [+|-] {digit}+
str
- the string to parsejava.lang.NumberFormatException
- if str is not a valid representation of a number