kDB: Knowledge DataBase
Loading...
Searching...
No Matches
BigNumber.h
1#ifndef _KNOW_CORE_BIGNUMBER_H_
2#define _KNOW_CORE_BIGNUMBER_H_
3
4#include <clog_qt>
5#include <cres_qt>
6
7#include <QList>
8
9#include "Forward.h"
10
11#include <Cyqlops/Crypto/Hash.h>
12
13class QVariant;
14
15namespace std
16{
17 template<>
18 struct is_floating_point<knowCore::BigNumber> : public true_type
19 {
20 };
21 template<>
22 struct is_signed<knowCore::BigNumber> : public true_type
23 {
24 };
25 template<>
26 struct is_unsigned<knowCore::BigNumber> : public false_type
27 {
28 };
29 template<>
30 struct is_integral<knowCore::BigNumber> : public false_type
31 {
32 };
33} // namespace std
34
35namespace knowCore
36{
51 {
52 public:
53 enum class Sign
54 {
55 Positive,
56 Negative,
57 NaN,
58 PositiveInfinite,
59 NegativeInfinite
60 };
61 public:
62 BigNumber();
63 BigNumber(const BigNumber& _rhs);
64 BigNumber(BigNumber&& _rhs);
65 BigNumber& operator=(const BigNumber& _rhs);
66 BigNumber(qint16 weight, Sign sign, qint16 dscale, const QList<quint16>& digits);
67 ~BigNumber();
68
69 BigNumber(std::intmax_t _value);
70 BigNumber(std::uintmax_t _value);
71 template<typename _T_>
72 requires(std::is_integral_v<_T_> and std::is_unsigned_v<_T_>)
73 BigNumber(_T_ _value) : BigNumber(std::uintmax_t(_value))
74 {
75 }
76 template<typename _T_>
77 requires(std::is_integral_v<_T_> and std::is_signed_v<_T_>)
78 BigNumber(_T_ _value) : BigNumber(std::intmax_t(_value))
79 {
80 }
81 BigNumber(double _value);
82 static cres_qresult<BigNumber> fromString(const QString& _value);
83 static cres_qresult<BigNumber> fromString(const char* _value)
84 {
85 return fromString(QString(_value));
86 }
87 static cres_qresult<BigNumber> fromVariant(const QVariant& _value);
88 static cres_qresult<BigNumber> fromValue(const knowCore::Value& _value);
89 static BigNumber zero();
90 static BigNumber nan();
91 static BigNumber positiveInfinite();
92 static BigNumber negativeInfinite();
93 bool isNaN() const;
94 bool isFinite() const;
95 bool isInfinite() const;
99 bool isFloating() const;
100 QString toString() const;
106 QVariant toVariant() const;
112 knowCore::Value toValue() const;
113 double toDouble() const;
120 cres_qresult<qint64> toInt64(bool _truncate = false) const;
121 cres_qresult<quint64> toUInt64(bool _truncate = false) const;
122 bool operator==(const BigNumber& _rhs) const;
123 bool operator!=(const BigNumber& _rhs) const { return not(*this == _rhs); }
124 QByteArray md5() const;
128 void hash(QCryptographicHash* _hash) const;
129
130 BigNumber operator+(const BigNumber& _rhs) const;
131 BigNumber operator-(const BigNumber& _rhs) const;
132 BigNumber operator*(const BigNumber& _rhs) const;
133 BigNumber operator/(const BigNumber& _rhs) const;
134 BigNumber operator-() const;
135 bool operator<(const BigNumber& _rhs) const;
136 bool operator<=(const BigNumber& _rhs) const;
137 bool operator>(const BigNumber& _rhs) const;
138 bool operator>=(const BigNumber& _rhs) const;
139
140 template<typename _T_>
141 requires(std::is_arithmetic_v<_T_>)
142 BigNumber operator+(const _T_& _rhs) const
143 {
144 return operator+(BigNumber(_rhs));
145 }
146 template<typename _T_>
147 requires(std::is_arithmetic_v<_T_>)
148 BigNumber operator-(const _T_& _rhs) const
149 {
150 return operator-(BigNumber(_rhs));
151 }
152 template<typename _T_>
153 requires(std::is_arithmetic_v<_T_>)
154 BigNumber operator*(const _T_& _rhs) const
155 {
156 return operator*(BigNumber(_rhs));
157 }
158 template<typename _T_>
159 requires(std::is_arithmetic_v<_T_>)
160 BigNumber operator/(const _T_& _rhs) const
161 {
162 return operator/(BigNumber(_rhs));
163 }
164 template<typename _T_>
165 requires(std::is_arithmetic_v<_T_>)
166 bool operator<(const _T_& _rhs) const
167 {
168 return operator<(BigNumber(_rhs));
169 }
170 template<typename _T_>
171 requires(std::is_arithmetic_v<_T_>)
172 bool operator<=(const _T_& _rhs) const
173 {
174 return operator<=(BigNumber(_rhs));
175 }
176 template<typename _T_>
177 requires(std::is_arithmetic_v<_T_>)
178 bool operator>(const _T_& _rhs) const
179 {
180 return operator>(BigNumber(_rhs));
181 }
182 template<typename _T_>
183 requires(std::is_arithmetic_v<_T_>)
184 bool operator>=(const _T_& _rhs) const
185 {
186 return operator>=(BigNumber(_rhs));
187 }
188 public:
189 quint16 weight() const { return m_weight; }
190 Sign sign() const { return m_sign; }
191 quint16 dscale() const { return m_dscale; }
192 QList<quint16> digits() const { return m_digits; }
193 private:
194 void simplify();
195 void roundVar();
196 void exponenfy(int exp);
197 BigNumber add_abs(const BigNumber& _rhs) const;
198 BigNumber sub_abs(const BigNumber& _rhs) const;
199 enum class ComparisonResult
200 {
201 Bigger /* this < _rhs */,
202 Smaller /* this > _rhs */,
203 Equal
204 };
205 ComparisonResult compare_abs(const BigNumber& _rhs) const;
206 ComparisonResult compare(const BigNumber& _rhs) const;
210 std::tuple<bool, quint64> toUInt64_ignore_sign() const;
211 template<typename _T_>
212 _T_ toVariantValue() const;
213 private:
214 qint16 m_weight, m_dscale;
215 Sign m_sign;
216 QList<quint16> m_digits;
217 };
218 template<typename _T_>
219 requires(std::is_arithmetic_v<_T_>)
220 static inline BigNumber operator+(const _T_& _lhs, const BigNumber& _rhs)
221 {
222 return BigNumber(_lhs) + _rhs;
223 }
224 template<typename _T_>
225 requires(std::is_arithmetic_v<_T_>)
226 static inline BigNumber operator-(const _T_& _lhs, const BigNumber& _rhs)
227 {
228 return BigNumber(_lhs) - _rhs;
229 }
230 template<typename _T_>
231 requires(std::is_arithmetic_v<_T_>)
232 static inline BigNumber operator*(const _T_& _lhs, const BigNumber& _rhs)
233 {
234 return BigNumber(_lhs) * _rhs;
235 }
236 template<typename _T_>
237 requires(std::is_arithmetic_v<_T_>)
238 static inline BigNumber operator/(const _T_& _lhs, const BigNumber& _rhs)
239 {
240 return BigNumber(_lhs) / _rhs;
241 }
242 template<typename _T_>
243 requires(std::is_arithmetic_v<_T_>)
244 static inline bool operator<(const _T_& _lhs, const BigNumber& _rhs)
245 {
246 return BigNumber(_lhs) < _rhs;
247 }
248 template<typename _T_>
249 requires(std::is_arithmetic_v<_T_>)
250 static inline bool operator<=(const _T_& _lhs, const BigNumber& _rhs)
251 {
252 return BigNumber(_lhs) <= _rhs;
253 }
254 template<typename _T_>
255 requires(std::is_arithmetic_v<_T_>)
256 static inline bool operator>(const _T_& _lhs, const BigNumber& _rhs)
257 {
258 return BigNumber(_lhs) > _rhs;
259 }
260 template<typename _T_>
261 requires(std::is_arithmetic_v<_T_>)
262 static inline bool operator>=(const _T_& _lhs, const BigNumber& _rhs)
263 {
264 return BigNumber(_lhs) >= _rhs;
265 }
266 template<typename _T_>
267 requires(std::is_arithmetic_v<_T_>)
268 static inline bool operator==(const _T_& _lhs, const BigNumber& _rhs)
269 {
270 return BigNumber(_lhs) == _rhs;
271 }
272} // namespace knowCore
273
274#include "MetaType.h"
275KNOWCORE_DECLARE_FULL_METATYPE(knowCore, BigNumber);
276
277#include "Formatter.h"
278
279clog_format_declare_formatter(knowCore::BigNumber)
280{
281 return std::format_to(ctx.out(), "{}", p.toString());
282}
283
284namespace Cyqlops::Crypto::Hash
285{
286 template<>
287 struct Compute<knowCore::BigNumber>
288 {
289 Compute(QCryptographicHash& _algorithm, const knowCore::BigNumber& _v) { _v.hash(&_algorithm); }
290 };
291} // namespace Cyqlops::Crypto::Hash
292
293#endif
Definition Revision.h:9
Class that can contains large numeric value.
Definition BigNumber.h:51
bool isFloating() const
Definition BigNumber.cpp:248
cres_qresult< qint64 > toInt64(bool _truncate=false) const
Definition BigNumber.cpp:288
void hash(QCryptographicHash *_hash) const
Definition BigNumber.cpp:560
knowCore::Value toValue() const
return an optimal value representation This function try to return a variant with an optimal represen...
Definition BigNumber.cpp:540
QVariant toVariant() const
return an optimal variant representation This function try to return a variant with an optimal repres...
Definition BigNumber.cpp:538
Definition Value.h:21