Top Description Fields Constructors Methods
org.apache.commons.math3.dfp

public Class DfpMath

extends Object
Class Inheritance

Mathematical routines for use with Dfp. The constants are defined in DfpField
Since
2.2

Field Summary

Modifier and TypeField and Description
private static final String
POW_TRAP

Name for traps triggered by pow.

Constructor Summary

AccessConstructor and Description
private
DfpMath()

Private Constructor.

Method Summary

Modifier and TypeMethod and Description
public static Dfp

Returns:

acos(a)
acos
(Dfp
number from which arc-cosine is desired
a
)

computes the arc-cosine of the argument.

public static Dfp

Returns:

asin(a)
asin
(final Dfp
number from which arc-sine is desired
a
)

computes the arc-sine of the argument.

public static Dfp

Returns:

atan(a)
atan
(final Dfp
number from which arc-tangent is desired
a
)

computes the arc tangent of the argument Uses the typical taylor series but may reduce arguments using the following identity tan(x+y) = (tan(x) + tan(y)) / (1 - tan(x)*tan(y)) since tan(PI/8) = sqrt(2)-1, atan(x) = atan( (x - sqrt(2) + 1) / (1+x*sqrt(2) - x) + PI/8.0

protected static Dfp

Returns:

atan(a)
atanInternal
(final Dfp
number from which arc-tangent is desired
a
)

computes the arc-tangent of the argument.

public static Dfp

Returns:

cos(a)
cos
(Dfp
number from which cosine is desired
a
)

computes the cosine of the argument.

protected static Dfp

Returns:

cos(a)
cosInternal
(Dfp[]
number from which cosine is desired, in split form
a
)

Computes cos(a) Used when 0 < a < pi/4.

public static Dfp

Returns:

ea
exp
(final Dfp
power at which e should be raised
a
)

Computes e to the given power.

protected static Dfp

Returns:

ea
expInternal
(final Dfp
power at which e should be raised
a
)

Computes e to the given power.

public static Dfp

Returns:

log(a)
log
(Dfp
number from which logarithm is requested
a
)

Returns the natural logarithm of a.

protected static Dfp[]

Returns:

log(a)
logInternal
(final Dfp[]
number from which logarithm is requested, in split form
a
)

Computes the natural log of a number between 0 and 2.

public static Dfp

Returns:

basea
pow
(Dfp
number to raise
base
,
int
power
a
)

Raises base to the power a by successive squaring.

public static Dfp

Returns:

xy
pow
(Dfp
base to be raised
x
,
final Dfp
power to which base should be raised
y
)

Computes x to the y power.

public static Dfp

Returns:

sin(a)
sin
(final Dfp
number from which sine is desired
a
)

computes the sine of the argument.

protected static Dfp

Returns:

sin(a)
sinInternal
(Dfp[]
number from which sine is desired, in split form
a
)

Computes sin(a) Used when 0 < a < pi/4.

protected static Dfp[]

Returns:

an array of two Dfp which sum is a
split
(final DfpField
field to which the Dfp must belong
field
,
final String
string representation to split
a
)

Breaks a string representation up into two dfp's.

protected static Dfp[]

Returns:

two elements array containing the split number
split
(final Dfp
number to split
a
)

Splits a Dfp into 2 Dfp's such that their sum is equal to the input Dfp.

protected static Dfp[]

Returns:

a / b, in split form
splitDiv
(final Dfp[]
dividend, in split form
a
,
final Dfp[]
divisor, in split form
b
)

Divide two numbers that are split in to two pieces that are meant to be added together.

protected static Dfp[]

Returns:

a × b, in split form
splitMult
(final Dfp[]
first factor of the multiplication, in split form
a
,
final Dfp[]
second factor of the multiplication, in split form
b
)

Multiply two numbers that are split in to two pieces that are meant to be added together.

protected static Dfp

Returns:

basea
splitPow
(final Dfp[]
number to raise
base
,
int
power
a
)

Raise a split base to the a power.

public static Dfp

Returns:

tan(a)
tan
(final Dfp
number from which tangent is desired
a
)

computes the tangent of the argument.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

POW_TRAPback to summary
private static final String POW_TRAP

Name for traps triggered by pow.

Constructor Detail

DfpMathback to summary
private DfpMath()

Private Constructor.

Method Detail

acosback to summary
public static Dfp acos(Dfp a)

computes the arc-cosine of the argument.

Parameters
a:Dfp

number from which arc-cosine is desired

Returns:Dfp

acos(a)

asinback to summary
public static Dfp asin(final Dfp a)

computes the arc-sine of the argument.

Parameters
a:Dfp

number from which arc-sine is desired

Returns:Dfp

asin(a)

atanback to summary
public static Dfp atan(final Dfp a)

computes the arc tangent of the argument Uses the typical taylor series but may reduce arguments using the following identity tan(x+y) = (tan(x) + tan(y)) / (1 - tan(x)*tan(y)) since tan(PI/8) = sqrt(2)-1, atan(x) = atan( (x - sqrt(2) + 1) / (1+x*sqrt(2) - x) + PI/8.0

Parameters
a:Dfp

number from which arc-tangent is desired

Returns:Dfp

atan(a)

atanInternalback to summary
protected static Dfp atanInternal(final Dfp a)

computes the arc-tangent of the argument.

Parameters
a:Dfp

number from which arc-tangent is desired

Returns:Dfp

atan(a)

cosback to summary
public static Dfp cos(Dfp a)

computes the cosine of the argument.

Parameters
a:Dfp

number from which cosine is desired

Returns:Dfp

cos(a)

cosInternalback to summary
protected static Dfp cosInternal(Dfp[] a)

Computes cos(a) Used when 0 < a < pi/4. Uses the classic Taylor series for cosine. 1 - x**2/2! + x**4/4! ...

Parameters
a:Dfp[]

number from which cosine is desired, in split form

Returns:Dfp

cos(a)

expback to summary
public static Dfp exp(final Dfp a)

Computes e to the given power. a is broken into two parts, such that a = n+m where n is an integer. We use pow() to compute en and a Taylor series to compute em. We return e*n × em

Parameters
a:Dfp

power at which e should be raised

Returns:Dfp

ea

expInternalback to summary
protected static Dfp expInternal(final Dfp a)

Computes e to the given power. Where -1 < a < 1. Use the classic Taylor series. 1 + x**2/2! + x**3/3! + x**4/4! ...

Parameters
a:Dfp

power at which e should be raised

Returns:Dfp

ea

logback to summary
public static Dfp log(Dfp a)

Returns the natural logarithm of a. a is first split into three parts such that a = (10000^h)(2^j)k. ln(a) is computed by ln(a) = ln(5)*h + ln(2)*(h+j) + ln(k) k is in the range 2/3 < k <4/3 and is passed on to a series expansion.

Parameters
a:Dfp

number from which logarithm is requested

Returns:Dfp

log(a)

logInternalback to summary
protected static Dfp[] logInternal(final Dfp[] a)

Computes the natural log of a number between 0 and 2. Let f(x) = ln(x), We know that f'(x) = 1/x, thus from Taylor's theorum we have: ----- n+1 n f(x) = \ (-1) (x - 1) / ---------------- for 1 <= n <= infinity ----- n or 2 3 4 (x-1) (x-1) (x-1) ln(x) = (x-1) - ----- + ------ - ------ + ... 2 3 4 alternatively, 2 3 4 x x x ln(x+1) = x - - + - - - + ... 2 3 4 This series can be used to compute ln(x), but it converges too slowly. If we substitute -x for x above, we get 2 3 4 x x x ln(1-x) = -x - - - - - - + ... 2 3 4 Note that all terms are now negative. Because the even powered ones absorbed the sign. Now, subtract the series above from the previous one to get ln(x+1) - ln(1-x). Note the even terms cancel out leaving only the odd ones 3 5 7 2x 2x 2x ln(x+1) - ln(x-1) = 2x + --- + --- + ---- + ... 3 5 7 By the property of logarithms that ln(a) - ln(b) = ln (a/b) we have: 3 5 7 x+1 / x x x \ ln ----- = 2 * | x + ---- + ---- + ---- + ... | x-1 \ 3 5 7 / But now we want to find ln(a), so we need to find the value of x such that a = (x+1)/(x-1). This is easily solved to find that x = (a-1)/(a+1).

Parameters
a:Dfp[]

number from which logarithm is requested, in split form

Returns:Dfp[]

log(a)

powback to summary
public static Dfp pow(Dfp base, int a)

Raises base to the power a by successive squaring.

Parameters
base:Dfp

number to raise

a:int

power

Returns:Dfp

basea

powback to summary
public static Dfp pow(Dfp x, final Dfp y)

Computes x to the y power.

Uses the following method:

  1. Set u = rint(y), v = y-u
  2. Compute a = v * ln(x)
  3. Compute b = rint( a/ln(2) )
  4. Compute c = a - b*ln(2)
  5. xy = xu * 2b * ec
if |y| > 1e8, then we compute by exp(y*ln(x))

Special Cases

  • if y is 0.0 or -0.0 then result is 1.0
  • if y is 1.0 then result is x
  • if y is NaN then result is NaN
  • if x is NaN and y is not zero then result is NaN
  • if |x| > 1.0 and y is +Infinity then result is +Infinity
  • if |x| < 1.0 and y is -Infinity then result is +Infinity
  • if |x| > 1.0 and y is -Infinity then result is +0
  • if |x| < 1.0 and y is +Infinity then result is +0
  • if |x| = 1.0 and y is +/-Infinity then result is NaN
  • if x = +0 and y > 0 then result is +0
  • if x = +Inf and y < 0 then result is +0
  • if x = +0 and y < 0 then result is +Inf
  • if x = +Inf and y > 0 then result is +Inf
  • if x = -0 and y > 0, finite, not odd integer then result is +0
  • if x = -0 and y < 0, finite, and odd integer then result is -Inf
  • if x = -Inf and y > 0, finite, and odd integer then result is -Inf
  • if x = -0 and y < 0, not finite odd integer then result is +Inf
  • if x = -Inf and y > 0, not finite odd integer then result is +Inf
  • if x < 0 and y > 0, finite, and odd integer then result is -(|x|y)
  • if x < 0 and y > 0, finite, and not integer then result is NaN
Parameters
x:Dfp

base to be raised

y:Dfp

power to which base should be raised

Returns:Dfp

xy

sinback to summary
public static Dfp sin(final Dfp a)

computes the sine of the argument.

Parameters
a:Dfp

number from which sine is desired

Returns:Dfp

sin(a)

sinInternalback to summary
protected static Dfp sinInternal(Dfp[] a)

Computes sin(a) Used when 0 < a < pi/4. Uses the classic Taylor series. x - x**3/3! + x**5/5! ...

Parameters
a:Dfp[]

number from which sine is desired, in split form

Returns:Dfp

sin(a)

splitback to summary
protected static Dfp[] split(final DfpField field, final String a)

Breaks a string representation up into two dfp's.

The two dfp are such that the sum of them is equivalent to the input string, but has higher precision than using a single dfp. This is useful for improving accuracy of exponentiation and critical multiplies.

Parameters
field:DfpField

field to which the Dfp must belong

a:String

string representation to split

Returns:Dfp[]

an array of two Dfp which sum is a

splitback to summary
protected static Dfp[] split(final Dfp a)

Splits a Dfp into 2 Dfp's such that their sum is equal to the input Dfp.

Parameters
a:Dfp

number to split

Returns:Dfp[]

two elements array containing the split number

splitDivback to summary
protected static Dfp[] splitDiv(final Dfp[] a, final Dfp[] b)

Divide two numbers that are split in to two pieces that are meant to be added together. Inverse of split multiply above: (a+b) / (c+d) = (a/c) + ( (bc-ad)/(c**2+cd) )

Parameters
a:Dfp[]

dividend, in split form

b:Dfp[]

divisor, in split form

Returns:Dfp[]

a / b, in split form

splitMultback to summary
protected static Dfp[] splitMult(final Dfp[] a, final Dfp[] b)

Multiply two numbers that are split in to two pieces that are meant to be added together. Use binomial multiplication so ab = a0 b0 + a0 b1 + a1 b0 + a1 b1 Store the first term in result0, the rest in result1

Parameters
a:Dfp[]

first factor of the multiplication, in split form

b:Dfp[]

second factor of the multiplication, in split form

Returns:Dfp[]

a × b, in split form

splitPowback to summary
protected static Dfp splitPow(final Dfp[] base, int a)

Raise a split base to the a power.

Parameters
base:Dfp[]

number to raise

a:int

power

Returns:Dfp

basea

tanback to summary
public static Dfp tan(final Dfp a)

computes the tangent of the argument.

Parameters
a:Dfp

number from which tangent is desired

Returns:Dfp

tan(a)