|On this page…|
Fixed-point numbers use integers and integer arithmetic to approximate real numbers. They are an efficient means for performing computations involving real numbers without requiring floating-point support in underlying system hardware.
Fixed-point numbers use integers and integer arithmetic to represent real numbers and arithmetic with the following encoding scheme:
V is a precise real-world value that you want to approximate with a fixed-point number.
is the approximate real-world value that results from fixed-point representation.
Q is an integer that encodes This value is the quantized integer.
Q is the actual stored integer value used in representing the fixed-point number. If a fixed-point number changes, its quantized integer, Q, changes but S and B remain unchanged.
S is a coefficient of Q, or the slope.
B is an additive correction, or the bias.
Fixed-point numbers encode real quantities (for example, 15.375) using the stored integer Q. You set the value of Q by solving the equation
for Q and rounding the result to an integer value as follows:
Q = round((V – B)/S)
For example, suppose you want to represent the number 15.375 in a fixed-point type with the slope S = 0.5 and the bias B = 0.1. This means that
Q = round((15.375 – 0.1)/0.5) = 30
However, because Q is rounded to an integer, you lose some precision in representing the number 15.375. If you calculate the number that Q actually represents, you now get a slightly different answer.
Using fixed-point numbers to represent real numbers with integers involves the loss of some precision. However, if you choose S and B correctly, you can minimize this loss to acceptable levels.
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 a binary operation: addition, subtraction, multiplication, or division.
The general form for a fixed-point number x is SxQx + Bx (see Fixed-Point Numbers). Substituting this form for the result and operands in the preceding equation yields this expression:
(ScQc + Bc) = (SaQa + Ba) <op> (SbQb + Bb)
The values for Sc and Bc are chosen by Stateflow® software for each operation (see Promotion Rules for Fixed-Point Operations) and are based on the values for Sa, Sb, Ba and Bb that you enter for each fixed-point data (see Specify Fixed-Point Data).
Note: You can be more precise in choosing the values for Sc and Bc when you use the := assignment operator (that is, c := a <op> b). See Assignment (=, :=) Operations.
Using the values for Sa, Sb, Sc, Ba, Bb, and Bc, you can solve the preceding equation for Qc for each binary operation as follows:
The operation c=a+b implies that
Qc = ((Sa/Sc)Qa + (Sb/Sc)Qb + (Ba + Bb – Bc)/Sc)
The operation c=a-b implies that
Qc = ((Sa/Sc)Qa – (Sb/Sc)Qb – (Ba – Bb – Bc)/Sc)
The operation c=a*b implies that
Qc = ((SaSb/Sc)QaQb + (BaSb/Sc)Qa + (BbSa/Sc)Qb + (BaBb – Bc)/Sc)
The operation c=a/b implies that
Qc = ((SaQa + Ba)/(Sc(SbQb + Bb)) – (Bc/Sc))
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 Qc. In this way, all fixed-point operations are performed using only the stored integer Q for each fixed-point number and integer operation.