Stateflow | ![]() ![]() |
Fixed-Point Arithmetic
This section presents a high-level overview of fixed-point arithmetic with the following topics:
Use this section to understand fixed-point numbers before you attempt to understand how Stateflow implements these numbers in How Stateflow Implements Fixed-Point Data.
For more discussion on the theory of fixed-point numbers, see Fixed-Point Numbers in the Fixed-Point Blockset documentation.
Fixed-Point Numbers
Fixed-point numbers use integers and integer arithmetic to represent real numbers and arithmetic with the following encoding scheme:
Fixed-point numbers encode real quantities (for example, 15.375) using the stored integer . You set
's value by solving the preceding equation
for
and rounding the result to an integer value as follows:
For example, suppose you want to represent the number 15.375 in a fixed-point type with the slope and the bias
. This means that
However, because is rounded to an integer, you have lost some precision in representing the number 15.375. If you calculate the number that
actually represents, you now get a slightly different answer.
So using fixed-point numbers to represent real numbers with integers involves the loss of some precision. However, if you choose and
correctly, you can minimize this loss to acceptable levels.
Fixed-Point Operations
Now that you can express fixed-point numbers as , you can define operations between two fixed-point numbers.
The general equation for an operation between fixed-point operands is as follows:
c = a <op> b
where a
, b
, and c
are all fixed-point numbers, and <op>
refers to one of the binary operations: addition, subtraction, multiplication, or division.
The general form for a fixed-point number x
is (see Fixed-Point Numbers). Substituting this form for the result and operands in the preceding equation yields the following:
The values for and
are usually chosen by Stateflow for each operation (see Promotion Rules for Fixed-Point Operations) and are based on the values for
,
,
, and
, which are entered for each fixed-point data (see Specifying Fixed-Point Data in Stateflow).
Note
Stateflow also offers a more precise means for choosing the values for ![]() ![]() := assignment operator (that is, c := a <op> b ). See Assignment Operations for more detail.
|
Using the values for , and
, you can solve the preceding equation for
for each binary operation as follows:
c=a+b
implies that c=a-b
implies that c=a/b
implies that
The fixed-point approximations of the real number result of the operation c = a <op> b
are given by the preceding solutions for the value . In this way, all fixed-point operations are performed using only the stored integer
for each fixed-point number and integer operation.
![]() | Fixed-Point Data | How Stateflow Implements Fixed-Point Data | ![]() |