Package org.apache.commons.math3.dfp
Class DfpField
java.lang.Object
org.apache.commons.math3.dfp.DfpField
Field for Decimal floating point instances.
- Since:
- 2.2
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final DfpADfpwith value e.private final Dfp[]A two elementsDfparray with value e split in two pieces.private static StringHigh precision string representation of e.static final intIEEE 854-1987 flag for division by zero.static final intIEEE 854-1987 flag for inexact result.static final intIEEE 854-1987 flag for invalid operation.static final intIEEE 854-1987 flag for overflow.static final intIEEE 854-1987 flag for underflow.private intIEEE 854-1987 signals.private final DfpADfpwith value ln(10).private static StringHigh precision string representation of ln(10).private final DfpADfpwith value ln(2).private final Dfp[]A two elementsDfparray with value ln(2) split in two pieces.private static StringHigh precision string representation of ln(2).private final DfpADfpwith value ln(5).private final Dfp[]A two elementsDfparray with value ln(5) split in two pieces.private static StringHigh precision string representation of ln(5).private final DfpADfpwith value 1.private final DfpADfpwith value π.private final Dfp[]A two elementsDfparray with value π split in two pieces.private static StringHigh precision string representation of π.private final intThe number of radix digits.private DfpField.RoundingModeCurrent rounding mode.private final DfpADfpwith value √2.private final DfpADfpwith value √2 / 2.private static StringHigh precision string representation of √2 / 2.private final Dfp[]A two elementsDfparray with value √2 split in two pieces.private static StringHigh precision string representation of √2.private final DfpADfpwith value √3.private final DfpADfpwith value √3 / 3.private static StringHigh precision string representation of √3 / 3.private static StringHigh precision string representation of √3.private final DfpADfpwith value 2.private final DfpADfpwith value 0. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the IEEE 854 status flags.static DfpcomputeExp(Dfp a, Dfp one) Compute exp(a).static DfpCompute ln(a).private static DfpCompute π using Jonathan and Peter Borwein quartic formula.private static voidcomputeStringConstants(int highPrecisionDecimalDigits) Recompute the high precision string constants.getE()Get the constant e.Dfp[]Get the constant e split in two pieces.intGet the IEEE 854 status flags.getLn10()Get the constant ln(10).getLn2()Get the constant ln(2).Dfp[]Get the constant ln(2) split in two pieces.getLn5()Get the constant ln(5).Dfp[]Get the constant ln(5) split in two pieces.getOne()Get the constant 1.getPi()Get the constant π.Dfp[]Get the constant π split in two pieces.intGet the number of radix digits of theDfpinstances built by this factory.Get the current rounding mode.Class<? extends FieldElement<Dfp>> Returns the runtime class of the FieldElement.getSqr2()Get the constant √2.Get the constant √2 / 2.Dfp[]Get the constant √2 split in two pieces.getSqr3()Get the constant √3.Get the constant √3 / 3.getTwo()Get the constant 2.getZero()Get the constant 0.newDfp()Makes aDfpwith a value of 0.newDfp(byte x) Create an instance from a byte value.newDfp(byte sign, byte nans) Creates aDfpwith a non-finite value.newDfp(double x) Create an instance from a double value.newDfp(int x) Create an instance from an int value.newDfp(long x) Create an instance from a long value.Create aDfpgiven a String representation.Copy constructor.voidsetIEEEFlags(int flags) Sets the IEEE 854 status flags.voidsetIEEEFlagsBits(int bits) Sets some bits in the IEEE 854 status flags, without changing the already set bits.voidSet the rounding mode.private Dfp[]Breaks a string representation up into twoDfp's.
-
Field Details
-
FLAG_INVALID
public static final int FLAG_INVALIDIEEE 854-1987 flag for invalid operation.- See Also:
-
FLAG_DIV_ZERO
public static final int FLAG_DIV_ZEROIEEE 854-1987 flag for division by zero.- See Also:
-
FLAG_OVERFLOW
public static final int FLAG_OVERFLOWIEEE 854-1987 flag for overflow.- See Also:
-
FLAG_UNDERFLOW
public static final int FLAG_UNDERFLOWIEEE 854-1987 flag for underflow.- See Also:
-
FLAG_INEXACT
public static final int FLAG_INEXACTIEEE 854-1987 flag for inexact result.- See Also:
-
sqr2String
High precision string representation of √2. -
sqr2ReciprocalString
High precision string representation of √2 / 2. -
sqr3String
High precision string representation of √3. -
sqr3ReciprocalString
High precision string representation of √3 / 3. -
piString
High precision string representation of π. -
eString
High precision string representation of e. -
ln2String
High precision string representation of ln(2). -
ln5String
High precision string representation of ln(5). -
ln10String
High precision string representation of ln(10). -
radixDigits
private final int radixDigitsThe number of radix digits. Note these depend on the radix which is 10000 digits, so each one is equivalent to 4 decimal digits. -
zero
ADfpwith value 0. -
one
ADfpwith value 1. -
two
ADfpwith value 2. -
sqr2
ADfpwith value √2. -
sqr2Split
A two elementsDfparray with value √2 split in two pieces. -
sqr2Reciprocal
ADfpwith value √2 / 2. -
sqr3
ADfpwith value √3. -
sqr3Reciprocal
ADfpwith value √3 / 3. -
pi
ADfpwith value π. -
piSplit
A two elementsDfparray with value π split in two pieces. -
e
ADfpwith value e. -
eSplit
A two elementsDfparray with value e split in two pieces. -
ln2
ADfpwith value ln(2). -
ln2Split
A two elementsDfparray with value ln(2) split in two pieces. -
ln5
ADfpwith value ln(5). -
ln5Split
A two elementsDfparray with value ln(5) split in two pieces. -
ln10
ADfpwith value ln(10). -
rMode
Current rounding mode. -
ieeeFlags
private int ieeeFlagsIEEE 854-1987 signals.
-
-
Constructor Details
-
DfpField
public DfpField(int decimalDigits) Create a factory for the specified number of radix digits.Note that since the
Dfpclass uses 10000 as its radix, each radix digit is equivalent to 4 decimal digits. This implies that asking for 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in all cases.- Parameters:
decimalDigits- minimal number of decimal digits.
-
DfpField
private DfpField(int decimalDigits, boolean computeConstants) Create a factory for the specified number of radix digits.Note that since the
Dfpclass uses 10000 as its radix, each radix digit is equivalent to 4 decimal digits. This implies that asking for 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in all cases.- Parameters:
decimalDigits- minimal number of decimal digitscomputeConstants- if true, the transcendental constants for the given precision must be computed (setting this flag to false is RESERVED for the internal recursive call)
-
-
Method Details
-
getRadixDigits
public int getRadixDigits()Get the number of radix digits of theDfpinstances built by this factory.- Returns:
- number of radix digits
-
setRoundingMode
Set the rounding mode. If not set, the default value isDfpField.RoundingMode.ROUND_HALF_EVEN. -
getRoundingMode
Get the current rounding mode.- Returns:
- current rounding mode
-
getIEEEFlags
public int getIEEEFlags()Get the IEEE 854 status flags.- Returns:
- IEEE 854 status flags
- See Also:
-
clearIEEEFlags
public void clearIEEEFlags()Clears the IEEE 854 status flags.- See Also:
-
setIEEEFlags
public void setIEEEFlags(int flags) Sets the IEEE 854 status flags.- Parameters:
flags- desired value for the flags- See Also:
-
setIEEEFlagsBits
public void setIEEEFlagsBits(int bits) Sets some bits in the IEEE 854 status flags, without changing the already set bits.Calling this method is equivalent to call
setIEEEFlags(getIEEEFlags() | bits)- Parameters:
bits- bits to set- See Also:
-
newDfp
Makes aDfpwith a value of 0.- Returns:
- a new
Dfpwith a value of 0
-
newDfp
Create an instance from a byte value.- Parameters:
x- value to convert to an instance- Returns:
- a new
Dfpwith the same value as x
-
newDfp
Create an instance from an int value.- Parameters:
x- value to convert to an instance- Returns:
- a new
Dfpwith the same value as x
-
newDfp
Create an instance from a long value.- Parameters:
x- value to convert to an instance- Returns:
- a new
Dfpwith the same value as x
-
newDfp
Create an instance from a double value.- Parameters:
x- value to convert to an instance- Returns:
- a new
Dfpwith the same value as x
-
newDfp
Copy constructor.- Parameters:
d- instance to copy- Returns:
- a new
Dfpwith the same value as d
-
newDfp
Create aDfpgiven a String representation.- Parameters:
s- string representation of the instance- Returns:
- a new
Dfpparsed from specified string
-
newDfp
Creates aDfpwith a non-finite value.- Parameters:
sign- sign of the Dfp to createnans- code of the value, must be one ofDfp.INFINITE,Dfp.SNAN,Dfp.QNAN- Returns:
- a new
Dfpwith a non-finite value
-
getZero
Get the constant 0. -
getOne
Get the constant 1. -
getRuntimeClass
Returns the runtime class of the FieldElement.- Specified by:
getRuntimeClassin interfaceField<Dfp>- Returns:
- The
Classobject that represents the runtime class of this object.
-
getTwo
Get the constant 2.- Returns:
- a
Dfpwith value 2
-
getSqr2
Get the constant √2.- Returns:
- a
Dfpwith value √2
-
getSqr2Split
Get the constant √2 split in two pieces.- Returns:
- a
Dfpwith value √2 split in two pieces
-
getSqr2Reciprocal
Get the constant √2 / 2.- Returns:
- a
Dfpwith value √2 / 2
-
getSqr3
Get the constant √3.- Returns:
- a
Dfpwith value √3
-
getSqr3Reciprocal
Get the constant √3 / 3.- Returns:
- a
Dfpwith value √3 / 3
-
getPi
Get the constant π.- Returns:
- a
Dfpwith value π
-
getPiSplit
Get the constant π split in two pieces.- Returns:
- a
Dfpwith value π split in two pieces
-
getE
Get the constant e.- Returns:
- a
Dfpwith value e
-
getESplit
Get the constant e split in two pieces.- Returns:
- a
Dfpwith value e split in two pieces
-
getLn2
Get the constant ln(2).- Returns:
- a
Dfpwith value ln(2)
-
getLn2Split
Get the constant ln(2) split in two pieces.- Returns:
- a
Dfpwith value ln(2) split in two pieces
-
getLn5
Get the constant ln(5).- Returns:
- a
Dfpwith value ln(5)
-
getLn5Split
Get the constant ln(5) split in two pieces.- Returns:
- a
Dfpwith value ln(5) split in two pieces
-
getLn10
Get the constant ln(10).- Returns:
- a
Dfpwith value ln(10)
-
split
Breaks a string representation up into twoDfp's. The split is such that the sum of them is equivalent to the input string, but has higher precision than using a single Dfp.- Parameters:
a- string representation of the number to split- Returns:
- an array of two
Dfpinstances which sum equals a
-
computeStringConstants
private static void computeStringConstants(int highPrecisionDecimalDigits) Recompute the high precision string constants.- Parameters:
highPrecisionDecimalDigits- precision at which the string constants mus be computed
-
computePi
Compute π using Jonathan and Peter Borwein quartic formula.- Parameters:
one- constant with value 1 at desired precisiontwo- constant with value 2 at desired precisionthree- constant with value 3 at desired precision- Returns:
- π
-
computeExp
Compute exp(a).- Parameters:
a- number for which we want the exponentialone- constant with value 1 at desired precision- Returns:
- exp(a)
-
computeLn
Compute ln(a). Let f(x) = ln(x), We know that f'(x) = 1/x, thus from Taylor's theorem we have: ----- n+1 n f(x) = \ (-1) (x - 1) / ---------------- for 1 invalid input: '<'= n invalid input: '<'= 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- number for which we want the exponentialone- constant with value 1 at desired precisiontwo- constant with value 2 at desired precision- Returns:
- ln(a)
-