|
| 1 | +# utility |
| 2 | + |
| 3 | +A collection of utility functions and classes. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [concepts](#concepts) |
| 8 | +- [epsilon](#epsilon) |
| 9 | +- [fuzzy_compare](#fuzzy_compare) |
| 10 | +- [type_traits](#type_traits) |
| 11 | + |
| 12 | +<a name="concepts"></a> |
| 13 | + |
| 14 | +## [`concepts`](concepts.h) |
| 15 | + |
| 16 | +The [concepts](concepts.h) header provides a set of concepts extended the standard library. |
| 17 | + |
| 18 | +- `arithmetic`: a concept that represents a type that is either an integral or floating point type; |
| 19 | + |
| 20 | +<a name="epsilon"></a> |
| 21 | + |
| 22 | +## [`epsilon`](epsilon.h.in) |
| 23 | + |
| 24 | +The [epsilon](epsilon.h.in) is a [CMake configure file](https://cmake.org/cmake/help/latest/command/configure_file.html) |
| 25 | +that defines the epsilon value passed to CMake during project build through the `ROBOCIN_FLOAT_EPSILON`, |
| 26 | +`ROBOCIN_DOUBLE_EPSILON` and `ROBOCIN_LONG_DOUBLE_EPSILON` flags (see [CMakeLists.txt](CMakeLists.txt)). |
| 27 | + |
| 28 | +These values will be used by the library to perform real number comparisons. |
| 29 | + |
| 30 | +* The existence of these values can be verified as follows: |
| 31 | + |
| 32 | +```cpp |
| 33 | +robocin::has_epsilon<float>::value; |
| 34 | +robocin::has_epsilon<double>::value; |
| 35 | +robocin::has_epsilon<long double>::value; |
| 36 | +``` |
| 37 | + |
| 38 | +or: |
| 39 | + |
| 40 | +```cpp |
| 41 | +robocin::has_epsilon_v<float>; |
| 42 | +robocin::has_epsilon_v<double>; |
| 43 | +robocin::has_epsilon_v<long double>; |
| 44 | +``` |
| 45 | + |
| 46 | +* When provided, you can access them without using: |
| 47 | + |
| 48 | +```cpp |
| 49 | +robocin::has_epsilon<float>::epsilon; |
| 50 | +robocin::has_epsilon<double>::epsilon; |
| 51 | +robocin::has_epsilon<long double>::epsilon; |
| 52 | +``` |
| 53 | + |
| 54 | +or: |
| 55 | + |
| 56 | +```cpp |
| 57 | +robocin::epsilon_v<float>; |
| 58 | +robocin::epsilon_v<double>; |
| 59 | +robocin::epsilon_v<long double>; |
| 60 | +``` |
| 61 | + |
| 62 | +<a name="fuzzy_compare"></a> |
| 63 | + |
| 64 | +## [`fuzzy_compare`](fuzzy_compare.h) |
| 65 | + |
| 66 | +The [fuzzy_compare](fuzzy_compare.h) header provides a set of functions to perform fuzzy comparisons and functor classes |
| 67 | +to be used with the standard library algorithms. There are many ways to compare real numbers, but for the applications |
| 68 | +that RobôCIn and other robotics teams use, the way chosen is to compare the absolute difference against |
| 69 | +a [tolerance margin](#epsilon): |
| 70 | + |
| 71 | +```cpp |
| 72 | +// returns true if a floating point number 'is zero': |
| 73 | +std::abs(value) <= epsilon; |
| 74 | + |
| 75 | +// returns true if two floating point numbers 'are equal': |
| 76 | +std::abs(lhs - rhs) <= epsilon; |
| 77 | +``` |
| 78 | +
|
| 79 | +- `fuzzyIsZero`: returns true if a floating point number is close to zero; |
| 80 | +- `fuzzyCmpEqual`: returns true if two floating point numbers are close to each other; |
| 81 | +- `fuzzyCmpNotEqual`: returns true if two floating point numbers are not close to each other; |
| 82 | +- `fuzzyCmpThreeWay`: returns true if two floating point numbers are close to each other, returning a C++20 three-way |
| 83 | + comparison result (see |
| 84 | + [Three-way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison)); |
| 85 | +- `fuzzyCmpLess`: returns true if a floating point number is less than another; |
| 86 | +- `fuzzyCmpLessEqual`: returns true if a floating point number is less than or equal to another; |
| 87 | +- `fuzzyCmpGreater`: returns true if a floating point number is greater than another; |
| 88 | +- `fuzzyCmpGreaterEqual`: returns true if a floating point number is greater than or equal to another. |
| 89 | +- Functors: |
| 90 | + - `FuzzyIsZero`: functor that returns true if a floating point number is close to zero; |
| 91 | + - `FuzzyEqualTo`: functor that returns true if two floating point numbers are close to each other; |
| 92 | + - `FuzzyNotEqualTo`: functor that returns true if two floating point numbers are not close to each other; |
| 93 | + - `FuzzyThreeWay`: functor to perform a three-way comparison between two floating point numbers; |
| 94 | + - `FuzzyLess`: functor that returns true if a floating point number is less than another; |
| 95 | + - `FuzzyLessEqual`: functor that returns true if a floating point number is less than or equal to another; |
| 96 | + - `FuzzyGreater`: functor that returns true if a floating point number is greater than another; |
| 97 | + - `FuzzyGreaterEqual`: functor that returns true if a floating point number is greater than or equal to another. |
| 98 | +
|
| 99 | +> **Note**: The `fuzzy*` functions / `Fuzzy*` functors with implicit epsilon are only available when |
| 100 | +> the [epsilon](#epsilon) is defined. |
| 101 | +> Otherwise, you must explicitly pass the epsilon value to the function / during construction. |
| 102 | +
|
| 103 | +<a name="type_traits"></a> |
| 104 | +
|
| 105 | +## [`type_traits`](type_traits.h) |
| 106 | +
|
| 107 | +The [type_traits](type_traits.h) header provides a set of type traits extended the standard library. |
| 108 | +
|
| 109 | +- `common_floating_point_for_comparison`: a type trait that represents the common floating point type for comparison |
| 110 | + between two arithmetic types (the tolerance of the lowest precision floating point is prioritized); |
| 111 | + - `common_floating_point_for_comparison_t`: a helper alias for `common_floating_point_for_comparison::type`; |
0 commit comments