Line | Branch | Exec | Source |
---|---|---|---|
1 | // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH | ||
2 | // | ||
3 | // SPDX-License-Identifier: MIT | ||
4 | |||
5 | #pragma once | ||
6 | |||
7 | #include <cmath> | ||
8 | |||
9 | namespace imgdoc2 | ||
10 | { | ||
11 | /// Determine whether the two specified numbers are "approximately equal". | ||
12 | /// This function uses the larger of the two values and multiples it by epsilon to determine the margin of error. | ||
13 | /// See https://www.boost.org/doc/libs/1_36_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html . | ||
14 | /// \tparam t Generic type parameter. | ||
15 | /// \param a The first number to compare. | ||
16 | /// \param b The second number to compare. | ||
17 | /// \param epsilon The factor to be multiplied with larger of the two values to give the allowed margin of error. | ||
18 | /// \returns True if numbers are "approximately equal"; false otherwise. | ||
19 | template <typename t> | ||
20 | 60 | inline bool approximatelyEqual(t a, t b, t epsilon) | |
21 | { | ||
22 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | return std::fabs(a - b) <= ((std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); |
23 | } | ||
24 | |||
25 | /// Determine whether the two specified numbers are "essentially equal". | ||
26 | /// This function uses the smaller of the two values and multiples it by epsilon to determine the margin of error. | ||
27 | /// See https://www.boost.org/doc/libs/1_36_0/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html . | ||
28 | /// \tparam t Generic type parameter. | ||
29 | /// \param a The first number to compare. | ||
30 | /// \param b The second number to compare. | ||
31 | /// \param epsilon The factor to be multiplied with smaller of the two values to give the allowed margin of error. | ||
32 | /// \returns True if numbers are "essentially equal"; false otherwise. | ||
33 | template <typename t> | ||
34 | inline bool essentiallyEqual(t a, t b, t epsilon) | ||
35 | { | ||
36 | return std::fabs(a - b) <= ((std::fabs(a) > std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); | ||
37 | } | ||
38 | |||
39 | /// Test whether the first argument is "definitely" greater than the second, i.e. the difference is greater than the specified epsilon. | ||
40 | /// \tparam t Generic type parameter. | ||
41 | /// \param a The first number to compare. | ||
42 | /// \param b The second number to compare. | ||
43 | /// \param epsilon The factor to be multiplied with smaller of the two values to give the allowed margin of error. | ||
44 | /// \returns True if first number is greater than second; false otherwise. | ||
45 | template <typename t> | ||
46 | inline bool definitelyGreaterThan(t a, t b, t epsilon) | ||
47 | { | ||
48 | return (a - b) > ((std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); | ||
49 | } | ||
50 | |||
51 | /// Test whether the first argument is "definitely" less than the second, i.e. the difference is greater than the specified epsilon. | ||
52 | /// \tparam t Generic type parameter. | ||
53 | /// \param a The first number to compare. | ||
54 | /// \param b The second number to compare. | ||
55 | /// \param epsilon The factor to be multiplied with smaller of the two values to give the allowed margin of error. | ||
56 | /// \returns True if first number is less than second; false otherwise. | ||
57 | template <typename t> | ||
58 | inline bool definitelyLessThan(t a, t b, t epsilon) | ||
59 | { | ||
60 | return (b - a) > ((std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); | ||
61 | } | ||
62 | |||
63 | } | ||
64 |