在状态下的浮子评估
我正在删除一些代码,发现一个有趣的条件在使用Misra2004的PC-LINT时没有标记为错误。
static float32_t float_values[10u] = {0.0f};
if (float_values[FIXED_ARRAY_ENTRY])
{
/* Do stuff */
}
我确实在这里看到了一个问题,但是这条线实际上做了它应该做的。因此,我的问题是要了解IF语句评估的工作方式。 我的理解是,编译器会将条件内容投放到INT值,如果浮点值为1.0F或更高,则该代码只能按预期工作。
我的第二个问题是,为什么PC-LINT找不到此错误。
I was debbugging some code and found an interesting if condition which was not marked as an error while using pc-lint with MISRA2004.
static float32_t float_values[10u] = {0.0f};
if (float_values[FIXED_ARRAY_ENTRY])
{
/* Do stuff */
}
I do see a problem here, but this line actualy did what it should do. So my question is about understanding how the if statement evaluation works.
My understanding would be, that the compiler will cast the condition content to an int value and if the float value is 1.0f or above, the code just works as intended.
And my second question would be, why did pc-lint not find this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Misra C有两个可能适用的规则:
中的条件。这意味着它们需要明确和自我记录。
是合规的。如果(PTR)
不合规,<代码>如果(PTR!= NULL)此代码当然违反了第一个规则,但实际上不是第二个规则,因为没有明确使用
==
或!=
。不,这是错误的。
如果
语句条件在C中接受任何标量,即:整数,浮子和指针。如果标量为非零/非null,则评估为true,否则为false。如果(0.1F)
评估为true,<代码>如果((int)0.1F)评估为false。因为您声称的错误在那里,所以不存在。问题是,为什么PC棉布没有发现Misra违反不将“基本上是布尔”的类型传递给(如果)的类型。答案是:因为PC皮棉真的很糟糕。
MISRA C has two rules that may apply:
if
statements should be "essentially boolean". Meaning they need to be explicit and self-documenting.if(ptr)
is not compliant,if(ptr != NULL)
is compliant.This code certainly violates the first rule, but not really the second since there is no explicit use of
==
or!=
.No that's wrong.
if
statement conditions in C accept any scalar, that is: integers, floats and pointers. It evaluates to true if the scalar is non-zero/non-null, otherwise false.if(0.1f)
evaluates as true,if((int)0.1f)
evaluates as false.Because the error you claim is there, isn't there. The question is rather why PC Lint didn't find the MISRA violation of not passing a type which is "essentially boolean" to
if
. The answer is: because PC Lint is really bad.