Package edu.jas.arith
Class BigDecimal
- java.lang.Object
-
- edu.jas.arith.BigDecimal
-
- All Implemented Interfaces:
Rational,AbelianGroupElem<BigDecimal>,AbelianGroupFactory<BigDecimal>,Element<BigDecimal>,ElemFactory<BigDecimal>,GcdRingElem<BigDecimal>,MonoidElem<BigDecimal>,MonoidFactory<BigDecimal>,RingElem<BigDecimal>,RingFactory<BigDecimal>,java.io.Serializable,java.lang.Comparable<BigDecimal>
public final class BigDecimal extends java.lang.Object implements GcdRingElem<BigDecimal>, RingFactory<BigDecimal>, Rational
BigDecimal class to make java.math.BigDecimal available with RingElem interface. Objects of this class are immutable. Experimental, use with care, compareTo is some times hacked.- Author:
- Heinz Kredel
- See Also:
BigDecimal, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description java.math.MathContextcontextstatic java.math.MathContextDEFAULT_CONTEXTstatic intDEFAULT_PRECISIONstatic booleanEXACT_EQUALIf true, then use equals from java.math.BigDecimal, else use hacked approximate compareTo().static BigDecimalONEThe constant 1.java.math.BigDecimalvalThe data structure.static BigDecimalZEROThe constant 0.
-
Constructor Summary
Constructors Constructor Description BigDecimal()Constructor for BigDecimal without parameters.BigDecimal(double a)Constructor for BigDecimal from double.BigDecimal(double a, java.math.MathContext mc)Constructor for BigDecimal from double and a context.BigDecimal(long a)Constructor for BigDecimal from long.BigDecimal(long a, java.math.MathContext mc)Constructor for BigDecimal from long and a context.BigDecimal(BigRational a)Constructor for BigDecimal from BigRational.BigDecimal(BigRational a, java.math.MathContext mc)Constructor for BigDecimal from BigRational.BigDecimal(java.lang.String s)Constructor for BigDecimal from String.BigDecimal(java.lang.String s, java.math.MathContext mc)Constructor for BigDecimal from String.BigDecimal(java.math.BigDecimal a)Constructor for BigDecimal from math.BigDecimal.BigDecimal(java.math.BigDecimal a, java.math.MathContext mc)Constructor for BigDecimal from math.BigDecimal.BigDecimal(java.math.BigInteger a)Constructor for BigDecimal from java.math.BigInteger.BigDecimal(java.math.BigInteger a, java.math.MathContext mc)Constructor for BigDecimal from java.math.BigInteger.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimalabs()Absolute value of this.longbitLength()Returns the number of bits in the representation of this BigDecimal, including a sign bit.java.math.BigIntegercharacteristic()Characteristic of this ring.intcompareTo(BigDecimal b)Compare to BigDecimal b.intcompareToAbsolute(BigDecimal b)Compare absolute to BigDecimal b.intcompareToRelative(BigDecimal b)Compare to relative BigDecimal b.BigDecimalcopy()Clone this.BigDecimalcopy(BigDecimal c)Copy BigDecimal element c.BigDecimaldivide(BigDecimal S)BigDecimal divide.doubledoubleValue()Get this decimal as a double.BigDecimal[]egcd(BigDecimal S)BigDecimal extended greatest common divisor.booleanequals(java.lang.Object b)Comparison with any other object.BigDecimalfactory()Get the corresponding element factory.BigDecimalfromInteger(long a)Get a BigDecimal element from long.BigDecimalfromInteger(java.math.BigInteger a)Get a BigDecimal element from a math.BigDecimal.BigDecimalgcd(BigDecimal S)BigDecimal greatest common divisor.java.util.List<BigDecimal>generators()Get a list of the generating elements.BigDecimalgetDecimal()Get the decimal representation.BigDecimalgetONE()Get the one element.BigRationalgetRational()Get the rational representation.BigDecimalgetZERO()Get the zero element.inthashCode()Hash code for this BigDecimal.BigDecimalinverse()Integer inverse.booleanisAssociative()Query if this ring is associative.booleanisCommutative()Query if this ring is commutative.booleanisField()Query if this ring is a field.booleanisFinite()Is this structure finite or infinite.booleanisONE()Is BigDecimal number one.booleanisUnit()Is BigDecimal number unit.booleanisZERO()Is BigDecimal number zero.BigDecimalmultiply(BigDecimal S)BigDecimal multiply.BigDecimalnegate()Negate this.BigDecimalparse(java.io.Reader r)BigDecimal parse from Reader.BigDecimalparse(java.lang.String s)BigDecimal parse from String.BigDecimal[]quotientRemainder(BigDecimal S)BigDecimal compute quotient and remainder.BigDecimalrandom(int n)BigDecimal random.BigDecimalrandom(int n, int e)BigDecimal random.BigDecimalrandom(int n, int e, java.util.Random rnd)BigDecimal random.BigDecimalrandom(int n, java.util.Random rnd)BigDecimal random.BigDecimalremainder(BigDecimal S)BigDecimal remainder.intsignum()signum.BigDecimalsubtract(BigDecimal S)BigDecimal subtract.BigDecimalsum(BigDecimal S)BigDecimal summation.java.lang.StringtoScript()Get a scripting compatible string representation.java.lang.StringtoScriptFactory()Get a scripting compatible string representation of the factory.java.lang.StringtoString()Get the String representation.static BigDecimalvalueOf(long a)Get a BigDecimal element from long.static BigDecimalvalueOf(java.math.BigDecimal a)Get a BigDecimal element from a math.BigDecimal.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface edu.jas.structure.ElemFactory
valueOf
-
Methods inherited from interface edu.jas.structure.MonoidElem
leftDivide, leftRemainder, power, rightDivide, rightRemainder, twosidedDivide, twosidedRemainder
-
-
-
-
Field Detail
-
val
public final java.math.BigDecimal val
The data structure.
-
DEFAULT_CONTEXT
public static final java.math.MathContext DEFAULT_CONTEXT
-
DEFAULT_PRECISION
public static final int DEFAULT_PRECISION
-
context
public final java.math.MathContext context
-
EXACT_EQUAL
public static final boolean EXACT_EQUAL
If true, then use equals from java.math.BigDecimal, else use hacked approximate compareTo().- See Also:
- Constant Field Values
-
ZERO
public static final BigDecimal ZERO
The constant 0.
-
ONE
public static final BigDecimal ONE
The constant 1.
-
-
Constructor Detail
-
BigDecimal
public BigDecimal(java.math.BigDecimal a)
Constructor for BigDecimal from math.BigDecimal.- Parameters:
a- java.math.BigDecimal.
-
BigDecimal
public BigDecimal(java.math.BigDecimal a, java.math.MathContext mc)
Constructor for BigDecimal from math.BigDecimal.- Parameters:
a- java.math.BigDecimal.mc- MathContext.
-
BigDecimal
public BigDecimal(long a)
Constructor for BigDecimal from long.- Parameters:
a- long.
-
BigDecimal
public BigDecimal(long a, java.math.MathContext mc)
Constructor for BigDecimal from long and a context.- Parameters:
a- long.mc- MathContext.
-
BigDecimal
public BigDecimal(double a)
Constructor for BigDecimal from double.- Parameters:
a- double.
-
BigDecimal
public BigDecimal(double a, java.math.MathContext mc)
Constructor for BigDecimal from double and a context.- Parameters:
a- double.mc- MathContext.
-
BigDecimal
public BigDecimal(java.math.BigInteger a)
Constructor for BigDecimal from java.math.BigInteger.- Parameters:
a- java.math.BigInteger.
-
BigDecimal
public BigDecimal(java.math.BigInteger a, java.math.MathContext mc)
Constructor for BigDecimal from java.math.BigInteger.- Parameters:
a- java.math.BigInteger.mc- MathContext.
-
BigDecimal
public BigDecimal(BigRational a)
Constructor for BigDecimal from BigRational.- Parameters:
a- edu.jas.arith.BigRational.
-
BigDecimal
public BigDecimal(BigRational a, java.math.MathContext mc)
Constructor for BigDecimal from BigRational.- Parameters:
a- edu.jas.arith.BigRational.mc- MathContext.
-
BigDecimal
public BigDecimal(java.lang.String s)
Constructor for BigDecimal from String.- Parameters:
s- String.
-
BigDecimal
public BigDecimal(java.lang.String s, java.math.MathContext mc)
Constructor for BigDecimal from String.- Parameters:
s- String.mc- MathContext.
-
BigDecimal
public BigDecimal()
Constructor for BigDecimal without parameters.
-
-
Method Detail
-
factory
public BigDecimal factory()
Get the corresponding element factory.- Specified by:
factoryin interfaceElement<BigDecimal>- Returns:
- factory for this Element.
- See Also:
Element.factory()
-
generators
public java.util.List<BigDecimal> generators()
Get a list of the generating elements.- Specified by:
generatorsin interfaceElemFactory<BigDecimal>- Returns:
- list of generators for the algebraic structure.
- See Also:
ElemFactory.generators()
-
isFinite
public boolean isFinite()
Is this structure finite or infinite.- Specified by:
isFinitein interfaceElemFactory<BigDecimal>- Returns:
- true if this structure is finite, else false.
- See Also:
Note: is actually finite but returns false.
-
copy
public BigDecimal copy()
Clone this.- Specified by:
copyin interfaceElement<BigDecimal>- Returns:
- Creates and returns a copy of this Element.
- See Also:
Object.clone()
-
copy
public BigDecimal copy(BigDecimal c)
Copy BigDecimal element c.- Specified by:
copyin interfaceElemFactory<BigDecimal>- Parameters:
c- BigDecimal.- Returns:
- a copy of c.
-
getZERO
public BigDecimal getZERO()
Get the zero element.- Specified by:
getZEROin interfaceAbelianGroupFactory<BigDecimal>- Returns:
- 0.
-
getONE
public BigDecimal getONE()
Get the one element.- Specified by:
getONEin interfaceMonoidFactory<BigDecimal>- Returns:
- 1.
-
getDecimal
public BigDecimal getDecimal()
Get the decimal representation.- Returns:
- decimal.
-
getRational
public BigRational getRational()
Get the rational representation.- Specified by:
getRationalin interfaceRational- Returns:
- rational number.
-
isCommutative
public boolean isCommutative()
Query if this ring is commutative.- Specified by:
isCommutativein interfaceMonoidFactory<BigDecimal>- Returns:
- true.
-
isAssociative
public boolean isAssociative()
Query if this ring is associative. Floating point number addition is not associative, but multiplication is.- Specified by:
isAssociativein interfaceMonoidFactory<BigDecimal>- Returns:
- true.
-
isField
public boolean isField()
Query if this ring is a field.- Specified by:
isFieldin interfaceRingFactory<BigDecimal>- Returns:
- true.
-
characteristic
public java.math.BigInteger characteristic()
Characteristic of this ring.- Specified by:
characteristicin interfaceRingFactory<BigDecimal>- Returns:
- characteristic of this ring.
-
fromInteger
public BigDecimal fromInteger(java.math.BigInteger a)
Get a BigDecimal element from a math.BigDecimal.- Specified by:
fromIntegerin interfaceElemFactory<BigDecimal>- Parameters:
a- math.BigDecimal.- Returns:
- a as BigDecimal.
-
valueOf
public static BigDecimal valueOf(java.math.BigDecimal a)
Get a BigDecimal element from a math.BigDecimal.- Parameters:
a- math.BigDecimal.- Returns:
- a as BigDecimal.
-
fromInteger
public BigDecimal fromInteger(long a)
Get a BigDecimal element from long.- Specified by:
fromIntegerin interfaceElemFactory<BigDecimal>- Parameters:
a- long.- Returns:
- a as BigDecimal.
-
valueOf
public static BigDecimal valueOf(long a)
Get a BigDecimal element from long.- Parameters:
a- long.- Returns:
- a as BigDecimal.
-
isZERO
public boolean isZERO()
Is BigDecimal number zero.- Specified by:
isZEROin interfaceAbelianGroupElem<BigDecimal>- Returns:
- If this is 0 then true is returned, else false.
- See Also:
AbelianGroupElem.isZERO()
-
isONE
public boolean isONE()
Is BigDecimal number one.- Specified by:
isONEin interfaceMonoidElem<BigDecimal>- Returns:
- true if this is 1, else false.
- See Also:
MonoidElem.isONE()
-
isUnit
public boolean isUnit()
Is BigDecimal number unit.- Specified by:
isUnitin interfaceMonoidElem<BigDecimal>- Returns:
- true if this is a unit, else false.
- See Also:
MonoidElem.isUnit()
-
toString
public java.lang.String toString()
Get the String representation.- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
doubleValue
public double doubleValue()
Get this decimal as a double.- Returns:
- the decimal as a double
- See Also:
Number.doubleValue()
-
toScript
public java.lang.String toScript()
Get a scripting compatible string representation.- Specified by:
toScriptin interfaceElement<BigDecimal>- Specified by:
toScriptin interfaceElemFactory<BigDecimal>- Returns:
- script compatible representation for this Element.
- See Also:
Element.toScript()
-
toScriptFactory
public java.lang.String toScriptFactory()
Get a scripting compatible string representation of the factory.- Specified by:
toScriptFactoryin interfaceElement<BigDecimal>- Returns:
- script compatible representation for this ElemFactory.
- See Also:
Element.toScriptFactory()
-
compareTo
public int compareTo(BigDecimal b)
Compare to BigDecimal b. Experimental, is hacked.- Specified by:
compareToin interfacejava.lang.Comparable<BigDecimal>- Specified by:
compareToin interfaceElement<BigDecimal>- Parameters:
b- BigDecimal.- Returns:
- 0 if abs(this-b) < epsilon, 1 if this > b, -1 if this < b.
-
compareToAbsolute
public int compareToAbsolute(BigDecimal b)
Compare absolute to BigDecimal b. Experimental, is hacked.- Parameters:
b- BigDecimal.- Returns:
- 0 if abs(this-b) < epsilon, 1 if this > b, -1 if this < b.
-
compareToRelative
public int compareToRelative(BigDecimal b)
Compare to relative BigDecimal b. Experimental, is hacked.- Parameters:
b- BigDecimal.- Returns:
- 0 if abs(this-b)/max(this,b) < epsilon, 1 if this > b, -1 if this < b.
-
equals
public boolean equals(java.lang.Object b)
Comparison with any other object.- Specified by:
equalsin interfaceElement<BigDecimal>- Overrides:
equalsin classjava.lang.Object- Returns:
- true if this is equal to b, else false.
- See Also:
Object.equals(java.lang.Object)
-
hashCode
public int hashCode()
Hash code for this BigDecimal.- Specified by:
hashCodein interfaceElement<BigDecimal>- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hashCode.
- See Also:
Object.hashCode()
-
abs
public BigDecimal abs()
Absolute value of this.- Specified by:
absin interfaceAbelianGroupElem<BigDecimal>- Returns:
- |this|.
- See Also:
AbelianGroupElem.abs()
-
negate
public BigDecimal negate()
Description copied from interface:AbelianGroupElemNegate this.- Specified by:
negatein interfaceAbelianGroupElem<BigDecimal>- Returns:
- - this.
-
signum
public int signum()
signum.- Specified by:
signumin interfaceAbelianGroupElem<BigDecimal>- Returns:
- the sign of this.
- See Also:
AbelianGroupElem.signum()
-
subtract
public BigDecimal subtract(BigDecimal S)
BigDecimal subtract.- Specified by:
subtractin interfaceAbelianGroupElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this-S.
-
divide
public BigDecimal divide(BigDecimal S)
BigDecimal divide.- Specified by:
dividein interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this/S.
-
inverse
public BigDecimal inverse()
Integer inverse. R is a non-zero integer. S=1/R if defined else 0.- Specified by:
inversein interfaceMonoidElem<BigDecimal>- Returns:
- x with this * x = 1, if it exists.
- See Also:
MonoidElem.inverse()
-
remainder
public BigDecimal remainder(BigDecimal S)
BigDecimal remainder.- Specified by:
remainderin interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this - (this/S)*S.
-
quotientRemainder
public BigDecimal[] quotientRemainder(BigDecimal S)
BigDecimal compute quotient and remainder.- Specified by:
quotientRemainderin interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- BigDecimal[] { q, r } with q = this/S and r = rem(this,S).
-
gcd
public BigDecimal gcd(BigDecimal S)
BigDecimal greatest common divisor.- Specified by:
gcdin interfaceRingElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- gcd(this,S).
-
egcd
public BigDecimal[] egcd(BigDecimal S)
BigDecimal extended greatest common divisor.- Specified by:
egcdin interfaceRingElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- [ gcd(this,S), a, b ] with a*this + b*S = gcd(this,S).
-
random
public BigDecimal random(int n)
BigDecimal random.- Specified by:
randomin interfaceElemFactory<BigDecimal>- Parameters:
n- such that 0 ≤ val(r) ≤ (2n-1). 0 ≤ exp(r) ≤ (10-1).- Returns:
- r, a random BigDecimal.
-
random
public BigDecimal random(int n, java.util.Random rnd)
BigDecimal random.- Specified by:
randomin interfaceElemFactory<BigDecimal>- Parameters:
n- such that 0 ≤ val(r) ≤ (2n-1). 0 ≤ exp(r) ≤ (10-1).rnd- is a source for random bits.- Returns:
- r, a random BigDecimal.
-
random
public BigDecimal random(int n, int e)
BigDecimal random.- Parameters:
n- such that 0 ≤ val(r) ≤ (2n-1).e- such that 0 ≤ exp(r) ≤ (e-1).- Returns:
- r, a random BigDecimal.
-
random
public BigDecimal random(int n, int e, java.util.Random rnd)
BigDecimal random.- Parameters:
n- such that 0 ≤ val(r) ≤ (2n-1).e- such that 0 ≤ exp(r) ≤ (e-1).rnd- is a source for random bits.- Returns:
- r, a random BigDecimal.
-
multiply
public BigDecimal multiply(BigDecimal S)
BigDecimal multiply.- Specified by:
multiplyin interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this*S.
-
sum
public BigDecimal sum(BigDecimal S)
BigDecimal summation.- Specified by:
sumin interfaceAbelianGroupElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this+S.
-
parse
public BigDecimal parse(java.lang.String s)
BigDecimal parse from String.- Specified by:
parsein interfaceElemFactory<BigDecimal>- Parameters:
s- String.- Returns:
- Biginteger from s.
-
parse
public BigDecimal parse(java.io.Reader r)
BigDecimal parse from Reader.- Specified by:
parsein interfaceElemFactory<BigDecimal>- Parameters:
r- Reader.- Returns:
- next Biginteger from r.
-
bitLength
public long bitLength()
Returns the number of bits in the representation of this BigDecimal, including a sign bit. For positive BigDecimal, this is equivalent toval.unscaledValue().bitLength().)- Returns:
- number of bits in the representation of this BigDecimal, including a sign bit.
-
-