Main Content

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

VcScQc + Bc.

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

OperationSlope and Bias ScalingBinary-Point Scaling

Addition

c = a+bQc = round((Sa/Sc)Qa + (Sb/Sc)Qb + (Ba + BbBc)/Sc)Qc = round((Sa/Sc)Qa + (Sb/Sc)Qb)

Subtraction

c = a-bQc = round((Sa/Sc)Qa – (Sb/Sc)Qb – (BaBbBc)/Sc)Qc = round((Sa/Sc)Qa – (Sb/Sc)Qb)

Multiplication

c = a*bQc = round((SaSb/Sc)QaQb + (BaSb/Sc)Qa + (BbSa/Sc)Qb + (BaBbBc)/Sc)Qc = round((SaSb/Sc)QaQb)

Division

c = a/bQc = round((SaQa + Ba)/(Sc(SbQb + Bb)) – (Bc/Sc))Qc = round((Sa/(SbSc))Qa/Qb)

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 Qc. 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 Sa = 0.3 and bias Ba = 0.1. The quantized integer for a is:

    Qa = (VBa)/Sa = (2.2 – 0.1)/0.3 = 7.

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

    Qb = (VBb)/Sb = (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–5Sa/28087 ≈ Sb · 2–16 and bias Bcomp = 0.1. (In this case, the slope Scomp arises from the approximate value of Sa/Sb = 0.3/0.7 ≈ 28087 · 2–16.) In this common encoding scheme, a and b correspond to these quantized integers:

Qa' = SaQa/Scomp = Qa(Sa/Scomp) ≈ 7 ⨉ 28087 = 196609

Qb' = SbQb/Scomp = Qb(Sb/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 Sa = 0.25 and a bias of Ba = 5.1. Using a in a logical operation is equivalent to testing whether the quantized integer Qa satisfies the condition

Qa = round((0 – Ba)/Sa) = round(–5.1 / 0.25) = round(–20.4) = –20.

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

VaSaQa + Ba = 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 Sc, the bias Bc, and the number of bits wc used to store the quantized integer Qc. 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.

    • 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.

Addition and Subtraction

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.

 abMATLAB as the Action LanguageC as the Action Language
Signsasbsc = sa||sbsc = sa && sb
Word lengthwawbwc = max(wafa, wbfb) + max(fa, fb) + 1wc = max(wa, wb, wint)
Fraction lengthfafbfc = max(fa, fb)fc = min(fa, fb)
SlopeSa (2-fa if using binary-point scaling)Sb (2-fb if using binary-point scaling)Sc = min(Sa, Sb)Sc = max(Sa, Sb)
BiasBa (0 if using binary-point scaling)Bb (0 if using binary-point scaling)Bc = 0Bc = Ba + Bb for addition or Bc = BaBb 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.

 abMATLAB as the Action LanguageC as the Action Language
Signsasbsc = sa||sbsc = sa && sb
Word lengthwawbwc = wa + wbwc = max(wa, wb, wint)
Fraction lengthfafbfc = fa + fbfc = fa + fb
SlopeSa (2-fa if using binary-point scaling)Sb (2-fb if using binary-point scaling)Sc = SaSbSc = SaSb
BiasBa = 0Bb = 0Bc = 0Bc = 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.

 abMATLAB as the Action LanguageC as the Action Language
Signsasbsc = sa||sbsc = sa && sb
Word lengthwawbwc = max(wa, wb)wc = max(wa, wb, wint)
Fraction lengthfafbfc = fafbfc = fafb
SlopeSa (2-fa if using binary-point scaling)Sb (2-fb if using binary-point scaling)Sc = Sa/SbSc = Sa/Sb
BiasBa = 0Bb = 0Bc = 0Bc = 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.

 aMATLAB as the Action LanguageC as the Action Language
Signsasc = sasc = sa
Word lengthwawc = wawc = max(wa, wint)
Fraction lengthfafc = fafc = fa
SlopeSa (2-fa if using binary-point scaling)Sc = SaSc = Sa
BiasBa (0 if using binary-point scaling)Bc = –BaBc = –Ba

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.

Related Topics