Skip to content

Comparing floating point containers without Approx #3035

@hughcars

Description

@hughcars

Before the move away from Approx, the way of absolutely comparing two std::vector<double> was to my understanding

std::vector<double> sample, truth;
constexpr double delta = 1e-12;
CHECK_THAT(sample, Approx(truth).margin(delta));

using the WithinAbs comparison however, seems to require unrolling:

      for (size_t i = 0; i < sample.size(); ++i)
      {
        CHECK_THAT(sample[i],
                   Catch::Matchers::WithinAbs(truth[i], delta));
      }

or

CHECK_THAT(sample, Catch::Matchers::RangeEquals(truth, [](auto x, auto y){ return std::abs(x-y) < delta;}));

We're wanting to follow the deprecation guidance, and are moving away from Approx, but is there a recommended way of doing a floating point comparison on a container that I missed? Having a single check for a container was quite elegant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions