Skip to content

Commit e4f4335

Browse files
committed
Improved Approx documentation
1 parent 8c07899 commit e4f4335

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

docs/assertions.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@ Catch provides a way to perform tolerant comparisons of floating point values th
5353
REQUIRE( performComputation() == Approx( 2.1 ) );
5454
```
5555

56-
By default a small epsilon value is used that covers many simple cases of rounding errors. When this is insufficent the epsilon value (the amount within which a difference either way is ignored) can be specified by calling the ```epsilon()``` method on the ```Approx``` instance. e.g.:
56+
This way `Approx` is constructed with reasonable defaults, covering most simple cases of rounding errors. If these are insufficient, each `Approx` instance has 3 tuning knobs, that can be used to customize it for your computation.
5757

58+
* __epsilon__ - epsilon serves to set the percentage by which a result can be erroneous, before it is rejected. By default set to `std::numeric_limits<float>::epsilon()*100`.
59+
* __margin__ - margin serves to set the the absolute value by which a result can be erroneous before it is rejected. By default set to `0.0`.
60+
* __scale__ - scale serves to adjust the base for comparison used by epsilon, can be used when By default set to `1.0`.
61+
62+
#### epsilon example
63+
```cpp
64+
Approx target = Approx(100).epsilon(0.01);
65+
100.0 == target; // Obviously true
66+
200.0 == target; // Obviously still false
67+
100.5 == target; // True, because we set target to allow up to 1% error
5868
```
59-
REQUIRE( 22/7 == Approx( 3.141 ).epsilon( 0.01 ) );
69+
70+
#### margin example
71+
_Margin check is used only if the relative (epsilon and scale based) check fails._
72+
```cpp
73+
Approx target = Approx(100).margin(5);
74+
100.0 == target; // Obviously true
75+
200.0 == target; // Obviously still false
76+
104.0 == target; // True, because we set target to allow absolute error up to 5
6077
```
6178

62-
When dealing with very large or very small numbers it can be useful to specify a scale, which can be achieved by calling the ```scale()``` method on the ```Approx``` instance.
79+
#### scale
80+
Scale can be useful if the computation leading to the result worked on different scale, than is used by the results (and thus expected errors are on a different scale than would be expected based on the results alone).
81+
6382

6483
## Exceptions
6584

0 commit comments

Comments
 (0)