I know that the operator == can return false on two double (or float) values with the same content, cf c++ comparison of two double values not working properly, but can the greater than operator (>) return true on the same value as well ?
Asked
Active
Viewed 334 times
-3
-
2The linked questions -> they are two *different* values (and they get that way through calculation differences). The conclusion that the *same* value (of a float, excluding NaN) is not equal to itself is wrong. Likewise, if the target results in an upward-to-infinity bias (to a *different* value then it is "greater" than it was before).. – user2864740 Mar 10 '15 at 22:36
-
@juanchopanza He didn't mean it that way. He's refering to truncation and rounding errors – Mar 10 '15 at 22:37
-
2It can be if the value gets saved to a register and then read back in, possibly resulting in a loss of precision. – David Schwartz Mar 10 '15 at 22:37
-
proper answer - it doesn't matter. just ignore it. – iced Mar 10 '15 at 22:38
-
2@juanchopanza Counter-example: `double dbl = 0.0 / 0.0;` – Konrad Rudolph Mar 10 '15 at 22:38
-
2The OP needs to more clearly define what they mean by "same content". The post linked to involves rounding from calculations, not "same content" in the literal sense. If OP doesn't inherently understand how floating point works, then this is the correct answer to the question :) - http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Jim Buck Mar 10 '15 at 22:43
-
@KerrekSB If both were NaN, could that be considered the same content? This is getting metaphysical. – juanchopanza Mar 10 '15 at 22:45
-
@DavidSchwartz That looks about right, and I encourage you to post it as an answer. It would also be useful to explain when it's possible, and when excess precision is guaranteed to get discarded. (I'd post it myself, but I'm actually not familiar with the C++ rules about it, only with the C rules, and they seem to be slightly different.) – Mar 10 '15 at 22:51
-
@juanchopanza cant NaN be actually represented in more than one way tho?(I am strongly positive that it can) – Creris Mar 10 '15 at 23:01
-
@juanchopanza: you're right, NaN always compares unequal to anything else; comment removed. – Kerrek SB Mar 10 '15 at 23:27
1 Answers
1
Your question seems poorly worded, but let me try anyway. Code like
double x = 0.6;
double y = 0.1 + 0.2 + 0.3;
May result in x == y being true or false. If you print x and y with 6 digits of precision they may appear to have the same value, but the machine stores more than 6 digits. You can see the full value by doing something like
union {
double d;
char hex[ sizeof(double) ];
} u;
u.d = x; print u.hex[]
u.d = y; print u.hex[]
Many times the hex[] values will not match, and then operator == will return false. Usually if == returns false, then either > or < will be true. There are obscure exceptions.

brian beuning
- 2,836
- 18
- 22