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

public Class Dfp

extends Object
implements RealFieldElement<Dfp>
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.RealFieldElement, org.apache.commons.math3.FieldElement
Known Direct Subclasses
org.apache.commons.math3.dfp.DfpDec
Imports
java.util.Arrays, org.apache.commons.math3.RealFieldElement, org.apache.commons.math3.exception.DimensionMismatchException, org.apache.commons.math3.util.FastMath

Decimal floating point library for Java

Another floating point class. This one is built using radix 10000 which is 104, so its almost decimal.

The design goals here are:

  1. Decimal math, or close to it
  2. Settable precision (but no mix between numbers using different settings)
  3. Portability. Code should be kept as portable as possible.
  4. Performance
  5. Accuracy - Results should always be +/- 1 ULP for basic algebraic operation
  6. Comply with IEEE 854-1987 as much as possible. (See IEEE 854-1987 notes below)

Trade offs:

  1. Memory foot print. I'm using more memory than necessary to represent numbers to get better performance.
  2. Digits are bigger, so rounding is a greater loss. So, if you really need 12 decimal digits, better use 4 base 10000 digits there can be one partially filled.

Numbers are represented in the following form:

 n  =  sign × mant × (radix)exp;

where sign is ±1, mantissa represents a fractional number between zero and one. mant[0] is the least significant digit. exp is in the range of -32767 to 32768

IEEE 854-1987 Notes and differences

IEEE 854 requires the radix to be either 2 or 10. The radix here is 10000, so that requirement is not met, but it is possible that a subclassed can be made to make it behave as a radix 10 number. It is my opinion that if it looks and behaves as a radix 10 number then it is one and that requirement would be met.

The radix of 10000 was chosen because it should be faster to operate on 4 decimal digits at once instead of one at a time. Radix 10 behavior can be realized by adding an additional rounding step to ensure that the number of decimal digits represented is constant.

The IEEE standard specifically leaves out internal data encoding, so it is reasonable to conclude that such a subclass of this radix 10000 system is merely an encoding of a radix 10 system.

IEEE 854 also specifies the existence of "sub-normal" numbers. This class does not contain any such entities. The most significant radix 10000 digit is always non-zero. Instead, we support "gradual underflow" by raising the underflow flag for numbers less with exponent less than expMin, but don't flush to zero until the exponent reaches MIN_EXP-digits. Thus the smallest number we can represent would be: 1E(-(MIN_EXP-digits-1)*4), eg, for digits=5, MIN_EXP=-32767, that would be 1e-131092.

IEEE 854 defines that the implied radix point lies just to the right of the most significant digit and to the left of the remaining digits. This implementation puts the implied radix point to the left of all digits including the most significant one. The most significant digit here is the one just to the right of the radix point. This is a fine detail and is really only a matter of definition. Any side effects of this can be rendered invisible by a subclass.

Since
2.2
See Also
DfpField

Field Summary

Modifier and TypeField and Description
private static final String
ADD_TRAP

Name for traps triggered by addition.

private static final String
ALIGN_TRAP

Name for traps triggered by alignment.

private static final String
DIVIDE_TRAP

Name for traps triggered by division.

public static final int
ERR_SCALE

The amount under/overflows are scaled by before going to trap handler

protected int
exp

Exponent.

private final DfpField
field

Factory building similar Dfp's.

public static final byte
FINITE

Indicator value for normal finite numbers.

private static final String
GREATER_THAN_TRAP

Name for traps triggered by greaterThan.

public static final byte
INFINITE

Indicator value for Infinity.

private static final String
LESS_THAN_TRAP

Name for traps triggered by lessThan.

protected int[]
mant

Mantissa.

public static final int
MAX_EXP

The maximum exponent before overflow is signaled and results flushed to infinity

public static final int
MIN_EXP

The minimum exponent before underflow is signaled.

private static final String
MULTIPLY_TRAP

Name for traps triggered by multiplication.

private static final String
NAN_STRING

String for NaN representation.

protected byte
nans

Indicator for non-finite / non-number values.

private static final String
NEG_INFINITY_STRING

String for negative infinity representation.

private static final String
NEW_INSTANCE_TRAP

Name for traps triggered by newInstance.

