Why do the data become zero when using the function fi?

15 ビュー (過去 30 日間)
MIKE JOHN
MIKE JOHN 2021 年 6 月 30 日
コメント済み: MIKE JOHN 2021 年 7 月 1 日
fm = get_fimath();
idx = fi(1,0,1,0,fm);
a = (idx+fi(2,0,2,0,fm))*fi(1/3,0,16,17,fm);
k = fi(a,0,17,0,fm)
function fm = get_fimath()
fm = fimath('RoundingMethod', 'Floor',...
'OverflowAction', 'Wrap',...
'ProductMode','FullPrecision',...
'MaxProductWordLength', 128,...
'SumMode','FullPrecision',...
'MaxSumWordLength', 128);
end
This code is generated when using the Matlab Coder .  I want to know why is k equal to zero? Is it because of division 1/3?

採用された回答

Divyam Gupta
Divyam Gupta 2021 年 6 月 30 日
Hi Mike,
In the fimath function, you've set the RoundingMethod parameter as Floor. 1/3 when floored leads to a 0 as the answer. This is why you're getting k as 0. You could consider changing the RoundingMethod parameter as per your desired result.
Hope this helps.
  3 件のコメント
Divyam Gupta
Divyam Gupta 2021 年 6 月 30 日
Hey Mike,
Kindly refer to Andy's answer below mine and let me know if that answers your questions. If not, I'd be happy to help you further.
MIKE JOHN
MIKE JOHN 2021 年 7 月 1 日
I already know the answer to my question. Thank you again!

サインインしてコメントする。

その他の回答 (1 件)

Andy Bartlett
Andy Bartlett 2021 年 6 月 30 日
編集済み: Andy Bartlett 2021 年 6 月 30 日
It's just like scientific notation
is the short answer to "Why FractionLength can be bigger than WordLength?".
The long answer is the following.
The concept of a binary-point is very useful for initial understanding of fixed-point types. Similarly, the concept of a decimal-point is useful for understanding values beyond integers. But using decimal-points becomes very cumbersome for very big or very small numbers. To make it easy to represent very big or very small values, scientific notation is super valuable.
verySmallNumber = 3e-200;
veryBigNumber = 7e123;
In essence, this notation breaks the value into two parts, a mantissa and an integer exponent for the given base.
Y = mantissa .* 10.^exponent
Fixed-point follows the same concept except that
  • base is 2
  • mantissa must be an integer
  • exponent is fixed, i.e. it is part of the variables type and does not change for the life of the variable
Y = intMantissa .* 2^FixedExponent
Since FractionLength = -FixedExponent, we can also write this as follows.
Y = intMantissa .* 2^-FractionLength
A nice thing about fi is that we can let it figure out the scaling that gives the best precision for a constant.
verySmallNumberFi = fi( 3e-200, 0, 8 )
veryBigNumberFi = fi( 7e123, 0, 8 )
Notice the very big positive and negative FractionLengths of 670 and -404 that are produced.
verySmallNumberFi =
3.00068384319763e-200
numerictype(0,8,670)
veryBigNumberFi =
6.982403670347e+123
numerictype(0,8,-404)
Fi has approximated the original double values using 8-bit unsigned integer mantissas.
147 * 2^-670
169 * 2^404
It's just scientific notation in base 2.
  1 件のコメント
MIKE JOHN
MIKE JOHN 2021 年 7 月 1 日
Thank you for your answer!
I have learned a lot!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFixed-Point Designer についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by