%% Compare Results of Fixed-Point Arithmetic
% This example shows the difference between various implementations of
% fixed-point arithmetic in Stateflow charts. The model contains three
% charts that calculate the ratio |a/b| where |a| = 19 and |b| = 24. Both
% inputs are signed 16-digit fixed-point numbers with one fraction bit
% (type |fixdt(1,16,1)|). They correspond to these slopes and quantized
% integers:
%
% $$ S_\texttt{a} = 2^{-1}, Q_\texttt{a} = 38 $$
%
% $$ S_\texttt{b} = 2^{-1}, Q_\texttt{b} = 48 $$

% Copyright 2019 The MathWorks, Inc.

%% 
%
% The model calculates the value of |a/b| as a floating-point number of
% type |fixdt(1,16,1)| in three different ways:
%
% * A type casting operation in a chart that uses MATLAB as the action language.
% * A simple assignment operation in a chart that uses C as the
% action language.
% * A special assignment operation in a chart that uses C as the
% action language.

mdl = 'sf_fixpt_conversion';
load_system(mdl);
sim(mdl);
open_system(mdl)

%% Type Casting in Chart That Uses MATLAB as the Action Language
%
% The chart at the top of the model computes an intermediate value for
% |a/b|. The quantized integer for the intermediate value is rounded to the
% nearest integer:
%
% $$ S_\texttt{int} = S_\texttt{a}/S_\texttt{b} = 1 $$
%
% $$ Q_\texttt{int} = Q_\texttt{a}/Q_\texttt{b} = 38/48 \approx 1 .$$
%
% The intermediate value is then cast as a signed 16-digit fixed-point
% number |c| with one fraction bit:
%
% $$ S_\texttt{c} = 2^{-1} $$
%
% $$ Q_\texttt{c} = S_\texttt{int} \cdot Q_\texttt{int} /S_\texttt{c} = 2
% .$$
%
% The output value from this chart is 
%
% $$ \tilde{V}_\texttt{c} = S_\texttt{c} \cdot Q_\texttt{c} = 1 .$$

%% Simple Assignment in Chart That Uses C as the Action Language
%
% The middle chart also computes an intermediate value for |a/b|. In this
% case, the quantized integer for the intermediate value is rounded to the
% floor:
%
% $$ S_\texttt{int} = S_\texttt{a}/S_\texttt{b} = 1 $$
%
% $$ Q_\texttt{int} = Q_\texttt{a}/Q_\texttt{b} = 38/48 \approx 0 .$$
%
% The intermediate value is then cast as a signed 16-digit fixed-point
% number |c| with one fraction bit:
%
% $$ S_\texttt{c} = 2^{-1} $$
%
% $$ Q_\texttt{c} = S_\texttt{int} \cdot Q_\texttt{int} /S_\texttt{c} = 0
% .$$
%
% The output value from this chart is 
%
% $$ \tilde{V}_\texttt{c} = S_\texttt{c} \cdot Q_\texttt{c} = 0 .$$

%% Special Assignment in Chart That Uses C as the Action Language
%
% The chart at the bottom of the model uses a special assignment
% of the form |c := a/b|. The value of the division is calculated directly
% as a signed 16-digit fixed-point number with one fraction bit. The
% quantized integer is rounded to the floor:
%
% $$ S_\texttt{c} = 2^{-1} $$
%
% $$ Q_\texttt{c} = (S_\texttt{a} \cdot Q_\texttt{a}) / (S_\texttt{c} \cdot
% S_\texttt{b} \cdot Q_\texttt{b}) = 19/12 \approx 1 .$$
%
% Therefore, the output value from this chart is 
%
% $$\tilde{V}_\texttt{c} = S_\texttt{c} \cdot Q_\texttt{c} = 0.5 .$$
%
% The three results exhibit loss of precision compared to the
% floating-point answer of 19/24 = 0.7917. To minimize the loss of
% precision to an acceptable level in your application, adjust the encoding
% scheme in your fixed-point data.


