Debian Codegraph @

3rdparty/catch2/docs/comparing-floating-point-numbers.md

raw symbols SCIP
1 <a id="top"></a>
2 # Comparing floating point numbers with Catch2
3
4 If you are not deeply familiar with them, floating point numbers can be
5 unintuitive. This also applies to comparing floating point numbers for
6 (in)equality.
7
8 This page assumes that you have some understanding of both FP, and the
9 meaning of different kinds of comparisons, and only goes over what
10 functionality Catch2 provides to help you with comparing floating point
11 numbers. If you do not have this understanding, we recommend that you first
12 study up on floating point numbers and their comparisons, e.g. by [reading
13 this blog post](https://codingnest.com/the-little-things-comparing-floating-point-numbers/).
14
15
16 ## Floating point matchers
17
18 ```
19 #include <catch2/matchers/catch_matchers_floating_point.hpp>
20 ```
21
22 [Matchers](matchers.md#top) are the preferred way of comparing floating
23 point numbers in Catch2. We provide 3 of them:
24
25 * `WithinAbs(double target, double margin)`,
26 * `WithinRel(FloatingPoint target, FloatingPoint eps)`, and
27 * `WithinULP(FloatingPoint target, uint64_t maxUlpDiff)`.
28
29 > `WithinRel` matcher was introduced in Catch2 2.10.0
30
31 As with all matchers, you can combine multiple floating point matchers
32 in a single assertion. For example, to check that some computation matches
33 a known good value within 0.1% or is close enough (no different to 5
34 decimal places) to zero, we would write this assertion:
35
36 ```cpp
37 REQUIRE_THAT( computation(input),
38 Catch::Matchers::WithinRel(expected, 0.001)
39 || Catch::Matchers::WithinAbs(0, 0.000001) );
40 ```
41
42
43 ### WithinAbs
44
45 `WithinAbs` creates a matcher that accepts floating point numbers whose
46 difference with `target` is less-or-equal to the `margin`. Since `float`
47 can be converted to `double` without losing precision, only `double`
48 overload exists.
49
50 ```cpp
51 REQUIRE_THAT(1.0, WithinAbs(1.2, 0.2));
52 REQUIRE_THAT(0.f, !WithinAbs(1.0, 0.5));
53 // Notice that infinity == infinity for WithinAbs
54 REQUIRE_THAT(INFINITY, WithinAbs(INFINITY, 0));
55 ```
56
57
58 ### WithinRel
59
60 `WithinRel` creates a matcher that accepts floating point numbers that
61 are _approximately equal_ to the `target` with a tolerance of `eps.`
62 Specifically, it matches if
63 `|arg - target| <= eps * max(|arg|, |target|)` holds. If you do not
64 specify `eps`, `std::numeric_limits<FloatingPoint>::epsilon * 100`
65 is used as the default.
66
67 ```cpp
68 // Notice that WithinRel comparison is symmetric, unlike Approx's.
69 REQUIRE_THAT(1.0, WithinRel(1.1, 0.1));
70 REQUIRE_THAT(1.1, WithinRel(1.0, 0.1));
71 // Notice that inifnity == infinity for WithinRel
72 REQUIRE_THAT(INFINITY, WithinRel(INFINITY));
73 ```
74
75
76 ### WithinULP
77
78 `WithinULP` creates a matcher that accepts floating point numbers that
79 are no more than `maxUlpDiff`
80 [ULPs](https://en.wikipedia.org/wiki/Unit_in_the_last_place)
81 away from the `target` value. The short version of what this means
82 is that there is no more than `maxUlpDiff - 1` representable floating
83 point numbers between the argument for matching and the `target` value.
84
85 When using the ULP matcher in Catch2, it is important to keep in mind
86 that Catch2 interprets ULP distance slightly differently than
87 e.g. `std::nextafter` does.
88
89 Catch2's ULP calculation obeys these relations:
90 * `ulpDistance(-x, x) == 2 * ulpDistance(x, 0)`
91 * `ulpDistance(-0, 0) == 0` (due to the above)
92 * `ulpDistance(DBL_MAX, INFINITY) == 1`
93 * `ulpDistancE(NaN, x) == infinity`
94
95
96 **Important**: The WithinULP matcher requires the platform to use the
97 [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754) representation for
98 floating point numbers.
99
100 ```cpp
101 REQUIRE_THAT( -0.f, WithinULP( 0.f, 0 ) );
102 ```
103
104
105 ## `Approx`
106
107 ```
108 #include <catch2/catch_approx.hpp>
109 ```
110
111 **We strongly recommend against using `Approx` when writing new code.**
112 You should be using floating point matchers instead.
113
114 Catch2 provides one more way to handle floating point comparisons. It is
115 `Approx`, a special type with overloaded comparison operators, that can
116 be used in standard assertions, e.g.
117
118 ```cpp
119 REQUIRE(0.99999 == Catch::Approx(1));
120 ```
121
122 `Approx` supports four comparison operators, `==`, `!=`, `<=`, `>=`, and can
123 also be used with strong typedefs over `double`s. It can be used for both
124 relative and margin comparisons by using its three customization points.
125 Note that the semantics of this is always that of an _or_, so if either
126 the relative or absolute margin comparison passes, then the whole comparison
127 passes.
128
129 The downside to `Approx` is that it has a couple of issues that we cannot
130 fix without breaking backwards compatibility. Because Catch2 also provides
131 complete set of matchers that implement different floating point comparison
132 methods, `Approx` is left as-is, is considered deprecated, and should
133 not be used in new code.
134
135 The issues are
136 * All internal computation is done in `double`s, leading to slightly
137 different results if the inputs were floats.
138 * `Approx`'s relative margin comparison is not symmetric. This means
139 that `Approx( 10 ).epsilon(0.1) != 11.1` but `Approx( 11.1 ).epsilon(0.1) == 10`.
140 * By default, `Approx` only uses relative margin comparison. This means
141 that `Approx(0) == X` only passes for `X == 0`.
142
143
144 ### Approx details
145
146 If you still want/need to know more about `Approx`, read on.
147
148 Catch2 provides a UDL for `Approx`; `_a`. It resides in the `Catch::literals`
149 namespace, and can be used like this:
150
151 ```cpp
152 using namespace Catch::literals;
153 REQUIRE( performComputation() == 2.1_a );
154 ```
155
156 `Approx` has three customization points for the comparison:
157
158 * **epsilon** - epsilon sets the coefficient by which a result
159 can differ from `Approx`'s value before it is rejected.
160 _Defaults to `std::numeric_limits<float>::epsilon()*100`._
161
162 ```cpp
163 Approx target = Approx(100).epsilon(0.01);
164 100.0 == target; // Obviously true
165 200.0 == target; // Obviously still false
166 100.5 == target; // True, because we set target to allow up to 1% difference
167 ```
168
169
170 * **margin** - margin sets the absolute value by which
171 a result can differ from `Approx`'s value before it is rejected.
172 _Defaults to `0.0`._
173
174 ```cpp
175 Approx target = Approx(100).margin(5);
176 100.0 == target; // Obviously true
177 200.0 == target; // Obviously still false
178 104.0 == target; // True, because we set target to allow absolute difference of at most 5
179 ```
180
181 * **scale** - scale is used to change the magnitude of `Approx` for the relative check.
182 _By default, set to `0.0`._
183
184 Scale could be useful if the computation leading to the result worked
185 on a different scale than is used by the results. Approx's scale is added
186 to Approx's value when computing the allowed relative margin from the
187 Approx's value.
188
189
190 ---
191
192 [Home](Readme.md#top)