Table of Contents

Struct DD

Namespace
NetTopologySuite.Mathematics
Assembly
NetTopologySuite.dll

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 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.

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
public struct DD : IComparable, IComparable<DD>
Implements
Inherited Members

Constructors

DD(DD)

Creates a DD with a value equal to the argument

public DD(DD dd)

Parameters

dd DD

The initial value

DD(double)

Creates a new DD with value x.

public DD(double x)

Parameters

x double

The initial value

DD(double, double)

Creates a new DD with value (hi, lo).

public DD(double hi, double lo)

Parameters

hi double

The high order component

lo double

The low order component

DD(string)

Creates a new DD with value equal to the argument.

public DD(string str)

Parameters

str string

The value to initialize by

Exceptions

FormatException

if str is not a valid representation of a number

Fields

E

The value nearest to the constant e (the natural logarithm base).

public static readonly DD E

Field Value

DD

Epsilon

The smallest representable relative difference between two DD values

public static readonly double Epsilon

Field Value

double

NaN

A value representing the result of an operation which does not return a valid number.

public static readonly DD NaN

Field Value

DD

PI

The value nearest to the constant Pi.

public static readonly DD PI

Field Value

DD

PiHalf

The value nearest to the constant Pi / 2.

public static readonly DD PiHalf

Field Value

DD

TwoPi

The value nearest to the constant 2 * Pi.

public static readonly DD TwoPi

Field Value

DD

Properties

IsNegative

Gets a value indicating whether this object is negative or not

public bool IsNegative { get; }

Property Value

bool

IsZero

Gets a value indicating whether this object is zero (0) or not

public bool IsZero { get; }

Property Value

bool

Methods

Abs()

Returns the absolute value of this value.

Special cases:
  • if this value is NaN, it is returned.
public DD Abs()

Returns

DD

The absolute value of this value

Ceiling()

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.
public DD Ceiling()

Returns

DD

The smallest (closest to negative infinity) value that is not less than the argument and is equal to a mathematical integer.

Clone()

Creates and returns a copy of this value.

public object Clone()

Returns

object

A copy of this value

CompareTo(DD)

Compares two DoubleDouble objects numerically.

public int CompareTo(DD other)

Parameters

other DD

An other DoubleDouble value

Returns

int

-1,0 or 1 depending on whether this value is less than, equal to or greater than the value of other

CompareTo(object)

public int CompareTo(object o)

Parameters

o object

Returns

int

Copy(DD)

Creates a new DD with the value of the argument.

public static DD Copy(DD dd)

Parameters

dd DD

The value to copy

Returns

DD

A copy of dd

Determinant(DD, DD, DD, DD)

Computes the determinant of the 2x2 matrix with the given entries.

public static DD Determinant(DD x1, DD y1, DD x2, DD y2)

Parameters

x1 DD

A matrix entry

y1 DD

A matrix entry

x2 DD

A matrix entry

y2 DD

A matrix entry

Returns

DD

The determinant of the matrix of values

Determinant(double, double, double, double)

Computes the determinant of the 2x2 matrix with the given entries.

public static DD Determinant(double x1, double y1, double x2, double y2)

Parameters

x1 double

A matrix entry

y1 double

A matrix entry

x2 double

A matrix entry

y2 double

A matrix entry

Returns

DD

The determinant of the matrix of values

Dump()

Dumps the components of this number to a string.

public string Dump()

Returns

string

A string showing the components of the number

Equals(DD)

Tests whether this value is equal to another DoubleDouble value.

public bool Equals(DD y)

Parameters

y DD

A DoubleDouble value

Returns

bool

true if this value == y.

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

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.
public DD Floor()

Returns

DD

The largest (closest to positive infinity) value that is not greater than the argument and is equal to a mathematical integer.

GetHashCode()

public override int GetHashCode()

Returns

int

GreaterOrEqualThan(DD)

Tests whether this value is greater than or equals to another DoubleDouble value.

public bool GreaterOrEqualThan(DD y)

Parameters

y DD

A DoubleDouble value

Returns

bool

true if this value >= y.

GreaterThan(DD)

Tests whether this value is greater than another DoubleDouble value.

public bool GreaterThan(DD y)

Parameters

y DD

A DoubleDouble value

Returns

bool

true if this value > y.

IsInfinity(DD)

Checks if value is infinity.

public static bool IsInfinity(DD value)

Parameters

value DD

A DoubleDouble value

Returns

bool

true if value is infinity.

IsNaN(DD)

Gets a value indicating whether this object is positive or not

public static bool IsNaN(DD value)

Parameters

value DD

Returns

bool

IsPositive()

Gets a value indicating whether this object is positive or not

public bool IsPositive()

Returns

bool

LessOrEqualThan(DD)

Tests whether this value is less than or equal to another DoubleDouble value.

public bool LessOrEqualThan(DD y)

Parameters

y DD

A DoubleDouble

Returns

bool

true if this value is <= y

LessThan(DD)

Tests whether this value is less than another DoubleDouble value.

public bool LessThan(DD y)

Parameters

y DD

A DoubleDouble value

Returns

bool

true if this value is < y

Max(DD)

Computes the maximum of this and another DD number.

public DD Max(DD x)

Parameters

x DD

A DD number

Returns

DD

The maximum of the two numbers

Min(DD)

Computes the minimum of this and another DD number.

public DD Min(DD x)

Parameters

x DD

A DD number

Returns

DD

The minimum of the two numbers

Parse(string)

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}+
public static DD Parse(string str)

