Class DDMath

java.lang.Object
org.apache.commons.numbers.core.DDMath

public final class DDMath extends Object
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 Classes
    Modifier and Type
    Class
    Description
    private static final class 
    Mutable double-double number used for working.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final double
    0.5.
    private static final double
    The limit for safe multiplication of x*y, assuming values above 1.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    No instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    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)).
    private static double
    add3(double a0, double a1, double a2, double b, DDMath.MDD s12)
    Compute the sum of (a0,a1,a2) and b.
    private static DD
    computePowScaled(long b, double x, double xx, int n, long[] exp)
    Compute the number x (non-zero finite) raised to the power n.
    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) and b.
    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
    pow(DD x, int n, long[] exp)
    Compute the number x raised to the power n.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HALF

      private static final double HALF
      0.5.
      See Also:
    • SAFE_MULTIPLY

      private static final double SAFE_MULTIPLY
      The limit for safe multiplication of x*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

      public static DD pow(DD x, int n, long[] exp)
      Compute the number x raised to the power n.

      The value is returned as fractional f and integral 2^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 using Math.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:
    • computePowScaled

      private static DD computePowScaled(long b, double x, double xx, int n, long[] exp)
      Compute the number x (non-zero finite) raised to the power n.

      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

      private static double norm3(double s0, double s1, double s2, double s3, DDMath.MDD s12)
      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

      private static DD inverse3(double y, double yy, double yyy)
      Compute the inverse of (y, yy, yyy). If y = 0 the result is undefined.

      This is special routine used in

      invalid reference
      #pow(int, long[])
      to invert the triple precision result.
      Parameters:
      y - First part of y.
      yy - Second part of y.
      yyy - Third part of y.
      Returns:
      the inverse
    • multiply3

      private static double multiply3(double a0, double a1, double a2, double b, DDMath.MDD s12)
      Compute the multiplication product of (a0,a1,a2) and b.
      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

      private static double add3(double a0, double a1, double a2, double b, DDMath.MDD s12)
      Compute the sum of (a0,a1,a2) and b.
      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