## Fixed-Point Operations in Stateflow Charts

Fixed-point numbers use integers and integer arithmetic to approximate real numbers. They enable you to perform computations involving real numbers without requiring floating-point support in underlying system hardware. For more information, see Fixed-Point Data in Stateflow Charts.

### Arithmetic Operations for Fixed-Point Data

The general equation for a binary arithmetic operation between fixed-point operands is

```c = a <op> b ```
where `a` and `b` are fixed-point numbers and `<op>` refers to addition, subtraction, multiplication, or division. The result of the operation is a fixed-point number `c` of the form

V`c`S`c`Q`c` + B`c`.

The fixed-point type for `c` determines the slope S`c`, the bias B`c`, and the number of bits used to store the quantized integer Q`c`. For each arithmetic operation, this table lists the value of the quantized integer Q`c` in terms of the values of the operands (`a` and `b`) and the fixed-point type for `c`.

OperationSlope and Bias ScalingBinary-Point Scaling

`c = a+b`Q`c` = round((S`a`/S`c`)Q`a` + (S`b`/S`c`)Q`b` + (B`a` + B`b`B`c`)/S`c`)Q`c` = round((S`a`/S`c`)Q`a` + (S`b`/S`c`)Q`b`)

Subtraction

`c = a-b`Q`c` = round((S`a`/S`c`)Q`a` – (S`b`/S`c`)Q`b` – (B`a`B`b`B`c`)/S`c`)Q`c` = round((S`a`/S`c`)Q`a` – (S`b`/S`c`)Q`b`)

Multiplication

`c = a*b`Q`c` = round((S`a`S`b`/S`c`)Q`a`Q`b` + (B`a`S`b`/S`c`)Q`a` + (B`b`S`a`/S`c`)Q`b` + (B`a`B`b`B`c`)/S`c`)Q`c` = round((S`a`S`b`/S`c`)Q`a`Q`b`)

Division

`c = a/b`Q`c` = round((S`a`Q`a` + B`a`)/(S`c`(S`b`Q`b` + B`b`)) – (B`c`/S`c`))Q`c` = round((S`a`/(S`b`S`c`))Q`a`/Q`b`)

To simplify these expressions and avoid computationally expensive operations, use binary-point scaling to encode all your fixed-point data. With this setting, the slope is an integer power of two and the bias is zero. Then, all fixed-point operations consist of integer arithmetic and bit shifts on the quantized integers.

Note

The result of an arithmetic operation depends on the rounding method that computes the value of the quantized integer Q`c`. For more information, see Conversion Operations.

### Relational Operations for Fixed-Point Data

You can use relational operations to compare fixed-point data. Comparing fixed-point values of different types can yield unexpected results because each operand is converted to a common type for comparison. Because of rounding or overflow errors during the conversion, some values that appear to be equal are not equal as fixed-point numbers.

For example, suppose that `a` and `b` represent the real-world value V = 2.2 in two different fixed-point encoding schemes:

• `a` is a fixed-point number with slope S`a` = 0.3 and bias B`a` = 0.1. The quantized integer for `a` is:

Q`a` = (VB`a`)/S`a` = (2.2 – 0.1)/0.3 = 7.

• `b` is a fixed-point number with slope S`b` = 0.7 and bias B`b` = 0.1. The quantized integer for `b` is:

Q`b` = (VB`b`)/S`b` = (2.2 – 0.1)/0.7 = 3.

To compare these values, the chart first converts them to a common fixed-point type with slope Scomp = 1.06811 · 10–5S`a`/28087 ≈ S`b` · 2–16 and bias Bcomp = 0.1. (In this case, the slope Scomp arises from the approximate value of S`a`/S`b` = 0.3/0.7 ≈ 28087 · 2–16.) In this common encoding scheme, `a` and `b` correspond to these quantized integers:

Q`a'` = S`a`Q`a`/Scomp = Q`a`(S`a`/Scomp) ≈ 7 ⨉ 28087 = 196609

Q`b'` = S`b`Q`b`/Scomp = Q`b`(S`b`/Scomp) ≈ 3 ⨉ 216 = 196608.

After the conversion, the quantized integers are different. Although `a` and `b` represent the same real-world value, they are not equal as fixed-point numbers.

Note

In charts that use C as the action language, comparisons of fixed-point operands with mismatched biases are not supported.

### Logical Operations for Fixed-Point Data

In a logical operation, a fixed-point operand `a` is interpreted as `false` if it corresponds to the real-world value for zero in the fixed-point type of `a`. Otherwise, `a` is interpreted as `true`.