private static final String
NEXT_AFTER_TRAP

Name for traps triggered by nextAfter.

private static final String
POS_INFINITY_STRING

String for positive infinity representation.

public static final byte
QNAN

Indicator value for quiet NaN.

public static final int
RADIX

The radix, or base of this system.

protected byte
sign

Sign bit: 1 for positive, -1 for negative.

public static final byte
SNAN

Indicator value for signaling NaN.

private static final String
SQRT_TRAP

Name for traps triggered by square root.

private static final String
TRUNC_TRAP

Name for traps triggered by truncation.

Constructor Summary

AccessConstructor and Description
protected
Dfp(final DfpField
field to which this instance belongs
field
)

Makes an instance with a value of zero.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
byte
value to convert to an instance
x
)

Create an instance from a byte value.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
int
value to convert to an instance
x
)

Create an instance from an int value.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
long
value to convert to an instance
x
)

Create an instance from a long value.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
double
value to convert to an instance
x
)

Create an instance from a double value.

public
Dfp(final Dfp
instance to copy
d
)

Copy constructor.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
final String
string representation of the instance
s
)

Create an instance from a String representation.

protected
Dfp(final DfpField
field to which this instance belongs
field
,
final byte
sign of the Dfp to create
sign
,
final byte
code of the value, must be one of INFINITE, SNAN, QNAN
nans
)

Creates an instance with a non-finite value.

Method Summary

Modifier and TypeMethod and Description
public Dfp

Returns:

absolute value of instance
abs
()

Implements org.apache.commons.math3.RealFieldElement.abs.

Get the absolute value of instance.

public Dfp
acos()

Implements org.apache.commons.math3.RealFieldElement.acos.

Arc cosine operation.

public Dfp
acosh()

Implements org.apache.commons.math3.RealFieldElement.acosh.

Inverse hyperbolic cosine operation.

public Dfp

Returns:

sum of this and x
add
(final Dfp
number to add
x
)

Implements org.apache.commons.math3.FieldElement.add.

Add x to this.

public Dfp
add(final double
right hand side parameter of the operator
a
)

Implements org.apache.commons.math3.RealFieldElement.add.

'+' operator.

protected int

Returns:

last digit destroyed by shifting right
align
(int
desired exponent
e
)

Make our exp equal to the supplied one, this may cause rounding.

public Dfp
asin()

Implements org.apache.commons.math3.RealFieldElement.asin.

Arc sine operation.

public Dfp
asinh()

Implements org.apache.commons.math3.RealFieldElement.asinh.

Inverse hyperbolic sine operation.

public Dfp
atan()

Implements org.apache.commons.math3.RealFieldElement.atan.

Arc tangent operation.

public Dfp
atan2(final Dfp
second argument of the arc tangent
x
)

Implements org.apache.commons.math3.RealFieldElement.atan2.

Two arguments arc tangent operation.

public Dfp
atanh()

Implements org.apache.commons.math3.RealFieldElement.atanh.

Inverse hyperbolic tangent operation.

public Dfp
public Dfp

Returns:

rounded value
ceil
()

Implements org.apache.commons.math3.RealFieldElement.ceil.

Round to an integer using the round ceil mode.

public int

Returns:

type of the number
classify
()

Returns the type - one of FINITE, INFINITE, SNAN, QNAN.

private static int

Returns:

-1 if ab and 0 if a==b Note this method does not properly handle NaNs or numbers with different precision.
compare
(final Dfp
first instance in comparison
a
,
final Dfp
second instance in comparison
b
)

Compare two instances.

protected int

Returns:

???
complement
(int
???
extra
)

Negate the mantissa of this by computing the complement.

public static Dfp

Returns:

a number with the value of x and the sign of y
copysign
(final Dfp
number to get the value from
x
,
final Dfp
number to get the sign from
y
)

Creates an instance that is the same as x except that it has the sign of y.

public Dfp
copySign(final Dfp
the sign for the returned value
s
)

Implements org.apache.commons.math3.RealFieldElement.copySign.

Returns the instance with the sign of the argument.

public Dfp
copySign(final double
the sign for the returned value
s
)

