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>, Serializable, Comparable<BigDecimal>
public final class BigDecimal
extends 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.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal MathContextstatic final MathContextstatic final intstatic final booleanIf true, then use equals from java.math.BigDecimal, else use hacked approximate compareTo().static final BigDecimalThe constant 1.private static final Randomfinal BigDecimalThe data structure.static final BigDecimalThe constant 0. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for BigDecimal without parameters.BigDecimal(double a) Constructor for BigDecimal from double.BigDecimal(double a, MathContext mc) Constructor for BigDecimal from double and a context.BigDecimal(long a) Constructor for BigDecimal from long.BigDecimal(long a, MathContext mc) Constructor for BigDecimal from long and a context.Constructor for BigDecimal from BigRational.BigDecimal(BigRational a, MathContext mc) Constructor for BigDecimal from BigRational.BigDecimal(String s) Constructor for BigDecimal from String.BigDecimal(String s, MathContext mc) Constructor for BigDecimal from String.Constructor for BigDecimal from math.BigDecimal.BigDecimal(BigDecimal a, MathContext mc) Constructor for BigDecimal from math.BigDecimal.Constructor for BigDecimal from java.math.BigInteger.BigDecimal(BigInteger a, MathContext mc) Constructor for BigDecimal from java.math.BigInteger. -
Method Summary
Modifier and TypeMethodDescriptionabs()Absolute value of this.longReturns the number of bits in the representation of this BigDecimal, including a sign bit.Characteristic of this ring.intCompare to BigDecimal b.intCompare absolute to BigDecimal b.intCompare to relative BigDecimal b.copy()Clone this.copy(BigDecimal c) Copy BigDecimal element c.divide(BigDecimal S) BigDecimal divide.doubleGet this decimal as a double.egcd(BigDecimal S) BigDecimal extended greatest common divisor.booleanComparison with any other object.factory()Get the corresponding element factory.fromInteger(long a) Get a BigDecimal element from long.Get a BigDecimal element from a math.BigDecimal.gcd(BigDecimal S) BigDecimal greatest common divisor.Get a list of the generating elements.Get the decimal representation.getONE()Get the one element.Get the rational representation.getZERO()Get the zero element.inthashCode()Hash code for this BigDecimal.inverse()Integer inverse.booleanQuery if this ring is associative.booleanQuery 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.BigDecimal multiply.negate()Negate this.BigDecimal parse from Reader.BigDecimal parse from String.BigDecimal compute quotient and remainder.random(int n) BigDecimal random.random(int n, int e) BigDecimal random.BigDecimal random.BigDecimal random.BigDecimal remainder.intsignum()signum.BigDecimal subtract.sum(BigDecimal S) BigDecimal summation.toScript()Get a scripting compatible string representation.Get a scripting compatible string representation of the factory.toString()Get the String representation.static BigDecimalvalueOf(long a) Get a BigDecimal element from long.static BigDecimalGet a BigDecimal element from a math.BigDecimal.Methods inherited from interface ElemFactory
valueOfMethods inherited from interface MonoidElem
leftDivide, leftRemainder, power, rightDivide, rightRemainder, twosidedDivide, twosidedRemainder
-
Field Details
-
val
The data structure. -
context
-
DEFAULT_CONTEXT
-
DEFAULT_PRECISION
public static final int DEFAULT_PRECISION -
random
-
EXACT_EQUAL
public static final boolean EXACT_EQUALIf true, then use equals from java.math.BigDecimal, else use hacked approximate compareTo().- See Also:
-
ZERO
The constant 0. -
ONE
The constant 1.
-
-
Constructor Details
-
BigDecimal
Constructor for BigDecimal from math.BigDecimal.- Parameters:
a- java.math.BigDecimal.
-
BigDecimal
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
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
Constructor for BigDecimal from double and a context.- Parameters:
a- double.mc- MathContext.
-
BigDecimal
Constructor for BigDecimal from java.math.BigInteger.- Parameters:
a- java.math.BigInteger.
-
BigDecimal
Constructor for BigDecimal from java.math.BigInteger.- Parameters:
a- java.math.BigInteger.mc- MathContext.
-
BigDecimal
Constructor for BigDecimal from BigRational.- Parameters:
a- edu.jas.arith.BigRational.
-
BigDecimal
Constructor for BigDecimal from BigRational.- Parameters:
a- edu.jas.arith.BigRational.mc- MathContext.
-
BigDecimal
-
BigDecimal
Constructor for BigDecimal from String.- Parameters:
s- String.mc- MathContext.
-
BigDecimal
public BigDecimal()Constructor for BigDecimal without parameters.
-
-
Method Details
-
factory
Get the corresponding element factory.- Specified by:
factoryin interfaceElement<BigDecimal>- Returns:
- factory for this Element.
- See Also:
-
generators
Get a list of the generating elements.- Specified by:
generatorsin interfaceElemFactory<BigDecimal>- Returns:
- list of generators for the algebraic structure.
- See Also:
-
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:
-
copy
Clone this.- Specified by:
copyin interfaceElement<BigDecimal>- Returns:
- Creates and returns a copy of this Element.
- See Also:
-
copy
Copy BigDecimal element c.- Specified by:
copyin interfaceElemFactory<BigDecimal>- Parameters:
c- BigDecimal.- Returns:
- a copy of c.
-
getZERO
Get the zero element.- Specified by:
getZEROin interfaceAbelianGroupFactory<BigDecimal>- Returns:
- 0.
-
getONE
Get the one element.- Specified by:
getONEin interfaceMonoidFactory<BigDecimal>- Returns:
- 1.
-
getDecimal
-
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
Characteristic of this ring.- Specified by:
characteristicin interfaceRingFactory<BigDecimal>- Returns:
- characteristic of this ring.
-
fromInteger
Get a BigDecimal element from a math.BigDecimal.- Specified by:
fromIntegerin interfaceElemFactory<BigDecimal>- Parameters:
a- math.BigDecimal.- Returns:
- a as BigDecimal.
-
valueOf
Get a BigDecimal element from a math.BigDecimal.- Parameters:
a- math.BigDecimal.- Returns:
- a as BigDecimal.
-
fromInteger
Get a BigDecimal element from long.- Specified by:
fromIntegerin interfaceElemFactory<BigDecimal>- Parameters:
a- long.- Returns:
- a as BigDecimal.
-
valueOf
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:
-
isONE
public boolean isONE()Is BigDecimal number one.- Specified by:
isONEin interfaceMonoidElem<BigDecimal>- Returns:
- true if this is 1, else false.
- See Also:
-
isUnit
public boolean isUnit()Is BigDecimal number unit.- Specified by:
isUnitin interfaceMonoidElem<BigDecimal>- Returns:
- true if this is a unit, else false.
- See Also:
-
toString
-
doubleValue
public double doubleValue()Get this decimal as a double.- Returns:
- the decimal as a double
- See Also:
-
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:
-
toScriptFactory
Get a scripting compatible string representation of the factory.- Specified by:
toScriptFactoryin interfaceElement<BigDecimal>- Returns:
- script compatible representation for this ElemFactory.
- See Also:
-
compareTo
Compare to BigDecimal b. Experimental, is hacked.- Specified by:
compareToin interfaceComparable<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
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
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
-
hashCode
public int hashCode()Hash code for this BigDecimal.- Specified by:
hashCodein interfaceElement<BigDecimal>- Overrides:
hashCodein classObject- Returns:
- the hashCode.
- See Also:
-
abs
Absolute value of this.- Specified by:
absin interfaceAbelianGroupElem<BigDecimal>- Returns:
- |this|.
- See Also:
-
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:
-
subtract
BigDecimal subtract.- Specified by:
subtractin interfaceAbelianGroupElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this-S.
-
divide
BigDecimal divide.- Specified by:
dividein interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this/S.
-
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:
-
remainder
BigDecimal remainder.- Specified by:
remainderin interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this - (this/S)*S.
-
quotientRemainder
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
BigDecimal greatest common divisor.- Specified by:
gcdin interfaceRingElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- gcd(this,S).
-
egcd
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
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
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
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
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
BigDecimal multiply.- Specified by:
multiplyin interfaceMonoidElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this*S.
-
sum
BigDecimal summation.- Specified by:
sumin interfaceAbelianGroupElem<BigDecimal>- Parameters:
S- BigDecimal.- Returns:
- this+S.
-
parse
BigDecimal parse from String.- Specified by:
parsein interfaceElemFactory<BigDecimal>- Parameters:
s- String.- Returns:
- BigDecimal from s.
-
parse
BigDecimal parse from Reader.- Specified by:
parsein interfaceElemFactory<BigDecimal>- Parameters:
r- Reader.- Returns:
- next BigDecimal 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.
-