Why a condition like If (~Input) changes to a complex condition with ternary operator ?
이전 댓글 표시
When I am trying to generate C code with Embedded Coder, the following condition
if (~Input)
is converted to the following ugly condition
if ((boolean_T)((int32_T)((Input ? ((int32_T)1) : ((int32_T)0)) ^ 1))) {
Is there any option to change in the setting to avoid this type of code being generated?
Is there a reason why this code would be generated instead of a simpler alternative?
The same code is generated if I change the condition to:
if (false == Input)
댓글 수: 4
Les Beckham
2022년 12월 1일
Yikes. That is ugly. Just curious, what is the type (class) of Input? Double? Logical?
Also, are you sure there isn't a typo? Correct me if I'm wrong, but isn't this going to be 1: ((int32_T)0)) ^ 1))
Walter Roberson
2022년 12월 1일
Bleh, that code is equivalent to
xor(1, Input ~= 0)
which is pretty ugly.
What happens if you change the condition to
if (Input == 0)
?
Miguel
2022년 12월 1일
채택된 답변
추가 답변 (1개)
Les Beckham
2022년 12월 1일
0 개 추천
So, the original condition if (~Input) is C code and not Matlab code? If so, you should use the logical not operator which is ! in C instead of ~ (as it is in Matlab).
~false in C will be 0xFFFFFFFE (or similar) and the ugly ternary expression coerces that back to a 1.
BTW - please post comments as comments rather than answers.
댓글 수: 3
Walter Roberson
2022년 12월 1일
No, the original is MATLAB code, but Embedded Coder is generating the ugly version.
Les Beckham
2022년 12월 1일
I thought that too until OP said "The class of variable "Input" is "boolean_T". Perhaps he meant in the generated code. I meant in the original Matlab code when I asked that. Probably a misunderstanding.
Miguel
2022년 12월 1일
카테고리
도움말 센터 및 File Exchange에서 MATLAB Coder에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

