|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--ptolemy.math.FixPoint
This class provides a fixed point data type and a set of functions that operate on and return fixed point data. An instance of the class is immutable, meaning that its value is set in the constructor and cannot then be modified. This is similar to the Java built-in classes like Double, Integer, etc.
In an instance of FixPoint, a value is represented by a finite numbers of bits, as defined by the precision. The precision indicates how many bits are used to represent the integer part and the fractional part. The total number of bits used is thus equal to the total number of bits used for the integer and fractional part. The precision of a FixPoint can be expressed in different ways:
The FixPoint class represents signed numbers in a two's-complement format.
Because a fixed point data type uses a finite number of bits to represent a value, a real value is rounded to the nearest number that can be expressed with a given precision of the fixed point, thereby introducing quantization and overflow errors.
In designing the FixPoint class, the main assumption is that all operators work losslessly, i.e. the precision of the result is changed such that no precision loss happens. These changes are different for the various operations.
When the precision of a fixed point value is changed, it might be that the new precision is less than the required precision to losslessly represent the fixed point. If the integer number of bits is changed, overflow may occur. When the fractional number of bits is changed, a quantization error may occur.
Overflows are handled in one of two ways:
The Quantizer class provides a number of convenient static methods for creating an instance of FixPoint. Indeed, the intent is that only those methods will be used to create instances of FixPoint. For this reason, this class has only a package friendly constructor.
The code for this FixPoint implementation is written from scratch. At its core, it uses the Java class BigInteger to represent the finite value captured in FixPoint. The use of the BigInteger class makes this FixPoint implementation truly platform independent. In some cases FixPoint also makes use of the BigDecimal class. Note that the FixPoint does not put any restrictions on the maximum number of bits used to represent a value.
Precision,
Quantizer, Serialized Form| Inner Class Summary | |
static class |
FixPoint.Error
Instances of this class represent a type safe enumeration of error conditions of the FixValue. |
| Field Summary | |
static FixPoint.Error |
NOOVERFLOW
Indicator that an overflow has occurred |
static FixPoint.Error |
OVERFLOW
Indicator that no overflow has occurred |
static FixPoint.Error |
ROUNDING
Indicator that a rounding error has occurred |
| Method Summary | |
FixPoint |
abs()
Return a new FixPoint with a value equal to the absolute value of this fixed point. |
FixPoint |
add(FixPoint arg)
Return a new FixPoint with a value equal to the sum of this FixPoint and the argument. |
java.math.BigDecimal |
bigDecimalValue()
Return the value of this FixPoint as a BigDecimal number. |
FixPoint |
divide(FixPoint arg)
Return a new FixPoint with a value equal to the division of this FixPoint by the argument. |
double |
doubleValue()
Return the value of this FixPoint as a double. |
boolean |
equals(FixPoint arg)
Return true if this FixPoint is equal to the argument. |
FixPoint.Error |
getError()
Get the error condition of this FixPoint, which is one of OVERFLOW, ROUNDING, or NOOVERFLOW indicating that an overflow error has occurred, a rounding error has occurred, or no error has occurred. |
Precision |
getPrecision()
Return the precision of this number. |
FixPoint |
multiply(FixPoint arg)
Return a new FixPoint number with a value equal to the product of this number and the argument. |
void |
printFix()
Print useful debug information about the FixPoint to standard out. |
FixPoint |
subtract(FixPoint arg)
Return a new FixPoint number with a value equal to this number minus the argument. |
java.lang.String |
toBitString()
Return a bit string representation of this fixed point in the form "integerBits . |
java.lang.String |
toString()
Return a string representation of the value of this number. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
public static final FixPoint.Error OVERFLOW
public static final FixPoint.Error NOOVERFLOW
public static final FixPoint.Error ROUNDING
| Method Detail |
public final FixPoint abs()
public FixPoint add(FixPoint arg)
The precision of the result is equal to the maximum of the integer part and fractional part of the fixed point values added. So when a number with precision (6, 3) and (12.4) are added, the resulting precision is equal to (max(6, 12), max(3, 4)), which is (12.4). If the resulting value doesn't fits into this new precision, then the precision is changed to accommodate it. In particular, the number of bits for the integer part might be increased by one.
arg - A number to add to this one.public java.math.BigDecimal bigDecimalValue()
public FixPoint divide(FixPoint arg)
arg - A FixPoint.public double doubleValue()
public boolean equals(FixPoint arg)
toBitString()public FixPoint.Error getError()
public Precision getPrecision()
public FixPoint multiply(FixPoint arg)
arg - The number to multiply this number by.public void printFix()
public FixPoint subtract(FixPoint arg)
arg - The number to subtract from this number.public java.lang.String toBitString()
public java.lang.String toString()
toString in class java.lang.Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||