public class FixedBin extends java.lang.Number implements java.lang.Comparable<FixedBin>
This represents a PL/I "FIXED BINARY PRECISION(p,q)" datatype. A FIXED BIN(p,q) means that it has a "p" total binary digits, "q" of which are to the right of the "binary point". Values will be stored internally to FixedBin as a "long" for values up to 63 bits, e.g., FIXED BIN(15), FIXED BIN(31), FIXED BIN(63,20) all will use "long" arithmetic where possible. Larger values than 63 will use BigInteger internally which is of arbitrary size.
For example, a FIXED BIN(5,3) in PL/I is a FixedBin with 5 digits and scale factor of 3. Such a value can be created with the "valueOf()" static method call as in,
FixedBin A = FixedBin.valueOf("10.111B");
This method call will create an immutable object of 5 digits and 3 scale. This represents the binary value 10.111B or decimal value 2.875 ("2 + 2**-1 + 2**-2 + 2**-3" or "2 + 1/2 + 1/4 + 1/8"). Within FixedBin this is represented with a long of value "10111B" or "23". To create a specific sized variable, say of 15 digits 5 precision, use
FixedBin B = FixedBin.valueOf(2.875, 15, 5);which is the same as
FixedBin B = new FixedBin("10.111B", 15, 5);
A major problem with FIXED BIN arithmetic are overflows and loss of precision. long can hold arbitrary numbers of binary digits, solving overflows. Precision is handled FixedBin by expanding the result of a arithmetic value (e.g., multiply() or divide() to accommodate the sum of the two scales. The assignment of an expression of the above,
FixedBin C = A.multiply(B); // multiply 2.875*2.87500
results in a a FIXED BIN(14,10). Multiplying quantities of equal precision will also expand the scale. For example, FIXED BIN(5,3) values 10.001B and 10.001B multiplied will give 100.000001B, a FIXED BIN(9,6). OVERFLOW or UNDERFLOW (PL/I) conditions will be raised if the result is scaled down to the original FIXED BIN(5,3) precision which can be attempted with
FixedBin A = new FixedBin("10.111B", 5, 3); FixedBin B = new FixedBin("10.111B", 5, 3); FixedBin C = new FixedBin(A.multiply(B), 5, 3); // throws Java Conditions for various PL/I CONVERSION, OVERFLOW or UNDERFLOW conditions
FixedBin, like Long, Integer or other wrappered types, are immutable. They can't be changed by can be converted to other FixedBin (or other forms like Double, String) as a result of arithmetic (multiply()) and have the the same interfaces as those wrapper classes (intValue(), longValue(), valueOf(), toString(), etc)
Modifier and Type | Field and Description |
---|---|
static FixedBin |
ONE |
static FixedBin |
TEN |
static FixedBin |
ZERO |
Constructor and Description |
---|
FixedBin(java.math.BigInteger cpyv,
int cpyd,
int cpys)
Piecemeal, non-converting, copy constructor.
|
FixedBin(byte[] val)
Construct a FixedBin from a byte array containing
the twos-complement binary rep of a FixedBin into
a FixedBin, of scale 0.
|
FixedBin(byte[] val,
int digits,
int scale)
Construct a FixedBin from a byte array containing a twos-compliment
binary representation of a FixedBin with a given precision and scale
|
FixedBin(FixedBin cpy)
Copy constructor creates a duplicate of another FixedBin
|
FixedBin(FixedBin val,
int digits,
int scale)
Construct a FixedBin of one scale to another
of the same or different scale
|
FixedBin(int signum,
byte[] val)
Construct a 0-scale FixedBin number
|
FixedBin(int bitLength,
int certainty,
java.util.Random rnd)
Constructs a randomly generated positive
0-scale FixedBin that is probably prime, with the
specified bitLength.
|
FixedBin(int numBits,
java.util.Random rnd)
Construct a randomly generated
positive 0-scale FixedBin
|
FixedBin(long cpyv,
int cpyd,
int cpys)
piecemeal, non-converting, copy constructor
|
FixedBin(java.lang.String val)
Construct the decimal String representation
of a FixedBin into a n-scale FixedBin.
|
FixedBin(java.lang.String val,
int radix)
Construct a FixedBin from a the String representation in radix
into a 0-scale FixedBin.
|
FixedBin(java.lang.String val,
int digits,
int scale)
Construct the decimal String representation
of a FixedBin into a n-scale FixedBin.
|
Modifier and Type | Method and Description |
---|---|
FixedBin |
add(FixedBin x)
add - add two FixedBin numbers returning the result
|
java.math.BigInteger |
bigIntegerValue()
bigIntegerValue() - return a big integer from a FixedBin number
|
boolean |
booleanValue()
Convert to boolean
|
byte |
byteValue()
Convert to byte, truncating any digits to the right of the binary point
|
byte |
byteValueExact()
Convert to short, truncating any digits to the right of the binary point
|
static int |
compare(FixedBin x,
FixedBin y)
compare - static compare two FixedBin values
|
int |
compareTo(FixedBin y)
compareTo - compare two FixedBin values after adjusting to the higher
precision and return he value 0 if this FixedBin is equal to the
argument FixedBin; a value less than 0 if this FixedBin is numerically
less than the argument FixedBin; and a value greater than 0 if this
FixedBin is numerically greater than the argument FixedBin (signed comparison).
|
int |
digits()
Return the number of defined or adjusted by way of computations
|
FixedBin |
divide(FixedBin x)
divide - divide a FixedBin number by another and return the result
|
double |
doubleValue()
Convert to double
|
boolean |
equals(java.lang.Object y)
equals - Compares this object to the specified object.
|
float |
floatValue()
Convert to float
|
void |
fromBytes(byte[] bytes)
fromBytes - convert a byte array (likely generated by toBytes())
back into a FixedBin
|
int |
getSize()
the size "if converted to an array of bytes" is calculated
by converting it to bytes
|
int |
intValue()
Convert to int, truncating any digits to the right of the binary point
|
int |
intValueExact()
Convert to int, truncating any digits to the right of the
binary point and throwing exceptions if precision is lost
|
long |
longValue()
Convert to long, truncating any digits to the right of the binary point
|
long |
longValueExact()
Convert to long, truncating any digits to the right of the
binary point and throwing exceptions if precision is lost
|
static void |
main(java.lang.String[] args)
main -- FixedBin unit test
|
FixedBin |
multiply(FixedBin x)
multiply - multiply a FixedBin number by another and return the result
|
FixedBin |
negate()
negate - negate a FixedBin number
|
int |
precision()
Return the number of defined or adjusted by way of computations
|
int |
scale()
Return the scale factor defined or adjusted by way of computations
|
short |
shortValue()
Convert to short, truncating any digits to the right of the binary point
|
short |
shortValueExact()
Convert to short, truncating any digits to the right of the binary point
|
int |
size()
size - return the size in bytes if this FixedBin were to be
converted toBytes()
|
FixedBin |
subtract(FixedBin x)
subtract - subtract a FixedBin number from this one and return the result
|
byte[] |
toByteArray()
toByteArray - synonym for toBytes()
|
byte[] |
toBytes()
toBytes - generate a byte array of the internal data value of the
variable of variable size, not including 0s or -1s indicating sign.
|
java.lang.String |
toString()
Convert to String
|
java.lang.String |
toString(int radix)
Convert to String with radix
|
static FixedBin |
valueOf(java.math.BigDecimal val)
Factory method to convert from BigDecimal with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(java.math.BigDecimal val,
int digits,
int scale)
Factory method to convert from BigDecimal with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(byte val)
Factory method to convert from byte (with implied scale 0) to FixedBin scale 0.
|
static FixedBin |
valueOf(byte[] val)
Factory method to convert from byte array with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(byte[] val,
int digits,
int scale)
Factory method to convert from byte array with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(byte val,
int digits,
int scaling)
Factory method to convert from byte
(with implied scale 0) FixedBin with a specific number of digits and scale
|
static FixedBin |
valueOf(double val)
Factory method to convert from double
(with inherent scale) to FixedBin
|
static FixedBin |
valueOf(double val,
int digits,
int scale)
Factory method to convert from double with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(float val)
Factory method to convert from float
(with inherent scale) to FixedBin
|
static FixedBin |
valueOf(float val,
int digits,
int scale)
Factory method to convert from float with specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(int val)
Factory method to convert from int (with implied scale 0) to FixedBin scale 0.
|
static FixedBin |
valueOf(int val,
int digits,
int scaling)
Factory method to convert from int
(with implied scale 0) FixedBin with a specific number of digits and scale
|
static FixedBin |
valueOf(long val)
Factory method to convert from long (with implied scale 0) to FixedBin scale 0.
|
static FixedBin |
valueOf(long val,
int digits,
int scaling)
Factory method to convert from long
(with implied scale 0) FixedBin with a specific number of digits and scale
|
static FixedBin |
valueOf(java.lang.Object val,
int... pq)
Factory method to convert an arbitrary Object (instanceof one of the
other valueOf() methods) to FixedBin
|
static FixedBin |
valueOf(short val)
Factory method to convert from short (with implied scale 0) to FixedBin scale 0.
|
static FixedBin |
valueOf(short val,
int digits,
int scaling)
Factory method to convert from short
(with implied scale 0) FixedBin with a specific number of digits and scale
|
static FixedBin |
valueOf(java.lang.String val)
Factory method to convert from String without specific precision and scale
to FixedBin
|
static FixedBin |
valueOf(java.lang.String val,
int digits,
int scale)
Factory method to convert from String with specific precision and scale
to FixedBin
|
public static final FixedBin ZERO
public static final FixedBin ONE
public static final FixedBin TEN
public FixedBin(byte[] val)
val
- - the byte array to usepublic FixedBin(byte[] val, int digits, int scale)
val
- the byte array valuedigits
- - the number of digits of precisionscale
- - the number of digits to the right of the decimal pointpublic FixedBin(int signum, byte[] val)
signum
- - translate the sign-magnitude rep of
a FixedBin into a FixedBinval
- - the byte array valuepublic FixedBin(int bitLength, int certainty, java.util.Random rnd)
BigInteger
bitLength
- - number of bitscertainty
- - not usedrnd
- - random numberpublic FixedBin(int numBits, java.util.Random rnd)
numBits
- - number of bitsrnd
- - random numberpublic FixedBin(java.lang.String val)
val
- - the string rep of the FixedBin numberpublic FixedBin(java.lang.String val, int digits, int scale)
val
- - the string rep of the FixedBin numberdigits
- - the number of digits of precisionscale
- - the number of digits to the right of the decimal pointpublic FixedBin(java.lang.String val, int radix)
val
- - the string rep of the bits to convertradix
- - and radixpublic FixedBin(FixedBin val, int digits, int scale)
val
- - another fixed bindigits
- - the new defined number of digits (an arithmetic operation involving
the variable, like adding 0 to it, might reduce the result's
number of digitsscale
- - the new scale, either higher or lower or same precisionCondition.OVERFLOW
- on OVERFLOW (the number of digits is too small to
hold the result) or UNDERFLOW (the scale is not big enough to
hold the result and results in truncationpublic FixedBin(FixedBin cpy)
public FixedBin(java.math.BigInteger cpyv, int cpyd, int cpys)
cpyv
- - the unscaled BigInteger value to assigncpyd
- - the number of digits (in bits)cpys
- - the scale (in bits to the right of the binary point)
Note: Not to be used to get a FixedBin from a BigInteger, use
FixedBin.valueOf(big-int.longValue(), num-digits, scale-factor)public FixedBin(long cpyv, int cpyd, int cpys)
cpyv
- - the long value to assigncpyd
- - the number of digits (in bits)cpys
- - the scale (in bits to the right of the binary point)
Note: Not to be used to get a FixedBin of a regular long with digits and scale,
use FixedBin.valueOf(long-number, number-of-digits, scaling-factor)
DCL A FIXED BIN(15,5) INIT(1234) >> FixedBin.valueOf(1234,15,5)public static FixedBin valueOf(long val)
val
- - long value to be used to set the value of the FixedBinpublic static FixedBin valueOf(int val)
val
- - int value to be used to set the value of the FixedBinpublic static FixedBin valueOf(short val)
val
- - short value to be used to set the value of the FixedBinpublic static FixedBin valueOf(byte val)
val
- - long value to be used to set the value of the FixedBinpublic static FixedBin valueOf(long val, int digits, int scaling)
val
- - long value to be used to set the value of the FixedBindigits
- - the defined number of binary digits to use (< LONGDIGITS)scaling
- - the defined binary scaling factorDCL A FIXED BIN(15,5) INIT(1234)
actually only needs FIXED BIN(10,5)public static FixedBin valueOf(int val, int digits, int scaling)
val
- - int value to be used to set the value of the FixedBindigits
- - the defined number of binary digits to use (< LONGDIGITS)scaling
- - the defined binary scaling factorDCL A FIXED BIN(15,5) INIT(1234)
actually only needs FIXED BIN(10,5)public static FixedBin valueOf(short val, int digits, int scaling)
val
- - short value to be used to set the value of the FixedBindigits
- - the defined number of binary digits to use (< LONGDIGITS)scaling
- - the defined binary scaling factorDCL A FIXED BIN(15,5) INIT(1234)
actually only needs FIXED BIN(10,5)public static FixedBin valueOf(byte val, int digits, int scaling)
val
- - byte value to be used to set the value of the FixedBindigits
- - the defined number of binary digits to use (< LONGDIGITS)scaling
- - the defined binary scaling factorDCL A FIXED BIN(15,5) INIT(1234)
actually only needs FIXED BIN(10,5)public static FixedBin valueOf(double val)
val
- - double value to be used to set the value of the FixedBinpublic static FixedBin valueOf(float val)
val
- - float value to be used to set the value of the FixedBinpublic static FixedBin valueOf(double val, int digits, int scale)
val
- - double to be used to set the value of the FixedBindigits
- - the number of binary digits of precisionscale
- - the number of binary digits to the right of the decimal pointpublic static FixedBin valueOf(float val, int digits, int scale)
val
- - float to be used to set the value of the FixedBindigits
- - the number of binary digits of precisionscale
- - the number of binary digits to the right of the decimal pointpublic static FixedBin valueOf(java.math.BigDecimal val)
val
- - BigDecimal to be used to set the value of the FixedBinpublic static FixedBin valueOf(java.math.BigDecimal val, int digits, int scale)
val
- - BigDecimal to be used to set the value of the FixedBindigits
- - the number of binary digits of precisionscale
- - the number of binary digits to the right of the decimal pointpublic static FixedBin valueOf(byte[] val)
val
- - byte array to be used to set the value of the FixedBinpublic static FixedBin valueOf(byte[] val, int digits, int scale)
val
- - byte array to be used to set the value of the FixedBindigits
- - the number of digits of precisionscale
- - the number of digits to the right of the decimal pointpublic static FixedBin valueOf(java.lang.String val, int digits, int scale)
val
- - string to be used to set the value of the FixedBindigits
- - the number of digits of precisionscale
- - the number of digits to the right of the decimal pointpublic static FixedBin valueOf(java.lang.String val)
val
- - string to be used to set the value of the FixedBinpublic static FixedBin valueOf(java.lang.Object val, int... pq)
val
- - string to be used to set the value of the FixedBinpq
- - zero to 2 ints giving the precision and the scale, if providedpublic int digits()
public int precision()
public int scale()
public long longValue()
longValue
in class java.lang.Number
public long longValueExact()
-Condition.CONDITION.CONVERSION
- if it can't be converted to a long-Condition.CONDITION.OVERFLOW
- if it can't fit in a longpublic int intValue()
intValue
in class java.lang.Number
public int intValueExact()
-Condition.CONDITION.CONVERSION
- if it can't be converted to an int-Condition.CONDITION.OVERFLOW
- if it can't fit in an intpublic short shortValue()
shortValue
in class java.lang.Number
public short shortValueExact()
-Condition.CONDITION.CONVERSION
- if it can't be converted to a short-Condition.CONDITION.OVERFLOW
- if it can't fit in a shortpublic byte byteValue()
byteValue
in class java.lang.Number
public byte byteValueExact()
-Condition.CONDITION.CONVERSION
- if it can't be converted to a byte-Condition.CONDITION.OVERFLOW
- if it can't fit in a bytepublic java.math.BigInteger bigIntegerValue()
public double doubleValue()
doubleValue
in class java.lang.Number
public float floatValue()
floatValue
in class java.lang.Number
public boolean booleanValue()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(int radix)
radix
- - the radix to convert topublic FixedBin negate()
public FixedBin add(FixedBin x)
x
- the FixedBin to add to thispublic FixedBin subtract(FixedBin x)
x
- the FixedBin to subtract to thispublic FixedBin multiply(FixedBin x)
x
- the FixedBin to multiply to thispublic FixedBin divide(FixedBin x)
x
- the FixedBin to divide into thispublic int compareTo(FixedBin y)
compareTo
in interface java.lang.Comparable<FixedBin>
y
- the FixedBin to compare against to thispublic static int compare(FixedBin x, FixedBin y)
x
- the FixedBin to compare against to thispublic boolean equals(java.lang.Object y)
equals
in class java.lang.Object
y
- the FixedBin to compare against to thispublic int size()
public byte[] toBytes()
public byte[] toByteArray()
public int getSize()
public void fromBytes(byte[] bytes)
bytes
- - the byte array to convert into this FixedBinpublic static void main(java.lang.String[] args)
args
- - sequence of either decimal or
binary numbers and operators, first determines
the size
1011.011B + 1.5