c++ - Can we assume any pair of following floating point arithmetic statements always produce identical result? -
Looking at the variables of floating points with arbitrary value, in c / c ++
float a, b, c, d; In the following statements, can we assume any pair of them, produce similar results?
Float result_1 = a + b + c + d - c; Float result_2 = a + b + c + (d - c); Float result_3 = a + b + d; In addition, there is no guarantee of the following decisions: a + b - b == a
No, you can not accept it. I broke all three of your examples:
#include & lt; Iostream & gt; Int main () {Double A = 1, B = 1 e 100, C = 1a 100, D = 1, C2 = 1, D2 = -1, B2 = 1; Std :: cout & lt; & Lt; (A + b2 + c2 + d2 - c2 == a + b2 + c2 + (d2 - c2)) & lt; & Lt; "\ N" & lt; & Lt; (A + b2 + c + d - c == a + b2 + d) & lt; & Lt; "\ N" & lt; & Lt; (A == A + B-B); } Output:
0
== and ! = Always unsafe on floating point types because they have rounding errors.
Comments
Post a Comment