Package org.apache.commons.numbers.core
Class DDMath
java.lang.Object
org.apache.commons.numbers.core.DDMath
Computes extended precision floating-point operations.
This class supplements the arithmetic operations in the DD
class providing
greater accuracy at the cost of performance.
- Since:
- 1.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Mutable double-double number used for working. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
0.5.private static final double
The limit for safe multiplication ofx*y
, assuming values above 1. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static double
add3
(double a0, double a1, double a2, double b0, double b1, double b2, DDMath.MDD s12) Compute the sum of(a0,a1,a2)
and(b0,b1,b2))
.private static double
add3
(double a0, double a1, double a2, double b, DDMath.MDD s12) Compute the sum of(a0,a1,a2)
andb
.private static DD
computePowScaled
(long b, double x, double xx, int n, long[] exp) Compute the numberx
(non-zero finite) raised to the powern
.private static DD
inverse3
(double y, double yy, double yyy) Compute the inverse of(y, yy, yyy)
.private static double
multiply3
(double a0, double a1, double a2, double b, DDMath.MDD s12) Compute the multiplication product of(a0,a1,a2)
andb
.private static double
norm3
(double s0, double s1, double s2, double s3, DDMath.MDD s12) Normalize (s0, s1, s2, s3) to (s0, s1, s2).static DD
Compute the numberx
raised to the powern
.
-
Field Details
-
HALF
private static final double HALF0.5.- See Also:
-
SAFE_MULTIPLY
private static final double SAFE_MULTIPLYThe limit for safe multiplication ofx*y
, assuming values above 1. Used to maintain positive values during the power computation.- See Also:
-
-
Constructor Details
-
DDMath
private DDMath()No instances.
-
-
Method Details
-
pow
Compute the numberx
raised to the powern
.The value is returned as fractional
f
and integral2^exp
components.(x+xx)^n = (f+ff) * 2^exp
The combined fractional part (f, ff) is in the range
[0.5, 1)
.Special cases:
- If
(x, xx)
is zero the high part of the fractional part is computed usingMath.pow(x, n)
and the exponent is 0. - If
n = 0
the fractional part is 0.5 and the exponent is 1. - If
(x, xx)
is an exact power of 2 the fractional part is 0.5 and the exponent is the power of 2 minus 1. - If the result high-part is an exact power of 2 and the low-part has an opposite
signed non-zero magnitude then the fraction high-part
f
will be+/-1
such that the double-double number is in the range[0.5, 1)
. - If the argument is not finite then a fractional representation is not possible. In this case the fraction and the scale factor is undefined.
The computed result is within 1 eps of the exact result where eps is 2-106.
The performance is approximately 4-fold slower than
DD.pow(int, long[])
.- Parameters:
x
- Number.n
- Power.exp
- Result power of two scale factor (integral exponent).- Returns:
- Fraction part.
- See Also:
- If
-
computePowScaled
Compute the numberx
(non-zero finite) raised to the powern
.Performs the computation using triple-double precision. If the input power is negative the result is computed using the absolute value of
n
and then inverted by dividing into 1.- Parameters:
b
- Integral component 2^b of x.x
- Fractional high part of x.xx
- Fractional low part of x.n
- Power (in [2, 2^31]).exp
- Result power of two scale factor (integral exponent).- Returns:
- Fraction part.
-
norm3
Normalize (s0, s1, s2, s3) to (s0, s1, s2).- Parameters:
s0
- High part of s.s1
- Second part of s.s2
- Third part of s.s3
- Fourth part of s.s12
- Output parts (s1, s2)- Returns:
- s0
-
inverse3
Compute the inverse of(y, yy, yyy)
. Ify = 0
the result is undefined.This is special routine used in
invalid reference
#pow(int, long[])
- Parameters:
y
- First part of y.yy
- Second part of y.yyy
- Third part of y.- Returns:
- the inverse
-
multiply3
Compute the multiplication product of(a0,a1,a2)
andb
.- Parameters:
a0
- High part of a.a1
- Second part of a.a2
- Third part of a.b
- Factor.s12
- Output parts (s1, s2)- Returns:
- s0
-
add3
Compute the sum of(a0,a1,a2)
andb
.- Parameters:
a0
- High part of a.a1
- Second part of a.a2
- Third part of a.b
- Addend.s12
- Output parts (s1, s2)- Returns:
- s0
-
add3
private static double add3(double a0, double a1, double a2, double b0, double b1, double b2, DDMath.MDD s12) Compute the sum of(a0,a1,a2)
and(b0,b1,b2))
. It is assumed the absolute magnitudes of a and b are equal and the sign of a and b are opposite.- Parameters:
a0
- High part of a.a1
- Second part of a.a2
- Third part of a.b0
- High part of b.b1
- Second part of b.b2
- Third part of b.s12
- Output parts (s1, s2)- Returns:
- s0
-