Parameters

str string

The string to parse

Returns

DD

The value of the parsed number

Exceptions

FormatException

Thrown if str is not a valid representation of a number

Pow(int)

Computes the value of this number raised to an integral power. Follows semantics of .Net Math.Pow as closely as possible.

public DD Pow(int exp)

Parameters

exp int

The integer exponent

Returns

DD

x raised to the integral power exp

Reciprocal()

Returns a DD whose value is 1 / this.

public DD Reciprocal()

Returns

DD

The reciprocal of this value

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.
public DD Rint()

Returns

DD

This value rounded to the nearest integer

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
public int Signum()

Returns

int

An integer indicating the sign of this value

Sqr()

Computes the square of this value.

public DD Sqr()

Returns

DD

The square of this value

Sqr(double)

Computes the square of this value.

public static DD Sqr(double x)

Parameters

x double

Returns

DD

The square of this value.

Sqrt()

Computes the positive square root of this value.

If the number is NaN or negative, NaN is returned.
public DD Sqrt()

Returns

DD

If this is NaN or less than zero, the result is NaN.

Sqrt(double)

Computes the positive square root of a DoubleDouble value.

If the number is NaN or negative, NaN is returned.
public static DD Sqrt(double x)

Parameters

x double

A numeric value

Returns

DD

the positive square root of this number.

If the argument is NaN or less than zero, the result is NaN.

ToDoubleValue()

Converts this value to the nearest double number.

public double ToDoubleValue()

Returns

double

The nearest double value

ToIntValue()

Converts this value to the nearest int value.

public int ToIntValue()

Returns

int

The nearest int value

ToSciNotation()

Returns the string representation of this value in scientific notation.

public string ToSciNotation()

Returns

string

The string representation in scientific notation

ToStandardNotation()

Returns the string representation of this value in standard notation.

public string ToStandardNotation()

Returns

string

The string representation in standard notation

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.

public override string ToString()

Returns

string

A string representation of this number

Truncate()

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.
public DD Truncate()

Returns

DD

The integer which is largest in absolute value and not further from zero than this value

ValueOf(double)

Converts the double argument to a DoubleDouble number.

public static DD ValueOf(double x)

Parameters

x double

A numeric value

Returns

DD

The extended precision version of the value

ValueOf(string)

Converts the string argument to a DoubleDouble number.

public static DD ValueOf(string str)

Parameters

str string

A string containing a representation of a numeric value

Returns

DD

The extended precision version of the value

Exceptions

FormatException

Thrown if str is not a valid representation of a number

Operators

operator +(DD, DD)

Returns the sum of lhs and rhs.

public static DD operator +(DD lhs, DD rhs)

Parameters

lhs DD

The left hand side

rhs DD

The right hand side

Returns

DD

The sum of lhs and rhs

operator +(DD, double)

Returns the sum of lhs and rhs.

public static DD operator +(DD lhs, double rhs)

Parameters

lhs DD

The left hand side

rhs double

The right hand side

Returns

DD

The sum of lhs and rhs

operator /(DD, DD)

Divides lhs by rhs.

public static DD operator /(DD lhs, DD rhs)

Parameters

lhs DD

A DoubleDouble numerator.

rhs DD

A DoubleDouble divisor.

Returns

DD

The result of the division.

operator /(DD, double)

Divides lhs by rhs.

public static DD operator /(DD lhs, double rhs)

Parameters

lhs DD

A DoubleDouble numerator.

rhs double

A double divisor.

Returns

DD

The result of the division.

operator ==(DD, DD)

Equality operator for DoubleDouble values

public static bool operator ==(DD lhs, DD rhs)

Parameters

lhs DD

A DoubleDouble value

rhs DD

A DoubleDouble value

Returns

bool

true if lhs == rhs.

explicit operator DD(string)

Operator to parse a DoubleDouble from a string

public static explicit operator DD(string val)

Parameters

val string

The DoubleDouble string

Returns

DD

implicit operator DD(double)

Operator to convert the double value to a DoubleDouble value.

public static implicit operator DD(double val)

Parameters

val double

The DoubleDouble string

Returns

DD

operator !=(DD, DD)

Inequality operator for DoubleDouble values

public static bool operator !=(DD rhs, DD lhs)

Parameters

rhs DD

A DoubleDouble value

lhs DD

A DoubleDouble value

Returns

bool

true if lhs != rhs.

operator *(DD, DD)

Multiplies lhs by rhs.

public static DD operator *(DD lhs, DD rhs)

Parameters

lhs DD

A DoubleDouble value.

rhs DD

A DoubleDouble value.

Returns

DD

The result of the multiplication.

operator *(DD, double)

Multiplies lhs by rhs.

public static DD operator *(DD lhs, double rhs)

Parameters

lhs DD

A DoubleDouble value.

rhs double

A double value.

Returns

DD

The result of the multiplication.

operator -(DD, DD)

Returns the difference of lhs and rhs.

public static DD operator -(DD lhs, DD rhs)

Parameters

lhs DD

The left hand side

rhs DD

The right hand side

Returns

DD

The difference of lhs and rhs

operator -(DD, double)

Returns the difference of lhs and rhs.

public static DD operator -(DD lhs, double rhs)

Parameters

lhs DD

The left hand side

rhs double

The right hand side

Returns

DD

The difference of lhs and rhs

operator -(DD)

Subtracts the argument from the value of this.

public static DD operator -(DD val)

Parameters

val DD

The subtrahend

Returns

DD

The result of this - y