catchorg / catchorg/Catch2

WithinRel and numbers approaching 0.0

Open
#2,659 1 comment 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

**Describe the bug**

I love Catch2 and use it daily!

The only thing I regularly wrestle with is the usability around small numbers around zero.

For example, I just had this matcher fail:

```
REQUIRE_THAT (result, Catch::Matchers::Approx ({ 0.0, 1.0f, -0.0f, -1.0 }));
```

with this (unhelpful) expansion:

```
with expansion:
{ 0.0f, 1.0f, -0.0f, 0.0f } is approx: { 0.0f, 1.0f, -0.0f, -1.0f }
````

I wasn't sure which number was the problem, nor how much it's off by. Is it possible to add a margin here? Or to output the full representation of the number?

On additional manual inspection, negative 0 was actually `-0.0000000874228f`.

So then I wrote

```
float value = -0.0000000874228f;
REQUIRE_THAT (value, Catch::Matchers::WithinRel (0.0f, 0.001f));
```
which fails with

```
-0.0f and 0 are within 0.1% of each other
```
which is incorrect.

I moved the decimal and wrote

```
REQUIRE_THAT (value, Catch::Matchers::WithinRel (0.0f, 0.1f));
```

Which fails with

```
-0.0f and 0 are within 10% of each other
```

Maybe I'm confused about WithinRel usage?

It seems that WithinAbs is nice and happy, I'll stick with that for now. Not sure how to make the vector version happy...

**Platform information:**
- Catch version: **v3.3.2**

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.