testing_floating_points.qbk 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. [/
  2. / Copyright (c) 2003-2015 Boost.Test contributors
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [/ ################################################ ]
  8. [section:floating_point Floating point comparison]
  9. Unless specified otherwise, when a value of floating-point type is compared inside a __BOOST_TEST__ assertion,
  10. operators `==`, `!=` , `<` etc. defined for this type are used. However for floating point type, in most cases what is needed is not an ['exact]
  11. equality (or inequality), but a verification that two numbers are ['sufficiently close] or ['sufficiently different]. For that purpose, a [*tolerance] parameter
  12. that will instruct the framework what is considered ['sufficiently close] needs to provided.
  13. [note
  14. How the tolerance parameter is processed in detail is described [link boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl here].
  15. ]
  16. [h4 Test-unit tolerance]
  17. It is possible to define a per-[link ref_test_unit test unit] tolerance for a given floating point type by using
  18. [link boost_test.tests_organization.decorators decorator] __decorator_tolerance__:
  19. [bt_example tolerance_01..specifying tolerance per test case..run-fail]
  20. [h4 Assertion tolerance]
  21. It is possible to specify floating point comparison tolerance per single assertion, by providing the ['manipulator] [funcref boost::test_tools::tolerance]
  22. as the second argument to __BOOST_TEST__:
  23. [bt_example tolerance_02..specifying tolerance per assertion..run-fail]
  24. [caution Manipulators requires a compiler that supports variadic macros, `auto` for type deduction
  25. and `decltype`. These are C++11 features, but are also available on some pre-C++11 compilers. On compilers that are
  26. lacking these features, resort to defining tolerance per test unit or to compatibility test assertions: __BOOST_CHECK_CLOSE__ and __BOOST_CHECK_SMALL__.]
  27. [h4 Tolerance expressed in percentage]
  28. It is possible to specify the tolerance as percentage. At test unit level, the decorator syntax is:
  29. ```
  30. * boost::unit_test::tolerance( boost::test_tools::fpc::percent_tolerance(2.0) )
  31. // equivalent to: boost::unit_test::tolerance( 2.0 / 100 )
  32. ```
  33. At assertion level, the manipulator syntax is:
  34. ```
  35. 2.0% boost::test_tools::tolerance()
  36. boost::test_tools::tolerance( boost::test_tools::fpc::percent_tolerance(2.0) )
  37. // both equivalent to: boost::test_tools::tolerance( 2.0 / 100 )
  38. ```
  39. [h4 Type of the tolerance]
  40. Manipulator `tolerance` specifies the tolerance only for a single floating-point type. This type is deduced from form
  41. the numeric value passed along the manipulator:
  42. [table
  43. [[expression][semantics]]
  44. [[`tolerance(0.5)`][tolerance for type `double` changed to 0.5]]
  45. [[`tolerance(float(0.5))`][tolerance for type `float` changed to 0.5]]
  46. [[`tolerance(0.5f)`][tolerance for type `float` changed to 0.5]]
  47. [[`tolerance(0.5L)`][tolerance for type `long double` changed to 0.5]]
  48. [[`tolerance(Decimal("0.5"))`][tolerance for a user-defined type `Decimal` changed to the supplied value]]
  49. [[`5.0% tolerance()`][tolerance for type `double` changed to 0.05 (`5.0 / 100`)]]
  50. [[`5.0f% tolerance()`][tolerance for type `float` changed to 0.05]]
  51. [[`Decimal("5.0")% tolerance()`][tolerance for type `Decimal` changed to value `(Decimal("5.0") / 100)`]]
  52. ]
  53. This is also the case for decorator `tolerance`. In the case of the decorator however, it is possible to apply multiple
  54. decorators `tolerance` defining the tolerance for different types.
  55. When values of two different floating point types `T` and `U` are compared, __BOOST_TEST__ uses the tolerance
  56. specified for type `boost::common_type<T, U>::type`. For instance, when setting a tolerance for mixed `float`-to-`double` comparison,
  57. the tolerance for type `double` needs to be set.
  58. Given two floating point types `T` and `U` and their common type `C`, the tolerance specified for type `C` is applied only when
  59. types `T` and `U` appear as sub-expressions of the full expression inside assertion __BOOST_TEST__. It is not applied when
  60. `T` and `U` are compared inside a function invoked during the evaluation of the expression:
  61. [bt_example tolerance_05..tolerance applied to different types..run-fail]
  62. [h4 Type promotion of the operands]
  63. Given two types `T` and `U` being compared inside an assertion __BOOST_TEST__, tolerance based comparison is invoked
  64. # whenever the types `T` and `U` are both [link boost_test.testing_tools.extended_comparison.floating_point.customizing_for_tolerance tolerance based] types
  65. # whenever `T` is /tolerance/ based and `U` is /arithmetic/, in the sense that `std::numeric_limits<U>::value` evaluates to `true` (or the other way round)
  66. In all cases, the type of the tolerance is deduced as `boost::common_type<T, U>::type`, and both type may be cast to this tolerance type.
  67. [note This behavior has been introduced in Boost 1.70 / __UTF__ [link ref_CHANGE_LOG_3_10 3.10]. Previously tolerance based comparison was used only when the type of the two
  68. operands were tolerance based types, which was silently ignoring the tolerance for expressions such as
  69. ``
  70. double x = 1E-9;
  71. BOOST_TEST(x == 0); // U is int
  72. ``
  73. ]
  74. [bt_example tolerance_06..operands type promotion..run-fail]
  75. [h4 Other relational operators]
  76. Finally, note that comparisons for tolerance are also applied to `operator<` with semantics ['less by more than some tolerance],
  77. and other relational operators. Also, the tolerance-based comparisons are involved when a more complicated expression tree is
  78. processed within the assertion body. The section on
  79. [link boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl.tolerance_in_operator relational operators]
  80. defines how `operator<` relates to tolerance.
  81. [bt_example tolerance_03..tolerance applied in more complex expressions..run-fail]
  82. [/############################################################################]
  83. [section:customizing_for_tolerance Enabling tolerance for user-defined types]
  84. The __UTF__ recognizes that a given type `T` is suitable for tolerance-based comparisons using the expression
  85. [classref boost::math::fpc::tolerance_based]`<T>::value`. This meta-function already returns `true` for built-in
  86. floating-point types as well as any other types that match the following compile-time expression:
  87. ```
  88. boost::is_floating_point<T>::value ||
  89. ( std::numeric_limits<T>::is_specialized &&
  90. !std::numeric_limits<T>::is_integer &&
  91. !std::numeric_limits<T>::is_exact)
  92. ```
  93. If you require your type to also participate in tolerance-based comparisons, regardless of the above expression,
  94. you can just specialize [classref boost::math::fpc::tolerance_based] for your type directly, and derive it from
  95. `boost::true_type`. Your type does not even have to be a floating-point type provided that it models concept
  96. [link boost_test.testing_tools.extended_comparison.floating_point.customizing_for_tolerance.concept_tolerance_based `ToleranceCompatible`].
  97. [bt_example tolerance_04..adapting user-defined types for tolerance-based comparison..run-fail]
  98. [h3:concept_tolerance_based Concept `ToleranceCompatible`]
  99. [h4 Refinement of]
  100. [@https://en.cppreference.com/w/cpp/named_req/MoveConstructible `MoveConstructible`],
  101. [@https://en.cppreference.com/w/cpp/named_req/EqualityComparable `EqualityComparable`],
  102. [@https://en.cppreference.com/w/cpp/named_req/LessThanComparable `LessThanComparable`]
  103. [h4 Notation]
  104. [table
  105. [[][]]
  106. [[`T`][A type that is a model of `ToleranceCompatible`]]
  107. [[`x`, `y`][objects of type `T`]]
  108. [[`i`, `j`][objects of type `int`]]
  109. ]
  110. [h4 Valid expressions]
  111. [table
  112. [[Name][Expression][Return type]]
  113. [[Conversion from `int`][`T j = i;`][]]
  114. [[Addition][`x + y`][`T`]]
  115. [[Subtraction][`x - y`][`T`]]
  116. [[Negation][`-x`][`T`]]
  117. [[Multiplication][`x * y`[br]`x * i`][`T`]]
  118. [[Division][`x / y`[br]`x / i`][`T`]]
  119. [[Mixed equality][`x == i`[br]`x != i`][`bool`]]
  120. [[Mixed ordering][`x < i`[br]`x > i`[br]`x <= i`[br]`x >= i`][`bool`]]
  121. ]
  122. [h4 Invariants]
  123. [table
  124. [[`T` and `int` consistency][`(x == T(i)) == (x == i)`[br]`(x != T(i)) == (x != i)`[br]`(x < T(i)) == (x < i)`[br]`(x > T(i)) == (x > i)`[br]`(x / T(i)) == (x / i)`[br]`(x * T(i)) == (x * i)`]]
  125. ]
  126. [endsect] [/ customizing_for_tolerance]
  127. [/############################################################################################]
  128. [section:floating_points_comparison_impl Tolerance-based comparisons]
  129. Assertions in the __UTF__ use two kinds of comparison. For `u` being close to zero with absolute tolerance `eps`:
  130. ``
  131. abs(u) <= eps; // (abs)
  132. ``
  133. For `u` and `v` being close with relative tolerance `eps`:
  134. ```
  135. abs(u - v)/abs(u) <= eps
  136. && abs(u - v)/abs(v) <= eps; // (rel)
  137. ```
  138. For rationale for choosing these formulae, see section __floating_points_testing_tools__.
  139. Assertion __BOOST_TEST__ (when comparing floating-point numbers) uses the following algorithm:
  140. * When either value `u` or `v` is zero, evaluates formula (abs) on the other value.
  141. * When the specified tolerance is zero, performs direct (native) comparison between `u` and `v`.
  142. * Otherwise, performs formula (rel) on `u` and `v`.
  143. [note Therefore in order to check if a number is close to zero with tolerance, you need to type:
  144. ```
  145. BOOST_TEST(v == T(0), tt::tolerance(eps));
  146. ```]
  147. The compatibility assertions __BOOST_LEVEL_CLOSE__ and __BOOST_LEVEL_CLOSE_FRACTION__ perform formula (rel).
  148. The compatibility assertion __BOOST_LEVEL_SMALL__ performs formula (abs).
  149. The __UTF__ also provides unary predicate [classref boost::math::fpc::small_with_tolerance `small_with_tolerance`] and binary predicate predicate
  150. [classref boost::math::fpc::close_at_tolerance `close_at_tolerance`] that implement formula (abs) and (rel) respectively.
  151. [h3 Tolerance in `operator<`]
  152. Tolerance-based computations also apply to `operator<` and other relational operators. The semantics are defined as follows:
  153. * ['less-at-tolerance] <==> ['strictly-less] and not ['close-at-tolerance]
  154. * ['greater-at-tolerance] <==> ['strictly-greater] and not ['close-at-tolerance]
  155. * ['less-or-equal-at-tolerance] <==> ['strictly-less] or ['close-at-tolerance]
  156. * ['greater-or-equal-at-tolerance] <==> ['strictly-greater] or ['close-at-tolerance]
  157. [note This implies that the exactly one of these: `u < v`, `u == v`, `u > v`, passes with __BOOST_TEST__ at any given tolerance.]
  158. [caution Relation ['less-at-tolerance] is not a ['Strict Weak Ordering] as it lacks the ['transitivity of the equivalence];
  159. using it as predicate in `std::map` or any order-based STL
  160. algorithm would result in undefined behavior.]
  161. [endsect] [/ floating_points_comparison_impl]
  162. [/############################################################################################]
  163. [section:floating_points_comparison_theory Theory behind floating point comparisons]
  164. The following is the most obvious way to compare two floating-point values `u` and `v` for being close at a given absolute tolerance `epsilon`:
  165. [#equ1]
  166. ``
  167. abs(u - v) <= epsilon; // (1)
  168. ``
  169. However, in many circumstances, this is not what we want. The same absolute tolerance value `0.01` may be too small to meaningfully compare
  170. two values of magnitude `10e12` and at the same time too little to meaningfully compare values of magnitude `10e-12`. For examples, see [link Squassabia].
  171. We do not want to apply the same absolute tolerance for huge and tiny numbers. Instead, we would like to scale the `epsilon` with `u` and `v`.
  172. The __UTF__ implements floating-point comparison algorithm that is based on the solution presented in [link KnuthII Knuth]:
  173. [#equ2]
  174. ``
  175. abs(u - v) <= epsilon * abs(u)
  176. && abs(u - v) <= epsilon * abs(v)); // (2)
  177. ``
  178. defines a ['very close with tolerance `epsilon`] relationship between `u` and `v`, while
  179. [#equ3]
  180. ``
  181. abs(u - v) <= epsilon * abs(u)
  182. || abs(u - v) <= epsilon * abs(v); // (3)
  183. ``
  184. defines a ['close enough with tolerance `epsilon`] relationship between `u` and `v`.
  185. Both relationships are commutative but are not transitive. The relationship defined in
  186. [link equ2 (2)] is stronger that the relationship defined in [link equ3 (3)] since [link equ2 (2)] necessarily implies [link equ3 (3)].
  187. The multiplication in the right side of inequalities may cause an unwanted underflow condition. To prevent this,
  188. the implementation is using modified version of [link equ2 (2)] and [link equ3 (3)], which scales the checked difference rather than `epsilon`:
  189. [#equ4]
  190. ``
  191. abs(u - v)/abs(u) <= epsilon
  192. && abs(u - v)/abs(v) <= epsilon; // (4)
  193. ``
  194. [#equ5]
  195. ``
  196. abs(u - v)/abs(u) <= epsilon
  197. || abs(u - v)/abs(v) <= epsilon; // (5)
  198. ``
  199. This way all underflow and overflow conditions can be guarded safely. The above however, will not work when `v` or `u` is zero.
  200. In such cases the solution is to resort to a different algorithm, e.g. [link equ1 (1)].
  201. [h3 Tolerance selection considerations]
  202. In case of absence of domain specific requirements the value of tolerance can be chosen as a sum of the predicted
  203. upper limits for "relative rounding errors" of compared values. The "rounding" is the operation by which a real
  204. value 'x' is represented in a floating-point format with 'p' binary digits (bits) as the floating-point value [*X].
  205. The "relative rounding error" is the difference between the real and the floating point values in relation to real
  206. value: `abs(x-X)/abs(x)`. The discrepancy between real and floating point value may be caused by several reasons:
  207. * Type promotion
  208. * Arithmetic operations
  209. * Conversion from a decimal presentation to a binary presentation
  210. * Non-arithmetic operation
  211. The first two operations proved to have a relative rounding error that does not exceed
  212. half_epsilon = half of the 'machine epsilon value'
  213. for the appropriate floating point type `FPT` [footnote [*machine epsilon value] is represented by `std::numeric_limits<FPT>::epsilon()`].
  214. Conversion to binary presentation, sadly, does not have such requirement. So we can't assume that `float(1.1)` is close
  215. to the real number `1.1` with tolerance `half_epsilon` for float (though for 11./10 we can). Non-arithmetic operations either do not have a
  216. predicted upper limit relative rounding errors.
  217. [note Note that both arithmetic and non-arithmetic operations might also
  218. produce others "non-rounding" errors, such as underflow/overflow, division-by-zero or "operation errors".]
  219. All theorems about the upper limit of a rounding error, including that of `half_epsilon`, refer only to
  220. the 'rounding' operation, nothing more. This means that the 'operation error', that is, the error incurred by the
  221. operation itself, besides rounding, isn't considered. In order for numerical software to be able to actually
  222. predict error bounds, the __IEEE754__ standard requires arithmetic operations to be 'correctly or exactly rounded'.
  223. That is, it is required that the internal computation of a given operation be such that the floating point result
  224. is the exact result rounded to the number of working bits. In other words, it is required that the computation used
  225. by the operation itself doesn't introduce any additional errors. The __IEEE754__ standard does not require same behavior
  226. from most non-arithmetic operation. The underflow/overflow and division-by-zero errors may cause rounding errors
  227. with unpredictable upper limits.
  228. At last be aware that `half_epsilon` rules are not transitive. In other words combination of two
  229. arithmetic operations may produce rounding error that significantly exceeds `2*half_epsilon`. All
  230. in all there are no generic rules on how to select the tolerance and users need to apply common sense and domain/
  231. problem specific knowledge to decide on tolerance value.
  232. To simplify things in most usage cases latest version of algorithm below opted to use percentage values for
  233. tolerance specification (instead of fractions of related values). In other words now you use it to check that
  234. difference between two values does not exceed x percent.
  235. For more reading about floating-point comparison see references below.
  236. [h4 Bibliographic references]
  237. [variablelist Books
  238. [
  239. [[#KnuthII]The art of computer programming (vol II)]
  240. [Donald. E. Knuth, 1998, Addison-Wesley Longman, Inc., ISBN 0-201-89684-2, Addison-Wesley Professional; 3rd edition.
  241. (The relevant equations are in §4.2.2, Eq. 36 and 37.)]
  242. ]
  243. [
  244. [Rounding near zero, in [@http://www.amazon.com/Advanced-Arithmetic-Digital-Computer-Kulisch/dp/3211838708 Advanced Arithmetic for the Digital Computer]]
  245. [Ulrich W. Kulisch, 2002, Springer, Inc., ISBN 0-201-89684-2, Springer; 1st edition]
  246. ]
  247. ]
  248. [variablelist Periodicals
  249. [
  250. [[#Squassabia][@https://adtmag.com/articles/2000/03/16/comparing-floats-how-to-determine-if-floating-quantities-are-close-enough-once-a-tolerance-has-been.aspx
  251. Comparing Floats: How To Determine if Floating Quantities Are Close Enough Once a Tolerance Has Been Reached]]
  252. [Alberto Squassabia, in C++ Report (March 2000)]
  253. ]
  254. [
  255. [The Journeyman's Shop: Trap Handlers, Sticky Bits, and Floating-Point Comparisons]
  256. [Pete Becker, in C/C++ Users Journal (December 2000)]
  257. ]
  258. ]
  259. [variablelist Publications
  260. [
  261. [[@http://dl.acm.org/citation.cfm?id=103163
  262. What Every Computer Scientist Should Know About Floating-Point Arithmetic]]
  263. [David Goldberg, pages 150-230, in Computing Surveys (March 1991), Association for Computing Machinery, Inc.]
  264. ]
  265. [
  266. [[@http://hal.archives-ouvertes.fr/docs/00/07/26/81/PDF/RR-3967.pdf From Rounding Error Estimation to Automatic Correction with Automatic Differentiation]]
  267. [Philippe Langlois, Technical report, INRIA]
  268. ]
  269. [
  270. [[@http://www.cs.berkeley.edu/~wkahan/
  271. William Kahan home page]]
  272. [Lots of information on floating point arithmetics.]
  273. ]
  274. ]
  275. [endsect] [/ theory]
  276. [endsect] [/ floating points]