-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimal.hpp
More file actions
530 lines (479 loc) · 26.4 KB
/
decimal.hpp
File metadata and controls
530 lines (479 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* Author: Kamil Galant
* e-mail: kamil.galant@gmail.com
* github: https://github.com/kamxgal
*
* Copyright (C) 2023 Kamil Galant. All Rights Reserved.
*
* License
* StrictDecimal is released under BSD-3-Clause license.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <numeric>
#include <string>
#include <sstream>
#include <iostream>
namespace strict
{
template<typename RetT>
constexpr RetT Power10(int n) noexcept {
const RetT lookup[] = {
static_cast<RetT>(1), // 10^0
static_cast<RetT>(10), // 10^1
static_cast<RetT>(100), // 10^2
static_cast<RetT>(1000), // 10^3
static_cast<RetT>(10000), // 10^4
static_cast<RetT>(100000), // 10^5
static_cast<RetT>(1000000), // 10^6
static_cast<RetT>(10000000), // 10^7
static_cast<RetT>(100000000), // 10^8
static_cast<RetT>(1000000000), // 10^9
static_cast<RetT>(10000000000), // 10^10
};
RetT res = lookup[n % 10];
const int loops = n / 10;
for (int i=0; i<loops; ++i) {
res *= lookup[10];
}
return res;
}
template<typename LhsT, typename RhsT>
struct select_operating_type
{
#if defined(__GNUC__) || defined(__clang__)
// If GCC or Clang, use 128-bit for intermediate multiplication
// (Be mindful: MSVC doesn't support __int128).
using type = __int128;
#else
// Otherwise, just pick the larger of the two underlying types
using type = std::conditional_t<
(sizeof(LhsT) >= sizeof(RhsT)),
LhsT,
RhsT
>;
#endif
};
// Convenience alias to avoid writing `typename select_operating_type<...>::type`
template<typename LhsT, typename RhsT>
using select_operating_type_t = typename select_operating_type<LhsT, RhsT>::type;
template<typename UnderlyingType = int64_t, int Precision = 2>
struct decimal_t
{
using underlying_type = UnderlyingType;
static constexpr int PRECISION = std::max(Precision, std::numeric_limits<UnderlyingType>::max_digits10-1);
static constexpr UnderlyingType DENOMINATOR = Power10<UnderlyingType>(PRECISION);
static constexpr UnderlyingType HALF_DENOMINATOR = DENOMINATOR / 2;
static constexpr UnderlyingType NAN_VALUE = std::numeric_limits<UnderlyingType>::max();
static constexpr UnderlyingType INFINITY_PLUS = std::numeric_limits<UnderlyingType>::max() - 1;
static constexpr UnderlyingType INFINITY_MINUS = std::numeric_limits<UnderlyingType>::min() + 1;
struct nominator_t { UnderlyingType value{}; };
explicit decimal_t() : mNominator{} {
}
explicit decimal_t(std::string num) {
UnderlyingType integerPart{}, fractionPart{};
char dotPlaceholder;
std::istringstream ss(num);
ss >> integerPart >> dotPlaceholder >> fractionPart;
const ::size_t dotPos = num.find('.');
if (dotPos == std::string::npos) {
fractionPart = 0;
} else {
int fractionPartLen = static_cast<int>(num.size() - dotPos - 1);
fractionPart *= fractionPartLen >= PRECISION ? UnderlyingType{1} : Power10<UnderlyingType>(PRECISION - fractionPartLen);
}
mNominator = decimal_t{integerPart, fractionPart}.mNominator;
}
template<typename FloatingT,
std::enable_if_t<std::is_floating_point<FloatingT>::value, bool> = true>
explicit decimal_t(FloatingT num) {
const UnderlyingType temp = static_cast<UnderlyingType>(std::round(num * DENOMINATOR * 10));
const int lastDigit = std::abs(temp) % 10;
underlying_type signFactor = temp >=0 ? 1 : -1;
mNominator.value = static_cast<UnderlyingType>(temp / 10);
mNominator.value += lastDigit >= 5 ? signFactor : 0;
}
template<typename IntegralT,
std::enable_if_t<std::is_integral<IntegralT>::value, bool> = true>
explicit decimal_t(IntegralT num) {
mNominator.value = static_cast<UnderlyingType>(num * DENOMINATOR);
}
explicit decimal_t(typename decimal_t<UnderlyingType, PRECISION>::nominator_t nominator) : mNominator(nominator) {
}
explicit decimal_t(UnderlyingType integerPart, UnderlyingType fractionPart) {
int fractionLength = static_cast<int>(std::log10(fractionPart)) + 1;
if (fractionLength > PRECISION) {
int lastSignificantDigit = fractionPart / Power10<UnderlyingType>(fractionLength - PRECISION -1) % 10;
fractionPart = fractionPart / Power10<UnderlyingType>(fractionLength - PRECISION) + (lastSignificantDigit >= 5 ? 1 : 0);
}
mNominator.value = integerPart * DENOMINATOR + (integerPart >= 0 ? fractionPart : -fractionPart);
}
UnderlyingType nominator() const { return mNominator.value; }
UnderlyingType integer_part() const { return mNominator.value / DENOMINATOR; }
UnderlyingType fraction_part() const { return std::abs(mNominator.value) % DENOMINATOR; }
constexpr bool operator<(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value < other.mNominator.value; }
constexpr bool operator<=(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value <= other.mNominator.value; }
constexpr bool operator==(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value == other.mNominator.value; }
constexpr bool operator!=(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value != other.mNominator.value; }
constexpr bool operator>=(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value >= other.mNominator.value; }
constexpr bool operator>(const decimal_t<underlying_type, PRECISION>& other) const { return mNominator.value > other.mNominator.value; }
decimal_t<underlying_type, PRECISION> operator+(const decimal_t<underlying_type, PRECISION>& rhs) const {
return decimal_t<UnderlyingType, PRECISION>{nominator_t{mNominator.value + rhs.mNominator.value}};
}
decimal_t<underlying_type, PRECISION> operator-(const decimal_t<underlying_type, PRECISION>& rhs) const {
return decimal_t<UnderlyingType, PRECISION>{nominator_t{mNominator.value - rhs.mNominator.value}};
}
decimal_t<underlying_type, PRECISION> operator-() const {
return decimal_t<UnderlyingType, PRECISION>{nominator_t{-mNominator.value}};
}
decimal_t<underlying_type, PRECISION> operator*(const decimal_t<underlying_type, PRECISION>& rhs) const {
using this_type = decimal_t<underlying_type, PRECISION>;
underlying_type res = mNominator.value * rhs.mNominator.value;
if constexpr (PRECISION == 0) {
return this_type{nominator_t{res}};
} else {
underlying_type fractionPart = std::abs(res) % DENOMINATOR;
underlying_type signFactor = res >= 0 ? 1 : -1;
return this_type{nominator_t{res / DENOMINATOR + (fractionPart >= HALF_DENOMINATOR ? signFactor : 0)}};
}
}
template<typename RhsUnderlyingType, int RhsPrecision>
decimal_t<underlying_type, PRECISION> operator*(const decimal_t<RhsUnderlyingType, RhsPrecision>& rhs) const {
decimal_t<underlying_type, PRECISION> res = *this;
res *= rhs;
return res;
}
template<typename RhsUnderlyingType, int RhsPrecision>
decimal_t<underlying_type, PRECISION> operator/(const decimal_t<RhsUnderlyingType, RhsPrecision>& rhs) const {
decimal_t<underlying_type, PRECISION> res = *this;
res /= rhs;
return res;
}
decimal_t<underlying_type, PRECISION>& operator+=(const decimal_t<underlying_type, PRECISION>& rhs) {
mNominator.value += rhs.mNominator.value;
return *this;
}
decimal_t<underlying_type, PRECISION>& operator-=(const decimal_t<underlying_type, PRECISION>& rhs) {
mNominator.value -= rhs.mNominator.value;
return *this;
}
decimal_t<underlying_type, PRECISION>& operator*=(const decimal_t<underlying_type, PRECISION>& rhs) {
underlying_type res = mNominator.value * rhs.mNominator.value;
if constexpr (PRECISION == 0) {
mNominator.value = mNominator.value * rhs.mNominator.value;
} else {
underlying_type fractionPart = std::abs(res) % DENOMINATOR;
underlying_type signFactor = res >= 0 ? 1 : -1;
mNominator.value = mNominator.value * rhs.mNominator.value / DENOMINATOR + (fractionPart >= HALF_DENOMINATOR ? signFactor : 0);
}
return *this;
}
template<typename RhsUnderlyingType, int RhsDecimalPrecision>
decimal_t<underlying_type, PRECISION>& operator*=(const decimal_t<RhsUnderlyingType, RhsDecimalPrecision>& rhs) {
using OperatingType = select_operating_type_t<underlying_type, RhsUnderlyingType>;
const OperatingType integerPart = static_cast<OperatingType>(this->integer_part());
OperatingType fractionPart = static_cast<OperatingType>(this->fraction_part());
const OperatingType rhsIntegerPart = static_cast<OperatingType>(rhs.integer_part());
OperatingType rhsFractionPart = static_cast<OperatingType>(rhs.fraction_part());
constexpr int CommonPrecision = PRECISION < RhsDecimalPrecision ? RhsDecimalPrecision : PRECISION;
// setting values of fractionPart and rhsFractionPart as having common denominator
fractionPart *= Power10<OperatingType>(CommonPrecision) / DENOMINATOR;
rhsFractionPart *= Power10<OperatingType>(CommonPrecision) / static_cast<underlying_type>(std::decay_t<decltype(rhs)>::DENOMINATOR);
OperatingType resultInteger = std::abs(integerPart * rhsIntegerPart);
underlying_type signFactor = integerPart * rhsIntegerPart >= 0 ? 1 : -1;
// commonFraction has denominator equal to commonDenominator
OperatingType commonFraction = std::abs(integerPart) * rhsFractionPart + fractionPart * std::abs(rhsIntegerPart);
resultInteger += commonFraction / Power10<OperatingType>(CommonPrecision);
commonFraction %= Power10<OperatingType>(CommonPrecision);
// from now on denominator of commonFraction equals commonDenominator*commonDenominator
commonFraction *= Power10<OperatingType>(CommonPrecision);
commonFraction += fractionPart * rhsFractionPart;
// from now on commonFraction is treated as if its denominator is equal Power10<underlying_type>(PRECISION + 1);
commonFraction /= Power10<OperatingType>(CommonPrecision + CommonPrecision - PRECISION - 1);
const int lastSignificantDigit = commonFraction % 10;
commonFraction /= 10;
commonFraction += lastSignificantDigit >= 5 ? 1 : 0;
mNominator.value = static_cast<underlying_type>(resultInteger * DENOMINATOR + commonFraction);
mNominator.value *= signFactor;
return *this;
}
template <typename RhsUnderlyingType, int RhsPrecision, std::enable_if_t<PRECISION != RhsPrecision, bool> = true>
decimal_t<underlying_type, PRECISION>& operator/=(const decimal_t<RhsUnderlyingType, RhsPrecision>& rhs)
{
if (mNominator.value == 0 && rhs.nominator() == 0) {
mNominator.value = NAN_VALUE;
return *this;
}
if (rhs.nominator() == 0) {
mNominator.value = mNominator.value >= 0 ? INFINITY_PLUS : INFINITY_MINUS;
return *this;
}
using OperationType = select_operating_type_t<underlying_type, RhsUnderlyingType>;
OperationType res = static_cast<OperationType>(mNominator.value);
res *= Power10<OperationType>(RhsPrecision+1);
res /= static_cast<OperationType>(rhs.nominator());
OperationType signFactor = res >= 0 ? 1 : -1;
int lastSignificantDigit = std::abs(res) % 10;
res /= 10;
res += lastSignificantDigit >= 5 ? signFactor : 0;
mNominator.value = static_cast<UnderlyingType>(res);
return *this;
}
template <typename RhsUnderlyingType>
decimal_t<underlying_type, PRECISION>& operator/=(const decimal_t<RhsUnderlyingType, PRECISION>& rhs)
{
if (mNominator.value == 0 && rhs.nominator() == 0) {
mNominator.value = NAN_VALUE;
return *this;
}
if (rhs.nominator() == 0) {
mNominator.value = mNominator.value >= 0 ? INFINITY_PLUS : INFINITY_MINUS;
return *this;
}
using OperationType = select_operating_type_t<underlying_type, RhsUnderlyingType>;
OperationType integerPart = std::abs(integer_part()) * Power10<OperationType>(PRECISION + 1);
OperationType fractionPart = fraction_part() * Power10<OperationType>(1);
OperationType signFactor = std::clamp<OperationType>(nominator(), -1, 1) * std::clamp<OperationType>(rhs.nominator(), -1, 1);
integerPart = static_cast<OperationType>(std::llround(1.0 * integerPart / std::fabs(rhs.to_double())));
fractionPart = static_cast<OperationType>(std::llround(1.0 * fractionPart / std::fabs(rhs.to_double())));
OperationType res = integerPart + fractionPart;
int lastSignificantDigit = std::abs(res) % 10;
res /= 10;
res *= signFactor;
res += lastSignificantDigit >= 5 ? signFactor : 0;
mNominator.value = static_cast<UnderlyingType>(res);
return *this;
}
friend std::ostream& operator<<(std::ostream& out, const decimal_t<underlying_type, PRECISION>& decimal) {
out << decimal.to_string();
return out;
}
float to_float() const {
switch (mNominator.value)
{
case NAN_VALUE: return std::nanf("");
case INFINITY_PLUS: return std::numeric_limits<float>::infinity();
case INFINITY_MINUS: return -std::numeric_limits<float>::infinity();
default: return static_cast<float>(mNominator.value) / DENOMINATOR;
}
}
double to_double() const {
switch (mNominator.value)
{
case NAN_VALUE: return std::nan("");
case INFINITY_PLUS: return std::numeric_limits<double>::infinity();
case INFINITY_MINUS: return -std::numeric_limits<double>::infinity();
default: return static_cast<double>(mNominator.value) / DENOMINATOR;
}
}
std::string to_string() const {
if constexpr (PRECISION == 0) {
return std::to_string(mNominator.value);
} else {
switch (mNominator.value)
{
case NAN_VALUE: return std::string("nan");
case INFINITY_PLUS: return std::string("inf");
case INFINITY_MINUS: return std::string("-inf");
default:
std::stringstream ss;
ss << integer_part() << '.' << std::setfill('0') << std::setw(PRECISION) << fraction_part();
return ss.str();
}
}
}
protected:
nominator_t mNominator{};
};
template<typename UnderlyingType, int Precision, UnderlyingType MinValue, UnderlyingType MaxValue>
struct ranged_decimal_t : public decimal_t<UnderlyingType, Precision>
{
using base_type = decimal_t<UnderlyingType, Precision>;
using this_type = ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>;
static constexpr int PRECISION = decimal_t<UnderlyingType, Precision>::PRECISION;
static constexpr UnderlyingType MIN_VALUE = std::clamp<UnderlyingType>(MinValue,
std::numeric_limits<UnderlyingType>::min() / base_type::DENOMINATOR,
std::numeric_limits<UnderlyingType>::max() / base_type::DENOMINATOR);
static constexpr UnderlyingType MAX_VALUE = std::clamp<UnderlyingType>(MaxValue,
std::numeric_limits<UnderlyingType>::min() / base_type::DENOMINATOR,
std::numeric_limits<UnderlyingType>::max() / base_type::DENOMINATOR);
static constexpr UnderlyingType NOMINATOR_MIN_VALUE = MIN_VALUE * base_type::DENOMINATOR;
static constexpr UnderlyingType NOMINATOR_MAX_VALUE = MAX_VALUE * base_type::DENOMINATOR;
explicit ranged_decimal_t() : base_type() {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
explicit ranged_decimal_t(std::string num) : base_type(num) {
if (this->mNominator.value != base_type::NAN_VALUE) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
}
template<typename FloatingT,
std::enable_if_t<std::is_floating_point<FloatingT>::value, bool> = true>
explicit ranged_decimal_t(FloatingT num) : base_type(num) {
if (this->mNominator.value != base_type::NAN_VALUE) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
}
template<typename IntegralT,
std::enable_if_t<std::is_integral<IntegralT>::value, bool> = true>
explicit ranged_decimal_t(IntegralT num) : base_type(num) {
if (this->mNominator.value != base_type::NAN_VALUE) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
}
explicit ranged_decimal_t(typename decimal_t<UnderlyingType, Precision>::nominator_t nominator)
: base_type(nominator) {
if (this->mNominator.value != base_type::NAN_VALUE) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
}
explicit ranged_decimal_t(UnderlyingType integerPart, UnderlyingType fractionPart)
: base_type(integerPart, fractionPart) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
constexpr bool operator<(const base_type& other) const { return this->mNominator.value < other.nominator(); }
constexpr bool operator<=(const base_type& other) const { return this->mNominator.value <= other.nominator(); }
constexpr bool operator==(const base_type& other) const { return this->mNominator.value == other.nominator(); }
constexpr bool operator==(const this_type& other) const { return this->mNominator.value == other.nominator(); }
constexpr bool operator>=(const base_type& other) const { return this->mNominator.value >= other.nominator(); }
constexpr bool operator>(const base_type& other) const { return this->mNominator.value > other.nominator(); }
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator+(const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& rhs) const {
const auto res = static_cast<const base_type&>(*this) + static_cast<const base_type&>(rhs);
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(typename base_type::nominator_t{res.nominator()});
}
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator-(const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& rhs) const {
const auto res = static_cast<const base_type&>(*this) - static_cast<const base_type&>(rhs);
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(typename base_type::nominator_t{res.nominator()});
}
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator-() const {
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(
typename base_type::nominator_t{-static_cast<const base_type&>(*this).nominator()});
}
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator*(const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& rhs) const {
const auto res = static_cast<const base_type&>(*this) * static_cast<const base_type&>(rhs);
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(typename base_type::nominator_t{res.nominator()});
}
template<typename RhsUnderlyingType, int RhsPrecision>
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator*(const decimal_t<RhsUnderlyingType, RhsPrecision>& rhs) const {
base_type res = static_cast<const base_type&>(*this);
res *= rhs;
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(typename base_type::nominator_t{res.nominator()});
}
template<typename RhsUnderlyingType, int RhsPrecision>
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>
operator/(const decimal_t<RhsUnderlyingType, RhsPrecision>& rhs) const {
base_type res = static_cast<const base_type&>(*this) / rhs;
return ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>(typename base_type::nominator_t{res.nominator()});
}
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>&
operator+=(const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& rhs) {
static_cast<base_type&>(*this) += static_cast<const base_type&>(rhs);
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
return *this;
}
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>&
operator-=(const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& rhs) {
static_cast<base_type&>(*this) -= static_cast<const base_type&>(rhs);
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
return *this;
}
template<typename RhsUnderlyingType, int RhsDecimalPrecision>
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>&
operator*=(const decimal_t<RhsUnderlyingType, RhsDecimalPrecision>& rhs) {
static_cast<base_type&>(*this) *= rhs;
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
return *this;
}
template<typename RhsUnderlyingType, int RhsDecimalPrecision>
ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>&
operator/=(const decimal_t<RhsUnderlyingType, RhsDecimalPrecision>& rhs) {
static_cast<base_type&>(*this) /= rhs;
if (this->mNominator.value != base_type::NAN_VALUE) {
this->mNominator.value = std::clamp(this->mNominator.value, NOMINATOR_MIN_VALUE, NOMINATOR_MAX_VALUE);
}
return *this;
}
friend std::ostream& operator<<(std::ostream& out, const ranged_decimal_t<UnderlyingType, Precision, MinValue, MaxValue>& decimal) {
out << decimal.to_string();
return out;
}
};
template<typename NewUnderlyingType, int NewPrecision, typename OldUnderlyingType, int OldPrecision>
inline typename std::enable_if<NewPrecision != OldPrecision, decimal_t<NewUnderlyingType, NewPrecision>>::type
decimal_cast(const decimal_t<OldUnderlyingType, OldPrecision>& oldDecimal) noexcept {
NewUnderlyingType nominator = NewPrecision > OldPrecision ?
static_cast<NewUnderlyingType>(oldDecimal.nominator()) * Power10<NewUnderlyingType>(NewPrecision - OldPrecision + 1)
: static_cast<NewUnderlyingType>(oldDecimal.nominator()) / Power10<NewUnderlyingType>(OldPrecision - NewPrecision - 1);
int lastSignificantDigit = std::abs(nominator) % 10;
int signFactor = nominator >= 0 ? 1 : -1;
return decimal_t<NewUnderlyingType, NewPrecision>(typename decimal_t<NewUnderlyingType, NewPrecision>::nominator_t{
nominator / 10 + (lastSignificantDigit >= 5 ? signFactor : 0)
});
}
template<typename NewUnderlyingType, int NewPrecision, typename OldUnderlyingType, int OldPrecision>
inline typename std::enable_if<NewPrecision == OldPrecision, decimal_t<NewUnderlyingType, NewPrecision>>::type
decimal_cast(const decimal_t<OldUnderlyingType, OldPrecision>& oldDecimal) noexcept {
return decimal_t<NewUnderlyingType, NewPrecision>(typename decimal_t<NewUnderlyingType, NewPrecision>::nominator_t{
static_cast<NewUnderlyingType>(oldDecimal.nominator())
});
}
template<typename ResDecimalType, typename OldUnderlyingType, int OldPrecision>
inline ResDecimalType
decimal_cast(const decimal_t<OldUnderlyingType, OldPrecision>& oldDecimal) noexcept {
return decimal_cast<typename ResDecimalType::underlying_type, ResDecimalType::PRECISION,
OldUnderlyingType, OldPrecision>(oldDecimal);
}
template<typename UnderlyingType, int Precision>
const decimal_t<UnderlyingType, Precision>&
min(const decimal_t<UnderlyingType, Precision>& first, const decimal_t<UnderlyingType, Precision>& second)
{
return first < second ? first : second;
}
template<typename UnderlyingType, int Precision>
const decimal_t<UnderlyingType, Precision>&
max(const decimal_t<UnderlyingType, Precision>& first, const decimal_t<UnderlyingType, Precision>& second)
{
return first > second ? first : second;
}
using integer_t = decimal_t<int, 0>;
using size_t = decimal_t<size_t, 0>;
using float32_3d_t = decimal_t<int32_t, 3>;
using float32_2d_t = decimal_t<int32_t, 2>;
using ratio64_t = ranged_decimal_t<int64_t, 5, 0, 1>;
} // namespace strict