2 ms·
Is if (costs[j+k] - costs[j] <= mincost) really the same as if (costs[j+k] <= mincost + costs[j]) even with floating point math?
by adrianN 11y ago
Is
if (costs[j+k] - costs[j] <= mincost)
really the same as
if (costs[j+k] <= mincost + costs[j])
even with floating point math?
- robryk 11y agoMy first guess is that it should be (because addition on FP gives the nearest representable value), but I'd also guess that any small differences here are irrelevant. I wonder how many of those optimizations (e.g. maybe this exact one) would be done by compilers with -ffast-math.
- jtlim 11y agoThis is a good question. I pondered about it myself. I could prove that a - b < c is not the same as a < b + c (large a, a=c,, b the minimum value to cause a-b change --> would result in first being true, 2nd being false) But I couldn't think of a failure case for a - b <= c not being the same as a <= b + c. Would love it if someone could verify/prove this. To further complicate it, the LHS of the code in question is a float evaluation, that is then upcast to a double to compare with the right (which is a double).