Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试比较整数和双精度:
printf("%d\n", pos<(td+tr)); if(td <= pos < (td+tr)) {}
print 语句pos<(td+tr)正确地评估比较。比较if(td <= pos < (td+tr))未正确评估。
pos<(td+tr)
if(td <= pos < (td+tr))
Pos 是一个 int:int pos; td 和 tr 是双精度数:double td,tr;
int pos;
double td,tr;
td <= pos < (td+tr)
从左到右进行评估。所以首先
td <= pos
被评估为真值。然后将该真值与 进行比较td+tr。
td+tr
那不是你想要的。你要
if (td <= pos && pos < td+tr)
您的代码没有做您可能认为的事情,也许您需要
td <= pos && pos < (td+tr)
?