为什么 IF 语句被侵犯,而输出结果明明应该执行?

Why an IF statement is being infringed while output results clearly says it should be executed?

上下文:我正在使用 Matlab/Simulink 来模拟我需要 follow a reference. To simplify it: below reference current then apply more current, above reference current then apply less current. To achieve this, I have to control two devices(Sa 和 Sb)的电路,它基本上关闭在我的代码中应用 1 并以 0 打开(两者同时)。

现在,我有 these results,其中重要的图表是 S1 Current、S-Function/2 和 S-Function/1 以及最后一个问题:考虑到以下代码,为什么 S-Function/2, 这是 THY[], 保持在 1 而显然有时间流逝 S-Function/1, 这是 IGBT[], 是 0?

看看你的代码:

IGBT[i] = 1.0;

if ( IGBT[i] == 0.0) {
    THY[i] = 0.0;         // <--- will never be executed
}
else {
    THY[i] = 1.0; 
}

假设它是按顺序执行的(即没有线程),你 总是IGBT[i] 设置为 1 然后 else 部分将被执行导致 THY[i] 等于 1,因此始终保持 1.

我不确定,但我认为代码有问题:

for (i=0; i<width; i++){
if (*Is[i] <  *Iref[i]){
    //IGBT[] is S-Function/1 and THY[] is S-Function/2
    //Is[] is S1 Current and Iref[] is Reference Current 1
    IGBT[i] = 1.0;

    if ( IGBT[i] == 0.0){ // condition 1
    // if condition 1 is true then execute this
    THY[i] = 0.0;   
    }
    else {
    // if condition 1 is false then execute this
    THY[i] = 1.0; 
    }
    }
    else {
   // this condition will never be executed
    IGBT[i] = 0.0;   
    }
}

现在如果条件 1 为真则 THY[I] = 0.0; 将被执行,如果条件为假则 THY[i] = 1.0;。在你的代码中

 else {
    IGBT[i] = 0.0;   
    }

是一种死代码,永远不会被执行。您需要用 else if(condition) 替换 else 后跟 if 条件,以便可以执行第二个 else。

还有1件事

IGBT[i] = 1.0; // IGBT[i] is updated

        if ( IGBT[i] == 0.0){ // IGBT[i]  is compared ... wow

我建议您阅读 float 和 double 之间的比较。