matlab interp C code generation, Waring: comparing floating point with == or != is unsafe
3 ビュー (過去 30 日間)
古いコメントを表示
I use interp1,interp2 in a .m file and generate C code, then the _sharedutils folder has a new xxxxx_interp1.c file to realize interp function, but the generated C code use "if floating point == floating point ", so C complier warn: comparing floating point with == or != is unsafe, how can I solve the problem in matlab and let it generate the rigtht C code.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1042520/image.png)
0 件のコメント
回答 (2 件)
SlipperyGnome
2022 年 6 月 23 日
When comparing floating point numbers, because of very small rounding off errors, using '==' or '!=' will generate compiler warnings.
You can set an error tolerance upto the magnitude you need it to be same.
num1=0.8-0.5;
num2=0.3;
Error_tolerance= (1e-15)*max(num1,num2);
if (abs(num1-num2) < Error_tolerance)
disp(0);
else
disp("not 0")
end
This tolerance value method would work accurately upto 1e-15. You can also go to this link for better understanding.
2 件のコメント
SlipperyGnome
2022 年 7 月 1 日
One of the ways is you can try remodelling with a lookup table block, which can henceforth generate better code for you.
Hope this helps!
Szabolcs Fodor
2023 年 10 月 25 日
Hello there,
did you found a reasonable solution for this issue? I'm facing similar issues and this is a big no for our embeded system.
Please get back to me if you found a solution.
Cheers,
Szabi
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Model Compatibility についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!