casting without rounding using coder

How can I manually trigger casting from a float/double to integer without involving any rounding while using coder?
LUT = single(rand(1,2000)*200);
for i=1:2000
ind = int32(LUT(i));
fprintf("%i\n",ind);
end
When the C++ code is generated it will be similar to:
int ind = static_cast<int>(rt_roundf_snf(LUT[i]));
I am aware that under certain circumstances coder is able to automatically figure out the optimal datatypes for indexing inside for loops resulting then in the desired result:
int ind = static_cast<int>(LUT[i]);

回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2022 年 7 月 12 日

0 投票

Well, the only interpretation I can thing of that matches the "casting without rounding" in a sensible way is:
ind = int32(floor(LUT2(i));
That way ind will be the integer part of whatever LUT2 is. But this is still "rounding-off the fractional part".
HTH

4 件のコメント

Marc Barnada
Marc Barnada 2022 年 7 月 15 日
Unfortunately my interest is to simply trigger a plain static_cast<int> call without flooring, ceiling or rounding.
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 7 月 15 日
What is the crucial difference?
Marc Barnada
Marc Barnada 2022 年 7 月 18 日
I want to have highly efficient C++ code, and that extra call to rt_roundf_snf has a big impact in the timings of a Look-Up-Table (LUT) function.
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 7 月 18 日
If it is a limited point-of-use and a one-time effort would it be possible to manually edit the generated code?

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 7 月 12 日

コメント済み:

2022 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by