|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ptolemy.math.Precision
public class Precision
This class defines the precision of a signed or unsigned fixed point value. The precision of a fixed point value is represented as internally as three fields: the number of bits (n), the binary exponent (e), and a sign bit (s). The number of bits (n) defines the dynamic range of the format (2n). The exponent (e) determines the placement of the binary point for all values of this format. This binary point placement is "fixed" and is specified within the precision format rather than within individual Fixed point values. The mantissa bits of all fixed point values using this this precision format are multiplied by 2m. Note that m can take on a positive, negative, or zero value. This allows the binary point to be placed anywhere necessary and even beyond the bounds of the mantissa (i.e. to the right of the lsb or to the left of the msb). The sign value s is used to indicate whether the fixed point precision value is treated as signed (s = 1) or unsigned (s = 0).
Signed fixed point formats are represented in a 2's complement format. As a consequence, a single bit is used to represent the sign of the number. The value of a multi-bit signed number is:
Signed(B) = 2^m x (- b_{n-1} x 2^{n-1} + \sum_{i=0}^{n-2} b_i x 2^i), where b_i is the value of bit i of the bit vector.
Unsigned fixed formats are represented as unsigned binary values. No bits are used to represent the sign and all values are positive. The actual value of a multi-bit unsigned fixed point number is:
Unsigned(B) = 2^m x (\sum_{i=0}^{n-1} b_i x 2^i)
This class supports several different String formats for specifying and
displaying the Precision. These String formats are supported by several
classes that extend the Precision.PrecisionFormat
class. Several such classes
have been created. Any given precision string format can be represented in
any of the valid precision string formats. The four supported Precision
String formats are shown in the table below. A static singleton object of
each of these formats is provided in this class. Each String Representation
column of the table represent equivalent Precision formats:
Format Name | Format Spec | ||||||
Precision.IntegerFractionPrecisionFormat |
[integer bits].[fraction bits] | 3.2 | 0.7 | -2.12 | 12.-4 | 32.0 | U1.7 |
Precision.LengthIntegerPrecisionFormat |
[total bits]/[integer bits] | 5/3 | 7/0 | 10/-2 | 8/12 | 32/32 | U8/1 |
Precision.VHDLPrecisionFormat |
[MSb position]:[LSb position] | 2:-2 | -1:-7 | -3:-12 | 11:4 | 31:0 | U0:-7 |
Precision.LengthExponentPrecisionFormat | \
[total bits]e[binary point position] | 5e-2 | 7e-7 | 10e-12 | 8e4 | 32e0 | U8e-7 |
An instance of the class is immutable, meaning that its value is set in the constructor and cannot then be modified.
FixPoint
,
Serialized Form
Red (kienhuis) |
Yellow (kienhuis) |
Nested Class Summary | |
---|---|
static class |
Precision.ExpressionLanguagePrecisionFormat
Precision format for use with the Expression Language. |
static class |
Precision.IntegerFractionPrecisionFormat
Defines a Precision string format using the INTEGER.FRACTION precision format. |
static class |
Precision.LengthExponentPrecisionFormat
Defines a Precision string format using the LENGTHeEXPONENT precision format. |
static class |
Precision.LengthIntegerPrecisionFormat
Defines a Precision string format using the LENGTH/INTEGER precision format. |
static class |
Precision.PrecisionFormat
Defines a String format for specifying a Precision object. |
static class |
Precision.VHDLPrecisionFormat
Defines a Precision string format using the VHDL MSB:LSB precision format. |
Field Summary | |
---|---|
private int |
_exponent
The exponent for all values of this type. |
private Precision.PrecisionFormat |
_format
The precision format used for parsing/printing precision information. |
private int |
_length
The total number of bits. |
private int |
_sign
The presence of a sign bit (0 = unsigned, 1 = signed) |
private static java.math.BigDecimal[] |
_twoRaisedTo
Calculate the table containing 2^x, with 0 <= x < TWORAISEDTOSIZE. |
static Precision.PrecisionFormat |
EXPRESSION_LANGUAGE
static ExpressionLanguagePrecisionFormat object. |
static Precision.PrecisionFormat |
INTEGER_FRACTION
static IntegerFractionPrecisionFormat object. |
static Precision.PrecisionFormat |
LENGTH_EXPONENT
static LengthExponentPrecisionFormat object. |
static Precision.PrecisionFormat |
LENGTH_INTEGER
static LengthIntegerPrecisionFormat object. |
private static int |
TWORAISEDTOSIZE
The size of the pre-computed _twoRaisedTo powers of two array. 65 entries are used to cache all powers of 2 from 0 to 64. |
static Precision.PrecisionFormat |
VHDL
static VHDLPrecisionFormat object. |
Constructor Summary | |
---|---|
Precision(int length,
int integerBits)
Construct a Precision object based on the length/integer bits format. |
|
Precision(int sign,
int length,
int exponent)
Construct a Precision object based on the sign, length, and exponent format. |
|
Precision(java.lang.String str)
Construct a Precision object based on the provided string. |
Method Summary | |
---|---|
private static java.math.BigDecimal |
_getTwoRaisedTo(int number)
Get the BigDecimal which is the 2^exponent. |
java.lang.Object |
clone()
Return this, that is, return the reference to this object. |
boolean |
equals(java.lang.Object object)
Return true if the indicated object is an instance of Precision and the precision format matches exactly. |
java.math.BigDecimal |
findMaximum()
Return the maximum obtainable value in this precision. |
java.math.BigDecimal |
findMinimum()
Return the minimum obtainable value for this precision. |
java.math.BigDecimal |
getEpsilon()
Return the incremental value between discrete values under the given Precision format. |
int |
getExponent()
Return the location of the binary exponent. |
int |
getFractionBitLength()
Return the number of bits representing the fractional part. |
int |
getIntegerBitLength()
Return the number of bits representing the integer part. |
int |
getLeastSignificantBitPosition()
Return the bit position of the least significant bit of the given fixed point precision. |
java.math.BigInteger |
getMaximumUnscaledValue()
Return the maximum integer value before scaling so that quantization levels are represented by adjacent integers. |
java.math.BigInteger |
getMinimumUnscaledValue()
Return the minimum integer value before scaling so that quantization levels are represented by adjacent integers. |
int |
getMostSignificantBitPosition()
Return the bit position of the most significant bit of the given fixed point precision. |
int |
getMostSignificantDataBitPosition()
Return the bit position of the most significant data bit of the given fixed point precision. |
int |
getNumberOfBits()
Return the total number of bits. |
java.math.BigInteger |
getNumberOfLevels()
Return the total number of discrete values possible. |
int |
getSign()
Return the sign (0 = unsigned, 1 = signed). |
int |
hashCode()
Return a hash code value for this Precision. |
boolean |
isSigned()
Determine if the fixed point format is signed or not. |
static java.math.BigDecimal |
shiftBigDecimal(java.math.BigDecimal val,
int shiftval)
Shift the BigDecimal value either right or left by a power of 2 value. |
java.lang.String |
toString()
Return a string representing this precision. |
java.lang.String |
toString(Precision.PrecisionFormat format)
Return a string representation of this format in one of several styles. |
static Precision |
union(Precision precisionA,
Precision precisionB)
Return the precision that is the maximum of the two supplied precisions in both the length and exponent. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Precision.PrecisionFormat INTEGER_FRACTION
public static final Precision.PrecisionFormat LENGTH_EXPONENT
public static final Precision.PrecisionFormat LENGTH_INTEGER
public static final Precision.PrecisionFormat EXPRESSION_LANGUAGE
public static final Precision.PrecisionFormat VHDL
private int _length
private int _exponent
private int _sign
private Precision.PrecisionFormat _format
private static final int TWORAISEDTOSIZE
private static java.math.BigDecimal[] _twoRaisedTo
Constructor Detail |
---|
public Precision(java.lang.String str) throws java.lang.IllegalArgumentException
str
- The string representing the precision.
java.lang.IllegalArgumentException
- If the precision string
supplied does not match one of the known formats.public Precision(int length, int integerBits) throws java.lang.IllegalArgumentException
length
- The total number of bits.integerBits
- The number of integer bits.
java.lang.IllegalArgumentException
- If the given values are
negative or when the integer number of bits is larger than the
total number of bits.public Precision(int sign, int length, int exponent) throws java.lang.IllegalArgumentException
sign
- The presence of a sign bit (1 = signed, 0 = unsigned).length
- The total number of bits.exponent
- The bit location of the exponent.
java.lang.IllegalArgumentException
- If the Precision arguments
are inconsistent.Method Detail |
---|
public java.lang.Object clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- Object to test for equality
public java.math.BigDecimal findMaximum()
getMaximumUnscaledValue()
by 2exponent.
public java.math.BigDecimal findMinimum()
getMinimumUnscaledValue()
by 2exponent.
public java.math.BigDecimal getEpsilon()
public int getExponent()
public int getFractionBitLength()
public int getIntegerBitLength()
public int getLeastSignificantBitPosition()
public java.math.BigInteger getMaximumUnscaledValue()
public java.math.BigInteger getMinimumUnscaledValue()
public int getMostSignificantBitPosition()
public int getMostSignificantDataBitPosition()
public int getNumberOfBits()
public java.math.BigInteger getNumberOfLevels()
public int getSign()
public int hashCode()
hashCode
in class java.lang.Object
public boolean isSigned()
public static Precision union(Precision precisionA, Precision precisionB)
precisionA
- A precisionprecisionB
- Another precision
public static java.math.BigDecimal shiftBigDecimal(java.math.BigDecimal val, int shiftval)
val
- BigDecimal value to shift.shiftval
- Amount of "power of 2" shifting to perform
on the BigDecimal value
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(Precision.PrecisionFormat format)
format
- The String format represented desired for printing.
private static java.math.BigDecimal _getTwoRaisedTo(int number)
number
- the exponent.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |