Azzera filtri
Azzera filtri

matlab interp C code generation, Waring: comparing floating point with == or != is unsafe

3 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

SlipperyGnome
SlipperyGnome il 23 Giu 2022
Hi da wu,
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
0
This tolerance value method would work accurately upto 1e-15. You can also go to this link for better understanding.
  2 Commenti
da wu
da wu il 24 Giu 2022
Hi SlipperyGnome,
Firstly, thank you very much for your answer.
I know, when I write a code by hand to realize " if single == single", I can use your method above. But the situation is that I use the matlab function " interp1", and generate C code automatically. Maybe a little different.
A test example:
function y = fcn(u)
y = interp1([0,20.5,50.2],[0,30.3,40],u);
Generated C code: (if single == single occurs)
void interp_test_step(void)
{
static const real_T b[3] = { 0.0, 20.5, 50.2 };
static const real_T c[3] = { 0.0, 30.3, 40.0 };
int32_T low_i;
real32_T r;
/* Outport: '<Root>/y' incorporates:
* MATLAB Function: '<Root>/MATLAB Function'
*/
interp_test_Y.y = (rtNaNF);
/* MATLAB Function: '<Root>/MATLAB Function' incorporates:
* Inport: '<Root>/u'
*/
if ((!rtIsNaNF(interp_test_U.u)) && ((!(interp_test_U.u > 50.2)) &&
(!(interp_test_U.u < 0.0F)))) {
low_i = 0;
if (interp_test_U.u >= 20.5F) {
low_i = 1;
}
r = (interp_test_U.u - (real32_T)b[low_i]) / (real32_T)(b[low_i + 1] -
b[low_i]);
if (r == 0.0F) {
/* Outport: '<Root>/y' */
interp_test_Y.y = (real32_T)c[low_i];
} else if (r == 1.0F) {
/* Outport: '<Root>/y' */
interp_test_Y.y = (real32_T)c[low_i + 1];
} else if (c[low_i + 1] == c[low_i]) {
/* Outport: '<Root>/y' */
interp_test_Y.y = 30.3F;
} else {
/* Outport: '<Root>/y' */
interp_test_Y.y = (1.0F - r) * (real32_T)c[low_i] + (real32_T)c[low_i + 1]
* r;
}
}
}
SlipperyGnome
SlipperyGnome il 1 Lug 2022
Hi da wu,
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!

Accedi per commentare.


Szabolcs Fodor
Szabolcs Fodor il 25 Ott 2023
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

Categorie

Scopri di più su Model Compatibility in Help Center e File Exchange

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by