QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
comparison.hpp
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
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
41 bool close(Real x, Real y);
42 bool close(Real x, Real y, Size n);
43
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
126
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> > {
139 QL_DEPRECATED
140 typedef ext::shared_ptr<T> first_argument_type;
141
145 QL_DEPRECATED
146 typedef ext::shared_ptr<T> second_argument_type;
147
151 QL_DEPRECATED
152 typedef bool result_type;
153
154 bool operator()(const ext::shared_ptr<T>& x,
155 const ext::shared_ptr<T>& y) const {
156 return earlier_than<T>()(*x,*y);
157 }
158 };
159
160}
161
162
163#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
QL_DEPRECATED typedef ext::shared_ptr< T > first_argument_type
Definition: comparison.hpp:140
bool operator()(const ext::shared_ptr< T > &x, const ext::shared_ptr< T > &y) const
Definition: comparison.hpp:154
QL_DEPRECATED typedef ext::shared_ptr< T > second_argument_type
Definition: comparison.hpp:146
compare two objects by date
Definition: comparison.hpp:130