gpt4 book ai didi

logic - 简化 if 语句?

转载 作者:行者123 更新时间:2023-12-02 05:50:18 29 4
gpt4 key购买 nike

是否可以简化这个 if 语句?

如果是,答案是什么?

    if (type)
{
if(NdotL >= 0.0)
{
color += Idiff + Ispec;
}
}
else
{
color += Idiff + Ispec;
}

最佳答案

从 bool 代数的角度考虑这个问题。你有两个条件

A = (type)
B = (NdotL >= 0.0 )

当你执行你的声明时

A * B
/A

(我用/A表示“NOT A”,用*表示“AND”)

所以你唯一不执行的时间是

A * /B

这意味着你的声明应该是

if (!((type) && NdotL < 0.0 )) {
// do your thing
}

或者,使用 bool 恒等式

(A * B) = /(/A + /B)

您可以将条件重写为

( /A + B )

if ( !(type) || ( NdotL >= 0 ) ) {
// do your thing
}

关于logic - 简化 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275774/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com