【问题标题】:ANSI C - Comparison OddityANSI C - 比较奇数
【发布时间】:2012-05-18 17:05:01
【问题描述】:

我正在尝试比较整数和双精度:

printf("%d\n", pos<(td+tr));
            if(td <= pos < (td+tr))
            {}

打印语句正确评估比较pos&lt;(td+tr)if(td &lt;= pos &lt; (td+tr)) 比较未正确评估。

Pos 是一个整数:int pos; td 和 tr 是双精度数:double td,tr;

【问题讨论】:

    标签: c compare


    【解决方案1】:
    td <= pos < (td+tr)
    

    从左到右进行评估。所以首先

    td <= pos
    

    被评估为真值。然后将该真值与td+tr 进行比较。

    这不是你想要的。你想要的

    if (td <= pos && pos < td+tr)
    

    【讨论】:

      【解决方案2】:

      你的代码没有做你可能认为它做的事情,也许你需要

      td <= pos && pos < (td+tr)
      

      ?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 2018-06-02
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2010-12-09
        相关资源
        最近更新 更多