• In charts that use MATLAB® as the action language, using `a` in a logical operation is equivalent to the expression ```a ~= cast(0,"like",a)```.

• In charts that use C as the action language, using `a` in a logical operation is equivalent to the expression `a != 0c`, where `0c` is a fixed-point context-sensitive constant. See Fixed-Point Context-Sensitive Constants.

For example, suppose that `a` is a fixed-point number with a slope of S`a` = 0.25 and a bias of B`a` = 5.1. Using `a` in a logical operation is equivalent to testing whether the quantized integer Q`a` satisfies the condition

Q`a` = round((0 – B`a`)/S`a`) = round(–5.1 / 0.25) = round(–20.4) = –20.

Therefore, `a` is equivalent to `false` when its real-world approximation is

V`a`S`a`Q`a` + B`a` = 0.25 ⨉ ( –20) + 5.1 = 0.1.

### Promotion Rules for Fixed-Point Operations

The rules for selecting the numeric type used for the result of an operation on fixed-point numbers are called fixed-point promotion rules. These rules help to maintain computational efficiency and usability.

The fixed-point promotion rules determine a result type for an operation ```c = a <op> b ``` by selecting the slope S`c`, the bias B`c`, and the number of bits `wc` used to store the quantized integer Q`c`. These parameters depend on the fixed-point types of the operands `a` and `b`, the operation `<op>` to be performed, and the action language property for the chart.

• In a chart that uses MATLAB as the action language, you control the fixed-point promotion rules through the fixed-point properties for the chart. See Fixed-Point Properties.

• If you set the MATLAB Chart `fimath` property to ```Same as MATLAB```, then arithmetic operations follow the default fixed-point promotion rules for MATLAB. See Performing Fixed-Point Arithmetic (Fixed-Point Designer).

• If you specify a chart `fimath` object with `SumMode` and `ProductMode` set to `SpecifyPrecision`, then you can define the word length, slope, and bias for all sums and products explicitly. See fimath Object Properties (Fixed-Point Designer).

• In a chart that uses C as the action language, the fixed-point promotion rules determine the type for an intermediate value of the result. This intermediate value is then cast to the type that you specify for `c`.

For all arithmetic operations, the default number of bits `wc` used to store the quantized integer is the larger value between:

• The maximum number of bits in the operand types (`wa` and `wb`).

• The number of bits in the integer word size for the target machine (`wint`).

To specify the value of `wint`, open the Configuration Parameters dialog box and, in the Hardware Implementation pane, set the Device vendor parameter to `Custom Processor` and the int parameter to the target integer word size. For more information, see Hardware Implementation Pane (Simulink).

You can avoid overflow and improve the precision in your floating-point operations by using the special assignment operation of the form ```c := a <op> b```. The special assignment operation does not follow the fixed-point promotion rules. Instead, the chart determines the result of the operation by using the type that you specify for `c`. See Override Fixed-Point Promotion in C Charts.

By default, charts that use MATLAB as the action language support addition and subtraction only on fixed-point data defined through binary-point scaling. If either operand is a signed fixed-point number, then the result is also signed. The choice of word length accommodates the integer and fraction parts of each operand in addition to a possible carry bit. The fraction length of the result is equal to the fraction length of the most precise operand. To perform addition and subtraction on fixed-point data defined by using either a slope that is not an integer power of two or a nonzero bias, specify a chart `fimath` object with `SumMode` set to `SpecifyPrecision`.

Charts that use C as the action language support addition and subtraction for operands of all fixed-point data types. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of the result is equal to the slope of the least precise operand. To simplify calculations and yield efficient code, the biases of the two inputs are added for an addition operation and subtracted for a subtraction operation.

`a``b`MATLAB as the Action LanguageC as the Action Language
Signs`a`s`b`s`c` = s`a``||`s`b`s`c` = s`a` `&&` s`b`
Word lengthw`a`w`b`w`c` = max(w`a`f`a`, w`b`f`b`) + max(f`a`, f`b`) + 1w`c` = max(w`a`, w`b`, w`int`)
Fraction lengthf`a`f`b`f`c` = max(f`a`, f`b`)f`c` = min(f`a`, f`b`)
SlopeS`a` (2-fa if using binary-point scaling)S`b` (2-fb if using binary-point scaling)S`c` = min(S`a`, S`b`)S`c` = max(S`a`, S`b`)
BiasB`a` (0 if using binary-point scaling)B`b` (0 if using binary-point scaling)B`c` = 0B`c` = B`a` + B`b` for addition or B`c` = B`a`B`b` for subtraction

