0

Is there a way to change the precision of the predefined <= and < in the comparison between std::vector<double> vectors lexicographically ?

I am comparing between std::vector<double> vectors lexicographically in many places in my code, the scale of the first component (close to 0) is different from the scale of the other components (between -700 and 700). I want the precision to be 1e-6, i.e for two components a and b if abs(a-b)<=1e-6 then we consider a=b, where a, b are double.

Since I used <= and < in many places in the code, defining a new function that replaces <= and < to compare between vectors is risky (I make skip some vectors), so I am wondering if it is possible to change the precision of <= and < so this change will apply to all the comparisons directly.

An example of vectors I have: A=(-2.6666666666666936, 33497.435897435964, -300.51282051282101), B=(-2.6666666666666914, 17403.589743589808, -251.28205128205173), the result by using <=, is that A<=B because of the first component, but in my case, the first components are equal with (with epsilon=1e-6) so A>B.

Emy John
  • 1
  • 1
  • Please supply example(s) of behaviour change you're after. – meaning-matters Sep 09 '22 at 16:08
  • 2
    This will result in problems, since the equality is no longer transitive. Consider values `a`, `b = a + 0.9e-6` and `c = a + 1.8e-6`. You've got `a == b` and `b == c` but `a != c`. – fabian Sep 09 '22 at 16:10
  • "defining a new function that replaces <= and < [...] is risky" I'd say the opposite. Using `<=` to mean something else than `<=` is risky. One reason is what fabian said. On the other hand it is unclear why you want to change how `<` works. Even if two floating point values carray some error then nevertheless `a < b` when a is smaller than `b` – 463035818_is_not_an_ai Sep 09 '22 at 16:22
  • "where a, b in R." do you mean real numbers with "R" ? Dont confuse floating point numbers with mathematical real numbers. They are very different. – 463035818_is_not_an_ai Sep 09 '22 at 16:24
  • @meaning-matters I added an example. – Emy John Sep 09 '22 at 17:18
  • @463035818_is_not_a_number I want to change the precision because the components have different scales as shown in the example I added to the description, a difference at 1e-15 of the first component has a huge impact on the comparison since for the second component the difference is huge. For your second question I meant double numbers – Emy John Sep 09 '22 at 17:22
  • You won't be able to change how the comparison operators work when both operands are basic types like `double`. You would be able to change how comparisons work for class types, such as `std::vector` (in which case, the comparison operation might loop over elements comparing them in any way you like). – Peter Sep 10 '22 at 00:17
  • @Peter can you show me please how to change how the class std::vector works? – Emy John Sep 10 '22 at 07:58

1 Answers1

1

There is no good way to change the precision of the operators.

I'd suggest you write your own function that iterates over the two vectors and does the comparisons directly. Something like:

bool approxLessThan(
        const std::vector<double>& a,
        const std::vector<double>& b,
        double tolerance) {
    // feel free to handle this differently
    assert(a.size() == b.size());

    for (size_t i =0; i < a.size(); i++) {
        double dif = a[i] - b[i];
        if (std::abs(dif) > tolerance)
            return dif < 0.0; // change this to <= as needed
    }
    return false; // The vectors are "equal"
}

You can expand this to handle vectors of different sizes if needed.

Dean Johnson
  • 1,682
  • 7
  • 12