If I copy a float to another variable, will they be equal?

后端 未结 5 1049
猫巷女王i
猫巷女王i 2021-01-30 00:11

I know that using == to check equality of floating-point variables is not a good way. But I just want to know that with the following statements:



        
5条回答
  •  遥遥无期
    2021-01-30 00:37

    Yes, y will assuredly take on the value of x:

    [expr.ass]/2: In simple assignment (=), the object referred to by the left operand is modified ([defns.access]) by replacing its value with the result of the right operand.

    There is no leeway for other values to be assigned.

    (Others have already pointed out that an equivalence comparison == will nonetheless evaluate to false for NaN values.)

    The usual issue with floating-point == is that it's easy to not have quite the value you think you do. Here, we know that the two values, whatever they are, are the same.

提交回复
热议问题