1

I wrote some code with segments on a line and I strive to save maximum precision from the inputs. Do I have to hold coordinates of both ends of the segment or is start and length enough? I have to have the length if I want to keep my current nice architecture, thus the desire to omit the right end.

Or formally as stated in the question:

Let's say I have 0 <= a <= b What are the cases when (b - a) + a will not yield exactly b?

unkulunkulu
  • 10,526
  • 2
  • 27
  • 47
  • They'll be different when `(b - a) + a != b + (-a + a) = b`. There are already a lot of examples on SO: [Example of non associative floating point addition](https://stackoverflow.com/q/46769671/995714), [Associative Property on floating point number additions](https://stackoverflow.com/q/47825436/995714), [Is Floating point addition and multiplication associative?](https://stackoverflow.com/q/10371857/995714), [Associativity math: (a + b) + c != a + (b + c)](https://stackoverflow.com/q/32004190/995714) – phuclv Jul 17 '18 at 09:14
  • @phuclv An answer to this question would be an answer to the non-associativity as well, but not necessarily vice versa. If you think that some of the examples in the linked questions can be used to construct an example for this one, please go ahead and provide an answer as I believe this is a different question in general. – unkulunkulu Jul 17 '18 at 09:47
  • it's just the same question as I proved above, `(b - a) + a` only equals to `b` when it equals to `b + (-a + a)`, which in turn is only satisfied if floating-point math is associative – phuclv Jul 17 '18 at 09:52
  • But floating point is not "never associative", there are examples when it is (lets's say 1.0 + 2.0 + 3.0 or let's say for sufficiently small whole numbers). My question is more restricted than the general case (In short show me an example for my problem if you think it's that easy). – unkulunkulu Jul 17 '18 at 09:55
  • 1
    It is easy. Here an example for IEEE-binary64 (if I did not make any mistakes): If `a = 1/7 = 3FE2492492492492` and `b = 5/7 = 3FE6DB6DB6DB6DB7` then `(b-a) + a = 3FE6DB6DB6DB6DB6`. – gammatester Jul 17 '18 at 10:45
  • @phuclv: `b + (-a + a) = b` is always true (unless `a` or `b` is a NaN) because `(-a+a)` is zero. `(b - a) + a` is not equivalent to `b + (-a + a)`. – Eric Postpischil Jul 17 '18 at 11:27
  • @phuclv: Your first link, in spite of its title, asks about commutativity, not associativity. Your other links, including the answers in them, do not speak specifically to the question asked here, as they discuss the lack of the associative property in floating-point generally but do not characterize the failures. – Eric Postpischil Jul 17 '18 at 11:29
  • @EricPostpischil yes, that's why I wrote `b + (-a + a) = b` above. I'm talking about a mathematical proof and not the floating-point proof here. If `(b - a) + a != b + (-a + a)` then `(b - a) + a != b`. And [there's an example in the first link](https://stackoverflow.com/a/46776601/995714) – phuclv Jul 17 '18 at 11:30
  • @phuclv: What useful information does that provide? It merely says that `(b-a)+a` does not equal `b` when it does not equal `b`. The question asks to characterize those cases, but this statement does nothing to advance that goal. – Eric Postpischil Jul 17 '18 at 11:31
  • @unkulunkulu: I doubt there is an easy way to completely characterize the cases for which `(b-a)+a` does not equal `b`, because many of them will depend on whether some number of low bits in the significand of `a` are non-zero, depending on the relative magnitudes of `a` and `b`. A broad category of cases for which the answer is exact can be characterized with Sterbenz’s lemma, namely a/2 ≤ b ≤ 2a. Everything satisfying that has an exact `(b-a)+a`. But there are many values that do not satisfy that but are exact, but they are irregular in the sense no simple algebraic property describes them. – Eric Postpischil Jul 17 '18 at 11:53
  • @gammatester that's cool, thank you, that would satisfy me as an answer to this question – unkulunkulu Jul 17 '18 at 12:20
  • @EricPostpischil thank you, that makes sense, I'm not looking into the complete criterion, just trying to make a sense of simple cases when it breaks. The 5/7 - 1/7 example is ok for me, somehow I haven't tried this. – unkulunkulu Jul 17 '18 at 12:25

1 Answers1

1

To extend my comment, I tested small fractions up to a limit n, i.e. a_i = i/n, i=1..n and b_j = j/n, j=i+1..n. It seems that for roughly 4 .. 5 percent of the test cases the equality failed. Here some numbers:

   n   failed    tested     f/t
  10        2        55    0.036
  50       48      1275    0.038
 100      204      5050    0.040
 127      383      8128    0.047
2047   114752   2096128    0.055

The failed cases for n=10 are a=2/10, b=9/10 and a=3/10, b=9/10.

gammatester
  • 1,091
  • 1
  • 8
  • 12