#### Multiplication

By default, charts that use MATLAB as the action language support multiplication only on fixed-point data defined through binary-point scaling. If either operand is a signed fixed-point number, then the result is also signed. A full precision product requires a word length equal to the sum of the word lengths of the operands. The fraction length of a product is the sum of the fraction lengths of the operands. To perform multiplication on fixed-point data defined by using either a slope that is not an integer power of two or a nonzero bias, specify a chart `fimath` object with `ProductMode` set to `SpecifyPrecision`.

Charts that use C as the action language support multiplication only on fixed-point data operands defined by nonzero biases. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of a product is the product of the slopes of the operands.

`a``b`MATLAB as the Action LanguageC as the Action Language
Signs`a`s`b`s`c` = s`a``||`s`b`s`c` = s`a` `&&` s`b`
Word lengthw`a`w`b`w`c` = w`a` + w`b`w`c` = max(w`a`, w`b`, w`int`)
Fraction lengthf`a`f`b`f`c` = f`a` + f`b`f`c` = f`a` + f`b`
SlopeS`a` (2-fa if using binary-point scaling)S`b` (2-fb if using binary-point scaling)S`c` = S`a`S`b`S`c` = S`a`S`b`
BiasB`a` = 0B`b` = 0B`c` = 0B`c` = 0

#### Division

Charts that use MATLAB as the action language support division only on fixed-point data defined through binary-point scaling. If either operand is a signed fixed-point number, then the result is also signed. A full precision quotient requires a word length equal to the maximum number of bits in the operands. The fraction length of a quotient is the difference of the fraction lengths of the operands.

Charts that use C as the action language support division for fixed-point data operands defined by nonzero biases. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of a quotient is the quotient of the slopes of the operands.

`a``b`MATLAB as the Action LanguageC as the Action Language
Signs`a`s`b`s`c` = s`a``||`s`b`s`c` = s`a` `&&` s`b`
Word lengthw`a`w`b`w`c` = max(w`a`, w`b`)w`c` = max(w`a`, w`b`, w`int`)
Fraction lengthf`a`f`b`f`c` = f`a`f`b`f`c` = f`a`f`b`
SlopeS`a` (2-fa if using binary-point scaling)S`b` (2-fb if using binary-point scaling)S`c` = S`a`/S`b`S`c` = S`a`/S`b`
BiasB`a` = 0B`b` = 0B`c` = 0B`c` = 0

#### Unary Minus

The only unary operation that requires a promotion of its result type is the unary minus operation `c = -a`. Taking the negative of an unsigned fixed-point number can yield unexpected results and is not recommended. The word size of the result depends on the action language property of the chart. The slope of the result is equal to the slope of the operand. The bias of the result type is the negative of the bias of the operand.

`a`MATLAB as the Action LanguageC as the Action Language
Signs`a`s`c` = s`a`s`c` = s`a`
Word lengthw`a`w`c` = w`a`w`c` = max(w`a`, w`int`)
Fraction lengthf`a`f`c` = f`a`f`c` = f`a`
SlopeS`a` (2-fa if using binary-point scaling)S`c` = S`a`S`c` = S`a`
BiasB`a` (0 if using binary-point scaling)B`c` = –B`a`B`c` = –B`a`

#### Arithmetic with Mixed Numeric Types

This table summarizes the fixed-point promotion rules for a binary operation between a fixed-point number and an operand of a different numeric type.

Numeric Type of Second OperandMATLAB as the Action LanguageC as the Action Language

Floating-point numbers:

• `single`

• `double`

Before performing the operation, the chart casts the floating-point operand to a fixed-point number. The type used for the cast depends on the operation:

• Addition and subtraction operations use the same type as the fixed-point operand.

• Multiplication operations use the same word length and signedness as the fixed-point operand, and the best precision fraction length for a fixed-point result.

The result of the operation is a fixed-point number.

Before performing the operation, the chart casts the fixed-point operand to a floating-point number . The casting operation uses the same type (`single` or `double`) as the floating-point operand. The result of the operation is a floating-point number.

Integers:

• `int64`

• `int32`

• `int16`

• `int8`

• `uint64`

• `uint32`

• `uint16`

• `uint8`

The integer operand is treated as a fixed-point number of the same word length and signedness with slope S = 1 and bias B = 0. The result of the operation is a fixed-point number.

The integer operand is treated as a fixed-point number of the same word length and signedness with slope S = 1 and bias B = 0. The result of the operation is a fixed-point number.