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 "internal/common.hpp"
5 #include "constants.hpp"
6 
7 #include <QJsonObject>
8 
9 #include <cmath>
10 
11 namespace cutehmi {
12 
18 QJsonObject CUTEHMI_API metadata(const QString & product);
19 
25 bool CUTEHMI_API metadataExists(const QString & product);
26 
38 inline
39 bool ape(qreal r1, qreal r2, qreal eps = EPS)
40 {
41  int exp1, exp2;
42  std::frexp(r1, & exp1);
43  std::frexp(r2, & exp2);
44 
45  // 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
46  // the consequence is that image of a function is a bit weird, e.g.:
47  // ape(1.0, 1.99, 0.25) -> false, ape(1.0, 2.0, 0.25) -> true, ape(1.0, 2.01, 0.25) -> false
48  return std::abs(r2 - r1) <= std::ldexp(eps, std::max(exp1, exp2));
49 }
50 
64 inline
65 bool ale(qreal r1, qreal r2, qreal eps = EPS)
66 {
67  int exp1, exp2;
68  std::frexp(r1, & exp1);
69  std::frexp(r2, & exp2);
70 
71  return std::abs(r2 - r1) <= std::ldexp(eps, std::min(exp1, exp2));
72 }
73 
83 inline
84 bool clt(qreal r1, qreal r2, qreal eps = EPS)
85 {
86  int exp1, exp2;
87  std::frexp(r1, & exp1);
88  std::frexp(r2, & exp2);
89 
90  return r2 - r1 > std::ldexp(eps, std::max(exp1, exp2));
91 }
92 
102 inline
103 bool cgt(qreal r1, qreal r2, qreal eps = EPS)
104 {
105  int exp1, exp2;
106  std::frexp(r1, & exp1);
107  std::frexp(r2, & exp2);
108 
109  return r1 - r2 > std::ldexp(eps, std::max(exp1, exp2));
110 }
111 
112 }
113 
114 #endif
115 
116 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>. All rights reserved.
117 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
118 //(c)C: This file is a part of CuteHMI.
119 //(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.
120 //(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.
121 //(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/>.
122 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
123 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
124 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
125 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cutehmi::ale
bool ale(qreal r1, qreal r2, qreal eps=EPS)
Almost equal.
Definition: functions.hpp:65
std::frexp
T frexp(T... args)
cutehmi
Definition: constants.hpp:6
cutehmi::metadataExists
bool CUTEHMI_API metadataExists(const QString &product)
Check if product metadata exists.
Definition: functions.cpp:29
QString
QJsonObject
std::min
T min(T... args)
cutehmi::cgt
bool cgt(qreal r1, qreal r2, qreal eps=EPS)
Considerably greater than.
Definition: functions.hpp:103
cutehmi::ape
bool ape(qreal r1, qreal r2, qreal eps=EPS)
Approximately equal.
Definition: functions.hpp:39
cutehmi::clt
bool clt(qreal r1, qreal r2, qreal eps=EPS)
Considerably less than.
Definition: functions.hpp:84
std::ldexp
T ldexp(T... args)
std::max
T max(T... args)
cutehmi::metadata
QJsonObject CUTEHMI_API metadata(const QString &product)
Load product metadata.
Definition: functions.cpp:18
cutehmi::EPS
constexpr qreal EPS
CuteHMI epsilon.
Definition: constants.hpp:13