Class DD
- java.lang.Object
-
- org.tinfour.vividsolutions.jts.math.DD
-
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<DD>
public final class DD extends Object implements Serializable, Comparable<DD>, Cloneable
Implements extended-precision floating-point numbers which maintain 106 bits (approximately 30 decimal digits) of precision.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 architecure) 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
strictfpmodifier, 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.
References
- Priest, D., Algorithms for Arbitrary Precision Floating Point Arithmetic, in P. Kornerup and D. Matula, Eds., Proc. 10th Symposium on Computer Arithmetic, IEEE Computer Society Press, Los Alamitos, Calif., 1991.
- Yozo Hida, Xiaoye S. Li and David H. Bailey, Quad-Double Arithmetic: Algorithms, Implementation, and Application, manuscript, Oct 2000; Lawrence Berkeley National Laboratory Report BNL-46996.
- David Bailey, High Precision Software Directory;
http://crd.lbl.gov/~dhbailey/mpdist/index.html
- Author:
- Martin Davis
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static DDEThe value nearest to the constant e (the natural logarithm base).static doubleEPSThe smallest representable relative difference between two {link @ DoubleDouble} valuesstatic DDNaNA value representing the result of an operation which does not return a valid number.static DDPIThe value nearest to the constant Pi.static DDPI_2The value nearest to the constant Pi / 2.static DDTWO_PIThe value nearest to the constant 2 * Pi.
-
Constructor Summary
Constructors Constructor Description DD()Creates a new DoubleDouble with value 0.0.DD(double x)Creates a new DoubleDouble with value x.DD(double hi, double lo)Creates a new DoubleDouble with value (hi, lo).DD(String str)Creates a new DoubleDouble with value equal to the argument.DD(DD dd)Creates a new DoubleDouble with value equal to the argument.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DDabs()Returns the absolute value of this value.DDadd(double y)Returns a new DoubleDouble whose value is(this + y).DDadd(DD y)Returns a new DoubleDouble whose value is(this + y).DDceil()Returns the smallest (closest to negative infinity) value that is not less than the argument and is equal to a mathematical integer.Objectclone()Creates and returns a copy of this value.intcompareTo(DD other)Compares two DoubleDouble objects numerically.static DDcopy(DD dd)Creates a new DoubleDouble with the value of the argument.DDdivide(double y)Computes a new DoubleDouble whose value is(this / y).DDdivide(DD y)Computes a new DoubleDouble whose value is(this / y).doubledoubleValue()Converts this value to the nearest double-precision number.Stringdump()Dumps the components of this number to a string.booleanequals(DD y)Tests whether this value is equal to anotherDoubleDoublevalue.DDfloor()Returns the largest (closest to positive infinity) value that is not greater than the argument and is equal to a mathematical integer.booleange(DD y)Tests whether this value is greater than or equals to anotherDoubleDoublevalue.booleangt(DD y)Tests whether this value is greater than anotherDoubleDoublevalue.intintValue()Converts this value to the nearest integer.booleanisNaN()Tests whether this value is NaN.booleanisNegative()Tests whether this value is less than 0.booleanisPositive()Tests whether this value is greater than 0.booleanisZero()Tests whether this value is equal to 0.booleanle(DD y)Tests whether this value is less than or equal to anotherDoubleDoublevalue.booleanlt(DD y)Tests whether this value is less than anotherDoubleDoublevalue.DDmax(DD x)Computes the maximum of this and another DD number.DDmin(DD x)Computes the minimum of this and another DD number.DDmultiply(double y)Returns a new DoubleDouble whose value is(this * y).DDmultiply(DD y)Returns a new DoubleDouble whose value is(this * y).DDnegate()Returns a new DoubleDouble whose value is-this.static DDparse(String str)Converts a string representation of a real number into a DoubleDouble value.DDpow(int exp)Computes the value of this number raised to an integral power.DDreciprocal()Returns a DoubleDouble whose value is1 / this.DDrint()Rounds this value to the nearest integer.DDselfAdd(double y)Adds the argument to the value ofthis.DDselfAdd(DD y)Adds the argument to the value ofthis.DDselfDivide(double y)Divides this object by the argument, returningthis.DDselfDivide(DD y)Divides this object by the argument, returningthis.DDselfMultiply(double y)Multiplies this object by the argument, returningthis.DDselfMultiply(DD y)Multiplies this object by the argument, returningthis.DDselfSqr()Squares this object.DDselfSubtract(double y)Subtracts the argument from the value ofthis.DDselfSubtract(DD y)Subtracts the argument from the value ofthis.DDsetValue(double value)Set the value for the DD object.DDsetValue(DD value)Set the value for the DD object.intsignum()Returns an integer indicating the sign of this value.DDsqr()Computes the square of this value.static DDsqr(double x)Computes the square of the specified value.DDsqrt()Computes the positive square root of this value.static DDsqrt(double x)Returns the square root of the specified value as a double-precision instance.DDsubtract(double y)Computes a new DoubleDouble object whose value is(this - y).DDsubtract(DD y)Computes a new DoubleDouble object whose value is(this - y).StringtoSciNotation()Returns the string representation of this value in scientific notation.StringtoStandardNotation()Returns the string representation of this value in standard notation.StringtoString()Returns a string representation of this number, in either standard or scientific notation.DDtrunc()Returns the integer which is largest in absolute value and not further from zero than this value.static DDvalueOf(double x)Converts thedoubleargument to a DoubleDouble number.static DDvalueOf(String str)Converts the string argument to a DoubleDouble number.
-
-
-
Field Detail
-
PI
public static final DD PI
The value nearest to the constant Pi.
-
TWO_PI
public static final DD TWO_PI
The value nearest to the constant 2 * Pi.
-
PI_2
public static final DD PI_2
The value nearest to the constant Pi / 2.
-
E
public static final DD E
The value nearest to the constant e (the natural logarithm base).
-
NaN
public static final DD NaN
A value representing the result of an operation which does not return a valid number.
-
EPS
public static final double EPS
The smallest representable relative difference between two {link @ DoubleDouble} values- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DD
public DD()
Creates a new DoubleDouble with value 0.0.
-
DD
public DD(double x)
Creates a new DoubleDouble with value x.- Parameters:
x- the value to initialize
-
DD
public DD(double hi, double lo)Creates a new DoubleDouble with value (hi, lo).- Parameters:
hi- the high-order componentlo- the high-order component
-
DD
public DD(DD dd)
Creates a new DoubleDouble with value equal to the argument.- Parameters:
dd- the value to initialize
-
DD
public DD(String str) throws NumberFormatException
Creates a new DoubleDouble with value equal to the argument.- Parameters:
str- the value to initialize by- Throws:
NumberFormatException- ifstris not a valid representation of a number
-
-
Method Detail
-
valueOf
public static DD valueOf(String str) throws NumberFormatException
Converts the string argument to a DoubleDouble number.- Parameters:
str- a string containing a representation of a numeric value- Returns:
- the extended precision version of the value
- Throws:
NumberFormatException- ifsis not a valid representation of a number
-
valueOf
public static DD valueOf(double x)
Converts thedoubleargument to a DoubleDouble number.- Parameters:
x- a numeric value- Returns:
- the extended precision version of the value
-
copy
public static DD copy(DD dd)
Creates a new DoubleDouble with the value of the argument.- Parameters:
dd- the DoubleDouble value to copy- Returns:
- a copy of the input value
-
clone
public Object clone()
Creates and returns a copy of this value.
-
setValue
public DD setValue(DD value)
Set the value for the DD object. This method supports the mutating operations concept described in the class documentation (see above).- Parameters:
value- a DD instance supplying an extended-precision value.- Returns:
- a self-reference to the DD instance.
-
setValue
public DD setValue(double value)
Set the value for the DD object. This method supports the mutating operations concept described in the class documentation (see above).- Parameters:
value- a floating point value to be stored in the instance.- Returns:
- a self-reference to the DD instance.
-
add
public final DD add(DD y)
Returns a new DoubleDouble whose value is(this + y).- Parameters:
y- the addend- Returns:
(this + y)
-
add
public final DD add(double y)
Returns a new DoubleDouble whose value is(this + y).- Parameters:
y- the addend- Returns:
(this + y)
-
selfAdd
public final DD selfAdd(DD y)
Adds the argument to the value ofthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the addend- Returns:
- this object, increased by y
-
selfAdd
public final DD selfAdd(double y)
Adds the argument to the value ofthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the addend- Returns:
- this object, increased by y
-
subtract
public final DD subtract(DD y)
Computes a new DoubleDouble object whose value is(this - y).- Parameters:
y- the subtrahend- Returns:
(this - y)
-
subtract
public final DD subtract(double y)
Computes a new DoubleDouble object whose value is(this - y).- Parameters:
y- the subtrahend- Returns:
(this - y)
-
selfSubtract
public final DD selfSubtract(DD y)
Subtracts the argument from the value ofthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the addend- Returns:
- this object, decreased by y
-
selfSubtract
public final DD selfSubtract(double y)
Subtracts the argument from the value ofthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the addend- Returns:
- this object, decreased by y
-
negate
public final DD negate()
Returns a new DoubleDouble whose value is-this.- Returns:
-this
-
multiply
public final DD multiply(DD y)
Returns a new DoubleDouble whose value is(this * y).- Parameters:
y- the multiplicand- Returns:
(this * y)
-
multiply
public final DD multiply(double y)
Returns a new DoubleDouble whose value is(this * y).- Parameters:
y- the multiplicand- Returns:
(this * y)
-
selfMultiply
public final DD selfMultiply(DD y)
Multiplies this object by the argument, returningthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the value to multiply by- Returns:
- this object, multiplied by y
-
selfMultiply
public final DD selfMultiply(double y)
Multiplies this object by the argument, returningthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the value to multiply by- Returns:
- this object, multiplied by y
-
divide
public final DD divide(DD y)
Computes a new DoubleDouble whose value is(this / y).- Parameters:
y- the divisor- Returns:
- a new object with the value
(this / y)
-
divide
public final DD divide(double y)
Computes a new DoubleDouble whose value is(this / y).- Parameters:
y- the divisor- Returns:
- a new object with the value
(this / y)
-
selfDivide
public final DD selfDivide(DD y)
Divides this object by the argument, returningthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the value to divide by- Returns:
- this object, divided by y
-
selfDivide
public final DD selfDivide(double y)
Divides this object by the argument, returningthis. To prevent altering constants, this method must only be used on values known to be newly created.- Parameters:
y- the value to divide by- Returns:
- this object, divided by y
-
reciprocal
public final DD reciprocal()
Returns a DoubleDouble whose value is1 / this.- Returns:
- the reciprocal of this value
-
floor
public DD floor()
Returns the largest (closest to positive infinity) value that is not greater than the argument and is equal to a mathematical integer. Special cases:- If this value is NaN, returns NaN.
- Returns:
- the largest (closest to positive infinity) value that is not greater than the argument and is equal to a mathematical integer.
-
ceil
public DD ceil()
Returns the smallest (closest to negative infinity) value that is not less than the argument and is equal to a mathematical integer. Special cases:- If this value is NaN, returns NaN.
- Returns:
- the smallest (closest to negative infinity) value that is not less than the argument and is equal to a mathematical integer.
-
signum
public int signum()
Returns an integer indicating the sign of this value.- if this value is > 0, returns 1
- if this value is < 0, returns -1
- if this value is = 0, returns 0
- if this value is NaN, returns 0
- Returns:
- an integer indicating the sign of this value
-
rint
public DD rint()
Rounds this value to the nearest integer. The value is rounded to an integer by adding 1/2 and taking the floor of the result. Special cases:- If this value is NaN, returns NaN.
- Returns:
- this value rounded to the nearest integer
-
trunc
public DD trunc()
Returns the integer which is largest in absolute value and not further from zero than this value. Special cases:- If this value is NaN, returns NaN.
- Returns:
- the integer which is largest in absolute value and not further from zero than this value
-
abs
public DD abs()
Returns the absolute value of this value. Special cases:- If this value is NaN, it is returned.
- Returns:
- the absolute value of this value
-
sqr
public DD sqr()
Computes the square of this value.- Returns:
- the square of this value.
-
selfSqr
public DD selfSqr()
Squares this object. To prevent altering constants, this method must only be used on values known to be newly created.- Returns:
- the square of this value.
-
sqr
public static DD sqr(double x)
Computes the square of the specified value.- Parameters:
x- the value to square- Returns:
- the square of this value.
-
sqrt
public DD sqrt()
Computes the positive square root of this value. If the number is NaN or negative, NaN is returned.- Returns:
- the positive square root of this number. If the argument is NaN or less than zero, the result is NaN.
-
sqrt
public static DD sqrt(double x)
Returns the square root of the specified value as a double-precision instance.- Parameters:
x- a valid, finite floating-point value.- Returns:
- a valid instance.
-
pow
public DD pow(int exp)
Computes the value of this number raised to an integral power. Follows semantics of Java Math.pow as closely as possible.- Parameters:
exp- the integer exponent- Returns:
- x raised to the integral power exp
-
min
public DD min(DD x)
Computes the minimum of this and another DD number.- Parameters:
x- a DD number- Returns:
- the minimum of the two numbers
-
max
public DD max(DD x)
Computes the maximum of this and another DD number.- Parameters:
x- a DD number- Returns:
- the maximum of the two numbers
-
doubleValue
public double doubleValue()
Converts this value to the nearest double-precision number.- Returns:
- the nearest double-precision number to this value
-
intValue
public int intValue()
Converts this value to the nearest integer.- Returns:
- the nearest integer to this value
-
isZero
public boolean isZero()
Tests whether this value is equal to 0.- Returns:
- true if this value is equal to 0
-
isNegative
public boolean isNegative()
Tests whether this value is less than 0.- Returns:
- true if this value is less than 0
-
isPositive
public boolean isPositive()
Tests whether this value is greater than 0.- Returns:
- true if this value is greater than 0
-
isNaN
public boolean isNaN()
Tests whether this value is NaN.- Returns:
- true if this value is NaN
-
equals
public boolean equals(DD y)
Tests whether this value is equal to anotherDoubleDoublevalue.- Parameters:
y- a DoubleDouble value- Returns:
- true if this value = y
-
gt
public boolean gt(DD y)
Tests whether this value is greater than anotherDoubleDoublevalue.- Parameters:
y- a DoubleDouble value- Returns:
- true if this value > y
-
ge
public boolean ge(DD y)
Tests whether this value is greater than or equals to anotherDoubleDoublevalue.- Parameters:
y- a DoubleDouble value- Returns:
- true if this value ≥ y
-
lt
public boolean lt(DD y)
Tests whether this value is less than anotherDoubleDoublevalue.- Parameters:
y- a DoubleDouble value- Returns:
- true if this value < y
-
le
public boolean le(DD y)
Tests whether this value is less than or equal to anotherDoubleDoublevalue.- Parameters:
y- a DoubleDouble value- Returns:
- true if this value ≤ y
-
compareTo
public int compareTo(DD other)
Compares two DoubleDouble objects numerically.- Specified by:
compareToin interfaceComparable<DD>- Parameters:
other- a valid instance of a DD object- Returns:
- -1,0 or 1 depending on whether this value is less than, equal to
or greater than the value of
o
-
dump
public String dump()
Dumps the components of this number to a string.- Returns:
- a string showing the components of the number
-
toString
public String toString()
Returns a string representation of this number, in either standard or scientific notation. If the magnitude of the number is in the range [ 10-3, 108 ] standard notation will be used. Otherwise, scientific notation will be used.
-
toStandardNotation
public String toStandardNotation()
Returns the string representation of this value in standard notation.- Returns:
- the string representation in standard notation
-
toSciNotation
public String toSciNotation()
Returns the string representation of this value in scientific notation.- Returns:
- the string representation in scientific notation
-
parse
public static DD parse(String str) throws NumberFormatException
Converts a string representation of a real number into a DoubleDouble value. The format accepted is similar to the standard Java real number syntax. It is defined by the following regular expression:[
+|-] {digit} [.{digit} ] [ (e|E) [+|-] {digit}+- Parameters:
str- the string to parse- Returns:
- the value of the parsed number
- Throws:
NumberFormatException- ifstris not a valid representation of a number
-
-