Divide two fi
objects
This example shows how to control the precision of the divide
function.
Create an unsigned fi
object with an 80-bit word length and 2^-83 scaling, which puts the leading 1 of the representation into the most significant bit. Initialize the object with value 0.1, and examine the binary representation.
P = fipref('NumberDisplay', 'bin',... 'NumericTypeDisplay', 'short',... 'FimathDisplay', 'none'); a = fi(0.1, 0, 80, 83)
a = 11001100110011001100110011001100110011001100110011010000000000000000000000000000 numerictype(0,80,83)
Notice that the infinite repeating representation is truncated after 52 bits, because the mantissa of an IEEE® standard double-precision floating-point number has 52 bits.
Contrast the above to calculating 1/10 in fixed-point arithmetic with the quotient set to the same numeric type as before.
T = numerictype('Signed', false,... 'WordLength', 80,... 'FractionLength', 83); a = fi(1); b = fi(10); c = divide(T, a, b); c.bin
ans = '11001100110011001100110011001100110011001100110011001100110011001100110011001101'
Notice that when you use the divide
function, the quotient is calculated to the full 80 bits, regardless of the precision of a
and b
. Thus, the fi
object c
represents 1/10 more precisely than a IEEE® standard double-precision floating-point number can.
T
— Numeric type of the outputnumerictype
objectNumeric type of the output, specified as a numerictype
object.
a
— NumeratorNumerator, specified as a scalar, vector, matrix, or multidimensional array.
a
and b
must have the same dimensions unless
one is a scalar. If either a
or b
is scalar, then
c
has the dimensions of the nonscalar object.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| fi
Complex Number Support: Yes
b
— DenominatorDenominator, specified as a real scalar, vector, matrix, or multidimensional array.
a
and b
must have the same dimensions unless
one is a scalar. If either a
or b
is scalar, then
c
has the dimensions of the nonscalar object.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| fi
Complex Number Support: Yes
If a
and b
are both fi
objects,
c
has the same fimath
object as a
.
If c
has a fi
Fixed
data type, and any one of the inputs have fi
floating point data types, then the fi
floating point is converted into a
fixed-point value. Intermediate quantities are calculated using the fimath
object of a
.
If either a
or b
is a fi
object,
and the other is a MATLAB® built-in numeric type, then the built-in object is cast to the word length of
the fi
object, preserving best-precision fraction length. Intermediate
quantities are calculated using the fimath
object of the input
fi
object.
If a
and b
are both MATLAB built-in doubles, then c
is the floating-point quotient
a./b
, and numerictype
T
is ignored.
For syntaxes for which Fixed-Point Designer™ software uses the numerictype
object T
,
the divide
function follows the data type propagation rules listed in the
following table. In most cases, floating-point data types are propagated. This allows you to
write code that can be used with both fixed-point and floating-point inputs.
Data Type of Input fi Objects a and b | Data Type of numerictype Object T | Data Type of Output c | |
---|---|---|---|
Built-in | Built-in | Any | Built-in |
|
|
| Data type of |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| fi
single |
|
|
|
|
|
|
|
|
|
|
|
|
| If either input |
|
|
|
|
|
|
|
|
|
|
| If either input |
Usage notes and limitations:
Any non-fi
input must be
constant; that is, its value must be known at compile time so that it can be cast to a
fi
object.
Complex and imaginary divisors are not supported.
Code generation does not support the syntax
T.divide(a,b)
.
For HDL Code generation, the divisor must be a constant and a power of two.
Non-fi
inputs must be constant; that is, their values must be
known at compile time so that they can be cast to fi
objects.
Complex and imaginary divisors are not supported.
Code generation in MATLAB does not support the syntax T.divide(a,b)
.
You have a modified version of this example. Do you want to open this example with your edits?