Package com.mckoi.util
Class BigNumber
- java.lang.Object
-
- java.lang.Number
-
- com.mckoi.util.BigNumber
-
- All Implemented Interfaces:
java.io.Serializable
public final class BigNumber extends java.lang.Number
Extends BigDecimal to allow a number to be positive infinity, negative infinity and not-a-number. This provides compatibility with float and double types.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static java.math.BigDecimal
BD_ZERO
Statics.private java.math.BigDecimal
big_decimal
The BigDecimal representation.static BigNumber
BIG_NUMBER_ONE
static BigNumber
BIG_NUMBER_ZERO
Statics for 0 and 1.private byte
byte_count
If this can be represented as an int or long, this contains the number of bytes needed to represent the number.private long
long_representation
A 'long' representation of this number.static BigNumber
NaN
private static byte
NaN_STATE
private static byte
NEG_INF_STATE
State enumerations.static BigNumber
NEGATIVE_INFINITY
Statics for negative infinity, positive infinity and NaN.private byte
number_state
The state of the number, either 0 for number is the BigDecimal, 1 for negative infinity, 2 for positive infinity and 3 for NaN.private static byte
POS_INF_STATE
static BigNumber
POSITIVE_INFINITY
(package private) static long
serialVersionUID
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigNumber
abs()
BigNumber
add(BigNumber number)
java.math.BigDecimal
asBigDecimal()
Returns the big number as a BigDecimal object.BigNumber
bitWiseOr(BigNumber number)
byte
byteValue()
Returns this big number as a byte.boolean
canBeRepresentedAsInt()
Returns true if this BigNumber can be represented by a 32-bit int (has no scale).boolean
canBeRepresentedAsLong()
Returns true if this BigNumber can be represented by a 64-bit long (has no scale).int
compareTo(BigNumber number)
Compares this BigNumber with the given BigNumber.BigNumber
divide(BigNumber number)
double
doubleValue()
Returns this big number as a double.boolean
equals(java.lang.Object ob)
The equals comparison uses the BigDecimal 'equals' method to compare values.float
floatValue()
Returns this big number as a float.static BigNumber
fromBigDecimal(java.math.BigDecimal val)
Creates a BigNumber from a BigDecimal.static BigNumber
fromData(byte[] buf, int scale, byte state)
Creates a BigNumber from the given data.static BigNumber
fromDouble(double value)
Creates a BigNumber from a double.static BigNumber
fromFloat(float value)
Creates a BigNumber from a float.static BigNumber
fromInt(int value)
Creates a BigNumber from an int.static BigNumber
fromLong(long value)
Creates a BigNumber from a long.static BigNumber
fromString(java.lang.String str)
Creates a BigNumber from a string.private byte
getInverseState()
Returns the inverse of the state.int
getScale()
Returns the scale of this number, or -1 if the number has no scale (if it -inf, +inf or NaN).byte
getState()
Returns the state of this number.int
intValue()
Returns this big number as an int.long
longValue()
Returns this big number as a long.BigNumber
multiply(BigNumber number)
private void
setBigDecimal(java.math.BigDecimal big_decimal)
BigNumber
setScale(int d, int round_enum)
short
shortValue()
Returns this big number as a short.int
signum()
BigNumber
sqrt()
BigNumber
subtract(BigNumber number)
byte[]
toByteArray()
Returns this number as a byte array (unscaled).java.lang.String
toString()
Returns this big number as a string.
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
NEG_INF_STATE
private static final byte NEG_INF_STATE
State enumerations.- See Also:
- Constant Field Values
-
POS_INF_STATE
private static final byte POS_INF_STATE
- See Also:
- Constant Field Values
-
NaN_STATE
private static final byte NaN_STATE
- See Also:
- Constant Field Values
-
number_state
private byte number_state
The state of the number, either 0 for number is the BigDecimal, 1 for negative infinity, 2 for positive infinity and 3 for NaN.
-
big_decimal
private java.math.BigDecimal big_decimal
The BigDecimal representation.
-
long_representation
private long long_representation
A 'long' representation of this number.
-
byte_count
private byte byte_count
If this can be represented as an int or long, this contains the number of bytes needed to represent the number.
-
BD_ZERO
private static final java.math.BigDecimal BD_ZERO
Statics.
-
NEGATIVE_INFINITY
public static final BigNumber NEGATIVE_INFINITY
Statics for negative infinity, positive infinity and NaN.
-
POSITIVE_INFINITY
public static final BigNumber POSITIVE_INFINITY
-
NaN
public static final BigNumber NaN
-
BIG_NUMBER_ZERO
public static final BigNumber BIG_NUMBER_ZERO
Statics for 0 and 1.
-
BIG_NUMBER_ONE
public static final BigNumber BIG_NUMBER_ONE
-
-
Method Detail
-
setBigDecimal
private void setBigDecimal(java.math.BigDecimal big_decimal)
-
canBeRepresentedAsLong
public boolean canBeRepresentedAsLong()
Returns true if this BigNumber can be represented by a 64-bit long (has no scale).
-
canBeRepresentedAsInt
public boolean canBeRepresentedAsInt()
Returns true if this BigNumber can be represented by a 32-bit int (has no scale).
-
getScale
public int getScale()
Returns the scale of this number, or -1 if the number has no scale (if it -inf, +inf or NaN).
-
getState
public byte getState()
Returns the state of this number. Returns either 1 which indicates negative infinity, 2 which indicates positive infinity, or 3 which indicates NaN.
-
getInverseState
private byte getInverseState()
Returns the inverse of the state.
-
toByteArray
public byte[] toByteArray()
Returns this number as a byte array (unscaled).
-
toString
public java.lang.String toString()
Returns this big number as a string.- Overrides:
toString
in classjava.lang.Object
-
doubleValue
public double doubleValue()
Returns this big number as a double.- Specified by:
doubleValue
in classjava.lang.Number
-
floatValue
public float floatValue()
Returns this big number as a float.- Specified by:
floatValue
in classjava.lang.Number
-
longValue
public long longValue()
Returns this big number as a long.- Specified by:
longValue
in classjava.lang.Number
-
intValue
public int intValue()
Returns this big number as an int.- Specified by:
intValue
in classjava.lang.Number
-
shortValue
public short shortValue()
Returns this big number as a short.- Overrides:
shortValue
in classjava.lang.Number
-
byteValue
public byte byteValue()
Returns this big number as a byte.- Overrides:
byteValue
in classjava.lang.Number
-
asBigDecimal
public java.math.BigDecimal asBigDecimal()
Returns the big number as a BigDecimal object. Note that this throws an arith error if this number represents NaN, +Inf or -Inf.
-
compareTo
public int compareTo(BigNumber number)
Compares this BigNumber with the given BigNumber. Returns 0 if the values are equal, >0 if this is greater than the given value, and < 0 if this is less than the given value.
-
equals
public boolean equals(java.lang.Object ob)
The equals comparison uses the BigDecimal 'equals' method to compare values. This means that '0' is NOT equal to '0.0' and '10.0' is NOT equal to '10.00'. Care should be taken when using this method.- Overrides:
equals
in classjava.lang.Object
-
abs
public BigNumber abs()
-
signum
public int signum()
-
setScale
public BigNumber setScale(int d, int round_enum)
-
sqrt
public BigNumber sqrt()
-
fromDouble
public static BigNumber fromDouble(double value)
Creates a BigNumber from a double.
-
fromFloat
public static BigNumber fromFloat(float value)
Creates a BigNumber from a float.
-
fromLong
public static BigNumber fromLong(long value)
Creates a BigNumber from a long.
-
fromInt
public static BigNumber fromInt(int value)
Creates a BigNumber from an int.
-
fromString
public static BigNumber fromString(java.lang.String str)
Creates a BigNumber from a string.
-
fromBigDecimal
public static BigNumber fromBigDecimal(java.math.BigDecimal val)
Creates a BigNumber from a BigDecimal.
-
fromData
public static BigNumber fromData(byte[] buf, int scale, byte state)
Creates a BigNumber from the given data.
-
-