catchorg / catchorg/Catch2

Add floating-point matcher similar to numpy's and pytorch's isclose

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

Description

**Description**
Add a floating-point matcher similar to [numpy](https://numpy.org/doc/stable/reference/generated/numpy.isclose.html)'s and [pytorch](https://pytorch.org/docs/stable/generated/torch.isclose.html)'s `isclose`, testing `abs(a - b) <= (atol + rtol * abs(b))`
with the same defaults `rtol=1e-05, atol=1e-08`

**Additional context**
`isclose` is useful for matching a floating-point value to a target reference floating-point value and is a de-facto standard. It would be useful to be able to make the equivalent comparisons in Catch2 out of the box.

The matcher would behave similar to `Approx`. However, referencing `Approx`'s [issues](https://github.com/catchorg/Catch2/blob/devel/docs/comparing-floating-point-numbers.md#approx):
- _All internal computation is done in `double`s, leading to slightly different results if the inputs were floats:_ In the new matcher we can just avoid this issue.
- _`Approx`'s relative margin comparison is not symmetric:_ This is not an issue, but the desired behavior for this matcher. (the [existing matchers](https://github.com/catchorg/Catch2/blob/devel/docs/comparing-floating-point-numbers.md#floating-point-matchers) are OK when symmetrical behavior is [desired](https://github.com/catchorg/Catch2/issues/1746)).
- _By default, `Approx` only uses relative margin comparison. This means that `Approx(0) == X` only passes for `X == 0`:_ This is avoided with the default values.
- `Approx` is not a matcher

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.