11 Fixed Point Numbers
Ptolemy II includes a preliminary fixed
point data type. We represent a fixed point value in the expression
language using the following format:
fix(value, totalBits, integerBits)
Thus, a fixed point value of 5.375 that uses 8 bit precision of which
4 bits are used to represent the (signed) integer part can be
represented as:
fix(5.375, 8, 4)
The value can also be a matrix of doubles. The values are rounded,
yielding the nearest value representable with the specified
precision. If the value to represent is out of range, then it is
saturated, meaning that the maximum or minimum fixed point value is
returned, depending on the sign of the specified value. For example,
fix(5.375, 8, 3)
will yield 3.968758, the maximum value possible with the (8/3) precision.
In addition to the fix() function, the expression language offers a
quantize()
function. The arguments are the same as those of the fix() function,
but the return type is a DoubleToken or DoubleMatrixToken instead of a
FixToken or FixMatrixToken. This function can therefore be used to
quantize double-precision values without ever explicitly working with
the fixed-point representation.
To make the FixToken accessible within the expression language, the
following functions are available:
- To create a single FixPoint Token using the expression language:
fix(5.34, 10, 4)
This will create a FixToken. In this case, we try to fit the
number 5.34 into a 10 bit representation with 4 bits used in the
integer part. This may lead to quantization errors. By default the
round quantizer is used.
- To create a Matrix with FixPoint values using the expression
language:
fix([ -.040609, -.001628, .17853 ], 10, 2)
This will create a FixMatrixToken with 1 row and 3 columns, in
which each element is a FixPoint value with precision(10/2). The
resulting FixMatrixToken will try to fit each element of the given
double matrix into a 10 bit representation with 2 bits used for
the integer part. By default the round quantizer is used.
- To create a single DoubleToken, which is the quantized version
of the double value given, using the expression language:
quantize(5.34, 10, 4)
This will create a DoubleToken. The resulting DoubleToken contains
the double value obtained by fitting the number 5.34 into a
10 bit representation with 4 bits used in the integer part. This may
lead to quantization errors. By default the round quantizer is used.
- To create a Matrix with doubles quantized to a particular
precision using the expression language:
quantize([ -.040609, -.001628, .17853 ], 10, 2)
This will create a DoubleMatrixToken with 1 row and 3
columns. The elements of the token are obtained by fitting the given
matrix elements into a 10 bit representation with 2 bits used for
the integer part. Instead of being a fixed point value, the values
are converted back to their double representation and by
default the round quantizer is used.