QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
comparison.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003, 2004, 2005, 2007 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file comparison.hpp
21 \brief floating-point comparisons
22*/
23
24#ifndef quantlib_comparison_hpp
25#define quantlib_comparison_hpp
26
27#include <ql/types.hpp>
28#include <ql/shared_ptr.hpp>
29
30namespace QuantLib {
31
32 /*! Follows somewhat the advice of Knuth on checking for floating-point
33 equality. The closeness relationship is:
34 \f[
35 \mathrm{close}(x,y,n) \equiv |x-y| \leq \varepsilon |x|
36 \wedge |x-y| \leq \varepsilon |y|
37 \f]
38 where \f$ \varepsilon \f$ is \f$ n \f$ times the machine accuracy;
39 \f$ n \f$ equals 42 if not given.
40 */
41 bool close(Real x, Real y);
42 bool close(Real x, Real y, Size n);
43
44 /*! Follows somewhat the advice of Knuth on checking for floating-point
45 equality. The closeness relationship is:
46 \f[
47 \mathrm{close}(x,y,n) \equiv |x-y| \leq \varepsilon |x|
48 \vee |x-y| \leq \varepsilon |y|
49 \f]
50 where \f$ \varepsilon \f$ is \f$ n \f$ times the machine accuracy;
51 \f$ n \f$ equals 42 if not given.
52 */
53 bool close_enough(Real x, Real y);
54 bool close_enough(Real x, Real y, Size n);
55
56
57 // inline definitions
58
59 inline bool close(Real x, Real y) {
60 // we're duplicating the code here instead of calling close(x,y,42)
61 // for optimization; this allows us to make tolerance constexpr
62 // and shave a few more cycles.
63
64 // Deals with +infinity and -infinity representations etc.
65 if (x == y)
66 return true;
67
68 Real diff = std::fabs(x-y);
69 constexpr double tolerance = 42 * QL_EPSILON;
70
71 if (x == 0.0 || y == 0.0)
72 return diff < (tolerance * tolerance);
73
74 return diff <= tolerance*std::fabs(x) &&
75 diff <= tolerance*std::fabs(y);
76 }
77
78 inline bool close(Real x, Real y, Size n) {
79 // Deals with +infinity and -infinity representations etc.
80 if (x == y)
81 return true;
82
83 Real diff = std::fabs(x-y), tolerance = n * QL_EPSILON;
84
85 if (x == 0.0 || y == 0.0)
86 return diff < (tolerance * tolerance);
87
88 return diff <= tolerance*std::fabs(x) &&
89 diff <= tolerance*std::fabs(y);
90 }
91
92 inline bool close_enough(Real x, Real y) {
93 // see close() for a note on duplication
94
95 // Deals with +infinity and -infinity representations etc.
96 if (x == y)
97 return true;
98
99 Real diff = std::fabs(x-y);
100 constexpr double tolerance = 42 * QL_EPSILON;
101
102 if (x == 0.0 || y == 0.0) // x or y = 0.0
103 return diff < (tolerance * tolerance);
104
105 return diff <= tolerance*std::fabs(x) ||
106 diff <= tolerance*std::fabs(y);
107 }
108
109 inline bool close_enough(Real x, Real y, Size n) {
110 // Deals with +infinity and -infinity representations etc.
111 if (x == y)
112 return true;
113
114 Real diff = std::fabs(x-y), tolerance = n * QL_EPSILON;
115
116 if (x == 0.0 || y == 0.0)
117 return diff < (tolerance * tolerance);
118
119 return diff <= tolerance*std::fabs(x) ||
120 diff <= tolerance*std::fabs(y);
121 }
122
123
124
125 //! compare two objects by date
126 /*! There is no generic implementation of this struct.
127 Template specializations will have to be defined for
128 each needed type (see CashFlow for an example.)
129 */
130 template <class T> struct earlier_than;
131
132 /* partial specialization for shared pointers, forwarding to their
133 pointees. */
134 template <class T>
135 struct earlier_than<ext::shared_ptr<T> > {
136 bool operator()(const ext::shared_ptr<T>& x,
137 const ext::shared_ptr<T>& y) const {
138 return earlier_than<T>()(*x,*y);
139 }
140 };
141
142}
143
144
145#endif
#define QL_EPSILON
Definition: qldefines.hpp:178
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
Maps shared_ptr to either the boost or std implementation.
bool operator()(const ext::shared_ptr< T > &x, const ext::shared_ptr< T > &y) const
Definition: comparison.hpp:136
compare two objects by date
Definition: comparison.hpp:130
Custom types.