Implements org.apache.commons.math3.RealFieldElement.copySign, org.apache.commons.math3.RealFieldElement.copySign.

Returns the instance with the sign of the argument.

public Dfp
cos()

Implements org.apache.commons.math3.RealFieldElement.cos.

Cosine operation.

public Dfp
cosh()

Implements org.apache.commons.math3.RealFieldElement.cosh.

Hyperbolic cosine operation.

protected String

Returns:

string representation of the instance in scientific notation
dfp2sci
()

Convert an instance to a string using scientific notation.

protected String

Returns:

string representation of the instance in normal notation
dfp2string
()

Convert an instance to a string using normal notation.

public Dfp

Returns:

quotient of this by divisor
divide
(Dfp
divisor
divisor
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide this by divisor.

public Dfp

Returns:

quotient of this by divisor
divide
(int
divisor
divisor
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide by a single digit less than radix.

public Dfp
divide(final double
right hand side parameter of the operator
a
)

Implements org.apache.commons.math3.RealFieldElement.divide.

'÷' operator.

public Dfp

Returns:

The suggested return value from the trap handler
dotrap
(int
the trap type
type
,
String
- name of routine trap occurred in
what
,
Dfp
- input operator to function
oper
,
Dfp
- the result computed prior to the trap
result
)

Raises a trap.

public boolean

Returns:

true if instance is equal to x and neither are NaN, false otherwise
equals
(final Object
object to check instance against
other
)

Overrides java.lang.Object.equals.

Check if instance is equal to x.

public Dfp
public Dfp
expm1()

Implements org.apache.commons.math3.RealFieldElement.expm1.

Exponential minus 1.

public Dfp

Returns:

rounded value
floor
()

Implements org.apache.commons.math3.RealFieldElement.floor.

Round to an integer using the round floor mode.

public DfpField

Returns:

Field (really a DfpField) to which the instance belongs
getField
()

Implements org.apache.commons.math3.FieldElement.getField.

Get the Field (really a DfpField) to which the instance belongs.

public Dfp

Returns:

a Dfp with value one
getOne
()

Get the constant 1.

public int

Returns:

number of radix digits
getRadixDigits
()

Get the number of radix digits of the instance.

public double
getReal()

Implements org.apache.commons.math3.RealFieldElement.getReal.

Get the real value of the number.

public Dfp

Returns:

a Dfp with value two
getTwo
()

Get the constant 2.

public Dfp

Returns:

a Dfp with value zero
getZero
()

Get the constant 0.

public boolean

Returns:

true if instance is greater than x and neither are NaN, false otherwise
greaterThan
(final Dfp
number to check instance against
x
)

Check if instance is greater than x.

public int

Returns:

a hash code value for this object
hashCode
()

Overrides java.lang.Object.hashCode.

Gets a hashCode for the instance.

public Dfp
hypot(final Dfp
a value
y
)

Implements org.apache.commons.math3.RealFieldElement.hypot.

Returns the hypotenuse of a triangle with sides this and y - sqrt(this2 +y2) avoiding intermediate overflow or underflow.

public int

Returns:

integer base 10 logarithm
intLog10
()

Get the exponent of the greatest power of 10 that is less than or equal to abs(this).

public int

Returns:

converted number
intValue
()

Convert this to an integer.

public boolean

Returns:

true if instance is infinite
isInfinite
()

Check if instance is infinite.

public boolean

Returns:

true if instance is not a number
isNaN
()

Check if instance is not a number.

public boolean

Returns:

true if instance is equal to zero
isZero
()

Check if instance is equal to zero.

public boolean

Returns:

true if instance is less than x and neither are NaN, false otherwise
lessThan
(final Dfp
number to check instance against
x
)

Check if instance is less than x.

public Dfp
linearCombination(final Dfp[]
Factors.
a
,
final Dfp[]
Factors.
b
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
public Dfp
linearCombination(final Dfp
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final Dfp
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
linearCombination(final double
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination, org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
linearCombination(final Dfp
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final Dfp
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
,
final Dfp
first factor of the third term
a3
,
final Dfp
second factor of the third term
b3
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
linearCombination(final double
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
,
final double
first factor of the third term
a3
,
final Dfp
second factor of the third term
b3
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination, org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
linearCombination(final Dfp
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final Dfp
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
,
final Dfp
first factor of the third term
a3
,
final Dfp
second factor of the third term
b3
,
final Dfp
first factor of the third term
a4
,
final Dfp
second factor of the third term
b4
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
linearCombination(final double
first factor of the first term
a1
,
final Dfp
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final Dfp
second factor of the second term
b2
,
final double
first factor of the third term
a3
,
final Dfp
second factor of the third term
b3
,
final double
first factor of the third term
a4
,
final Dfp
second factor of the third term
b4
)

Implements org.apache.commons.math3.RealFieldElement.linearCombination, org.apache.commons.math3.RealFieldElement.linearCombination.

Compute a linear combination.

public Dfp
log()

Implements org.apache.commons.math3.RealFieldElement.log.

Natural logarithm.

public int

Returns:

integer base 10 logarithm
log10
()
Deprecated as of 3.2, replaced by intLog10(), in 4.0 the return type will be changed to Dfp

Get the exponent of the greatest power of 10 that is less than or equal to abs(this).

public int

Returns:

integer base 10000 logarithm
log10K
()

Get the exponent of the greatest power of 10000 that is less than or equal to the absolute value of this.

public Dfp
log1p()

Implements org.apache.commons.math3.RealFieldElement.log1p.

Shifted natural logarithm.

public Dfp

Returns:

product of this and x
multiply
(final Dfp
multiplicand
x
)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiply this by x.

public Dfp

Returns:

product of this and x
multiply
(final int
multiplicand
x
)

Implements org.apache.commons.math3.FieldElement.multiply, org.apache.commons.math3.FieldElement.multiply.

Multiply this by a single digit x.

public Dfp
multiply(final double
right hand side parameter of the operator
a
)

Implements org.apache.commons.math3.RealFieldElement.multiply.

'×' operator.

private Dfp

Returns:

product of this and x
multiplyFast
(final int
multiplicand
x
)

Multiply this by a single digit 0<=x<radix.

public Dfp

Returns:

the opposite of this
negate
()

Implements org.apache.commons.math3.FieldElement.negate.

Returns a number that is this number with the sign bit reversed.

public boolean

Returns:

true if instance is not NaN and less than or equal to 0, false otherwise
negativeOrNull
()

Check if instance is less than or equal to 0.

public Dfp

Returns:

a new instance with a value of 0
newInstance
()

Create an instance with a value of 0.

public Dfp

Returns:

a new instance with value x
newInstance
(final byte
value to convert to an instance
x
)

Create an instance from a byte value.

public Dfp

Returns:

a new instance with value x
newInstance
(final int
value to convert to an instance
x
)

Create an instance from an int value.

public Dfp

Returns:

a new instance with value x
newInstance
(final long
value to convert to an instance
x
)

Create an instance from a long value.

public Dfp

Returns:

a new instance with value x
newInstance
(final double
value to convert to an instance
x
)

Create an instance from a double value.

public Dfp

Returns:

a new instance with the same value as d
newInstance
(final Dfp
instance to copy
d
)

Create an instance by copying an existing one.

public Dfp

Returns:

a new instance parsed from specified string
newInstance
(final String
string representation of the instance
s
)

Create an instance from a String representation.

public Dfp

Returns:

a new instance with a non-finite value
newInstance
(final byte
sign of the Dfp to create
sig
,
final byte
code of the value, must be one of INFINITE, SNAN, QNAN
code
)

Creates an instance with a non-finite value.

public Dfp

Returns:

closest number next to instance in the direction of x
nextAfter
(final Dfp
direction where to look at
x
)

Returns the next number greater than this one in the direction of x.

public boolean

Returns:

true if instance is not NaN and greater than or equal to 0, false otherwise
positiveOrNull
()

Check if instance is greater than or equal to 0.

public Dfp
pow(final double
power to apply
p
)

Implements org.apache.commons.math3.RealFieldElement.pow, org.apache.commons.math3.RealFieldElement.pow.

Power operation.

public Dfp
pow(final int
power to apply
n
)

Implements org.apache.commons.math3.RealFieldElement.pow, org.apache.commons.math3.RealFieldElement.pow.

Integer power operation.

public Dfp
pow(final Dfp
exponent
e
)

Implements org.apache.commons.math3.RealFieldElement.pow.

Power operation.

public Dfp

Returns:

10e
power10
(final int
desired power
e
)

Return the specified power of 10.

public Dfp

Returns:

10000e
power10K
(final int
desired power
e
)

Get the specified power of 10000.

public Dfp
reciprocal()

Implements org.apache.commons.math3.RealFieldElement.reciprocal.

Returns the multiplicative inverse of this element.

public Dfp

Returns:

this less n × d, where n is the integer closest to this/d
remainder
(final Dfp
divisor
d
)

Implements org.apache.commons.math3.RealFieldElement.remainder.

Returns the IEEE remainder.

public Dfp
remainder(final double
right hand side parameter of the operator
a
)

Implements org.apache.commons.math3.RealFieldElement.remainder, org.apache.commons.math3.RealFieldElement.remainder.

IEEE remainder operator.

public Dfp

Returns:

rounded value
rint
()

Implements org.apache.commons.math3.RealFieldElement.rint.

Round to nearest integer using the round-half-even method.

public Dfp
rootN(final int
order of the root
n
)

Implements org.apache.commons.math3.RealFieldElement.rootN.

Nth root.

protected int

Returns:

the IEEE flag if an exception occurred
round
(int
???
n
)

Round this given the next digit n using the current rounding mode.

public long
round()

Implements org.apache.commons.math3.RealFieldElement.round.

Get the closest long to instance value.

public Dfp
scalb(final int
power of 2
n
)

Implements org.apache.commons.math3.RealFieldElement.scalb.

Multiply the instance by a power of 2.

protected void
shiftLeft()

Shift the mantissa left, and adjust the exponent to compensate.

protected void
shiftRight()

Shift the mantissa right, and adjust the exponent to compensate.

public Dfp
signum()

Implements org.apache.commons.math3.RealFieldElement.signum.

Compute the signum of the instance.

public Dfp
public Dfp
sinh()

Implements org.apache.commons.math3.RealFieldElement.sinh.

Hyperbolic sine operation.

public Dfp

Returns:

square root of the instance
sqrt
()

Implements org.apache.commons.math3.RealFieldElement.sqrt.

Compute the square root.

public boolean

Returns:

true if instance is not NaN and less than or equal to 0, false otherwise
strictlyNegative
()

Check if instance is strictly less than 0.

public boolean

Returns:

true if instance is not NaN and greater than or equal to 0, false otherwise
strictlyPositive
()

Check if instance is strictly greater than 0.

public Dfp

Returns:

difference of this and a
subtract
(final Dfp
number to subtract
x
)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtract x from this.

public Dfp
subtract(final double
right hand side parameter of the operator
a
)

Implements org.apache.commons.math3.RealFieldElement.subtract.

'-' operator.

public Dfp
tan()

Implements org.apache.commons.math3.RealFieldElement.tan.

Tangent operation.

public Dfp
tanh()

Implements org.apache.commons.math3.RealFieldElement.tanh.

Hyperbolic tangent operation.

public double

Returns:

a double approximating the instance
toDouble
()

Convert the instance into a double.

public double[]

Returns:

an array of two doubles which sum represent the instance
toSplitDouble
()

Convert the instance into a split double.

public String

Returns:

string representation of the instance
toString
()

Overrides java.lang.Object.toString.

Get a string representation of the instance.

protected Dfp

Returns:

the value that should be return by the operation triggering the trap
trap
(int
The exception type - e.g. FLAG_OVERFLOW
type
,
String
The name of the routine we were in e.g. divide()
what
,
Dfp
An operand to this function if any
oper
,
Dfp
The default return value if trap not enabled
def
,
Dfp
The result that is specified to be delivered per IEEE 854, if any
result
)

Trap handler.

protected Dfp

Returns:

truncated value
trunc
(final DfpField.RoundingMode
rounding mode to use
rmode
)

Does the integer conversions with the specified rounding.

public boolean

Returns:

true if instance is not equal to x and neither are NaN, false otherwise
unequal
(final Dfp
number to check instance against
x
)

Check if instance is not equal to x.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait