catchorg / catchorg/Catch2

Comparing floating point containers without `Approx`

Open
#3,035 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

Before the move away from [`Approx`](https://catch2-temp.readthedocs.io/en/latest/comparing-floating-point-numbers.html#approx), the way of absolutely comparing two `std::vector` was to my understanding
```
std::vector 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.