Stateflow | ![]() ![]() |
Assignment (=, :=) and Cast Operations
Stateflow supports the assignment operations LHS = RHS
and LHS := RHS
between a left-hand side (LHS
) and a right-hand side (RHS
). These are described in the following topics:
Assignment Operator =
An assignment statement of the type LHS = RHS is equivalent to casting the right-hand side to the type of the left-hand side. Stateflow supports any assignment between fixed-point types and therefore, implicitly, any cast.
A cast converts the stored integer from its original fixed-point type while preserving its value as accurately as possible using the online conversions (see Offline and Online Conversions of Fixed-Point Data). Assignments are most efficient when both types have the same bias, and slopes that are equal or both powers of 2.
Assignment Operator :=
Ordinarily, Stateflow uses the fixed-point promotion rules to choose the result type for an operation. Using the :=
assignment operator overrides this behavior by using the type of the LHS
as the result type of the RHS
operation.
This type of assignment is particularly useful in retaining useful range and precision in the result of a multiplication or division that ordinary assignment might not retain. It is less useful with addition or subtraction but can avoid overflow or the loss of memory to store a result even in these cases.
Use of the :=
assignment operator is governed by the following rules:
RHS
can contain at most one binary operator.
RHS
contains anything other than a multiplication (*), division (/), addition (+), or subtraction (-) operation, or a constant, then the :=
assignment behaves exactly like regular assignment (=
).
RHS
of an LHS := RHS
assignment are converted to the type of the left-hand side using offline conversion (see Offline and Online Conversions of Fixed-Point Data). Ordinary assignment always casts the RHS
using online conversions.
For examples contrasting the LHS := RHS
and the LHS = RHS
assignment operations, see the following:
Caution
Using the := assignment operator to produce a more accurate result might generate code that is less efficient than the code generated using the normal fixed-point promotion rules.
|
:= Multiplication Example
The following example contrasts the :=
and =
assignment operators for multiplication. Here, the :=
operator is used to avoid overflow in the results of the multiplication c
=
a
*
b
in which a
and b
are of two fixed-point operands. The operands and result for this operation are 16 bit unsigned integers with the following assignments:
Fixed-Point Number a |
Fixed-Point Number b |
Fixed-Point Number c |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
where is the slope,
is the bias,
is the real-world value, and
is the quantized integer.
c = a*b. In this case, first calculate an intermediate result for a*b
in the fixed-point type given by the rules in the section Fixed-Point Operations, and then cast that result into the type for c
.
The intermediate value is calculated as follows:
Because the maximum value of a 16 bit unsigned integer is , the preceding result overflows its word size. An operation that overflows its type produces an undefined result.
You can capture overflow errors like the preceding example during simulation with the Debugger window. See Overflow Detection for Fixed-Point Types.
c := a*b. In this case, calculate a*b
directly in the type of c
. Use the solution for given in Fixed-Point Operations with the requirement of zero bias, which is as follows:
No overflow occurs in this case, and the approximate real-world value is as follows:
This value is very close to the actual real-world result of .
:= Division Example
The following example contrasts the :=
and =
assignment operators for division. The :=
operator is used to obtain more precise results for the division of two fixed-point operands, b
and c
, in the statement c := a/b
.
This example uses the following fixed-point numbers, where is the slope,
is the bias,
is the real-world value, and
is the quantized integer:
Fixed-Point Number a |
Fixed-Point Number b |
Fixed-Point Number c |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
c = a/b. In this case, first calculate an intermediate result for a/b
in the fixed-point type given by the rules in the section Fixed-Point Operations, and then cast that result into the type for c
.
The intermediate value is calculated as follows:
The intermediate value is then cast to the result type for c
as follows:
The slope of the intermediate value for a division operation is calculated as
Substitution of this value into the preceding result yields the final result.
In this case, the approximate real-world value is , which is not a very good approximation of the actual result,
.
c := a/b. In this case, calculate a/b
directly in the type of c
. Use the solution for given in Fixed-Point Operations with the simplification of zero bias, which is as follows:
In this case, the approximate real-world value = 42/64 = 0.6563, a much better approximation to the precise result,
.
:= Assignment and Context-Sensitive Constants
In a :=
assignment operation, the type of the left-hand side (LHS
) determines part of the context used for inferring the type of a right-hand side (RHS
) context-sensitive constant.
The following rules apply to RHS
context-sensitive constants in assignments with the :=
operator:
LHS
is a floating-point data (type double
or single
) , the RHS
context-sensitive constant becomes a floating-point constant.
![]() | Promotion Rules for Fixed-Point Operations | Overflow Detection for Fixed-Point Types | ![]() |