CuteHMI - CuteHMI (CuteHMI.2)
functions.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_FUNCTIONS_HPP
2 #define H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_FUNCTIONS_HPP
3 
4 #include "constants.hpp"
5 
6 #include <cmath>
7 
8 namespace cutehmi {
9 
21 inline
22 bool ape(qreal r1, qreal r2, qreal eps = EPS)
23 {
24  int exp1, exp2;
25  std::frexp(r1, & exp1);
26  std::frexp(r2, & exp2);
27 
28  // For some reason <= is used in the book to compare both sides. There may be a reason for this, so I'll leave it for now, but
29  // the consequence is that image of a function is a bit weird, e.g.:
30  // ape(1.0, 1.99, 0.25) -> false, ape(1.0, 2.0, 0.25) -> true, ape(1.0, 2.01, 0.25) -> false
31  return std::abs(r2 - r1) <= std::ldexp(eps, std::max(exp1, exp2));
32 }
33 
47 inline
48 bool ale(qreal r1, qreal r2, qreal eps = EPS)
49 {
50  int exp1, exp2;
51  std::frexp(r1, & exp1);
52  std::frexp(r2, & exp2);
53 
54  return std::abs(r2 - r1) <= std::ldexp(eps, std::min(exp1, exp2));
55 }
56 
66 inline
67 bool clt(qreal r1, qreal r2, qreal eps = EPS)
68 {
69  int exp1, exp2;
70  std::frexp(r1, & exp1);
71  std::frexp(r2, & exp2);
72 
73  return r2 - r1 > std::ldexp(eps, std::max(exp1, exp2));
74 }
75 
85 inline
86 bool cgt(qreal r1, qreal r2, qreal eps = EPS)
87 {
88  int exp1, exp2;
89  std::frexp(r1, & exp1);
90  std::frexp(r2, & exp2);
91 
92  return r1 - r2 > std::ldexp(eps, std::max(exp1, exp2));
93 }
94 
95 }
96 
97 #endif
98 
99 //(c)C: Copyright © 2019, Michał Policht <michal@policht.pl>. All rights reserved.
100 //(c)C: This file is a part of CuteHMI.
101 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
102 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
103 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
cutehmi::ale
bool ale(qreal r1, qreal r2, qreal eps=EPS)
Almost equal.
Definition: functions.hpp:48
std::frexp
T frexp(T... args)
cutehmi
Definition: constants.hpp:6
std::min
T min(T... args)
cutehmi::cgt
bool cgt(qreal r1, qreal r2, qreal eps=EPS)
Considerably greater than.
Definition: functions.hpp:86
cutehmi::ape
bool ape(qreal r1, qreal r2, qreal eps=EPS)
Approximately equal.
Definition: functions.hpp:22
cutehmi::clt
bool clt(qreal r1, qreal r2, qreal eps=EPS)
Considerably less than.
Definition: functions.hpp:67
std::ldexp
T ldexp(T... args)
std::max
T max(T... args)
cutehmi::EPS
constexpr qreal EPS
CuteHMI epsilon.
Definition: constants.hpp:13