forked from Begun/libslave
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdecimal.cpp
171 lines (143 loc) · 4.73 KB
/
decimal.cpp
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
#include "decimal.h"
#include <alloca.h>
#include <cstring>
#include <ostream>
#include "decimal_supp.h"
#include "decimal_internal.h"
namespace
{
using slave::decimal::digit_t;
constexpr double scaler10[] = {1.0, 1e10, 1e20, 1e30, 1e40, 1e50, 1e60, 1e70, 1e80, 1e90};
constexpr double scaler1[] = {1.0, 10.0, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9};
inline size_t count_leading_zeroes(uint8_t digits, digit_t val)
{
size_t count = 0;
while (count < digits && val >= slave::decimal::powers10[count]) { ++count; }
return digits - count;
}
// @brief checks that [first1, std::min(std::distance(first1, last1), std::min(std::distance(first2, last2))
// is same and tail is zero
template <typename It, typename T>
inline bool equal_digits(It first1, It last1, It first2, It last2, T cmp)
{
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
{
if (!cmp(*first1, *first2))
return false;
}
// expected that tail is zero
for (; first1 != last1; ++first1)
{
if (0 != (*first1).first)
return false;
}
for (; first2 != last2; ++first2)
{
if (0 != (*first2).first)
return false;
}
return true;
}
} // namespace
bool slave::decimal::operator==(const Decimal& l, const Decimal& r)
{
if (l.sign != r.sign)
return false;
// check integer part
IntegerDigits idigitsl(l);
IntegerDigits idigitsr(r);
bool sEqual = equal_digits(idigitsl.rbegin(), idigitsl.rend(), idigitsr.rbegin(), idigitsr.rend(), [](const auto& x, const auto& y) {
return x.first == y.first;
});
if (!sEqual)
return false;
FractionalDigits fdigitsl(l);
FractionalDigits fdigitsr(r);
return equal_digits(fdigitsl.begin(), fdigitsl.end(), fdigitsr.begin(), fdigitsr.end(), [](const auto& x, const auto& y) {
if (x.second == y.second)
return x.first == y.first;
if (x.second > y.second)
return y.first * powers10[x.second - y.second] == x.first;
return x.first * powers10[y.second - x.second] == y.first;
});
}
std::ostream& slave::decimal::operator<<(std::ostream& os, const Decimal& d)
{
return os << to_string(d);
}
double slave::decimal::to_double(const Decimal& d)
{
// duplicate values to improve performance and avoid casting to double
static constexpr double base = DIGIT_BASE;
double result = 0.0;
unsigned exp = 0;
for (const auto x : IntegerDigits(d))
{
result = result * base + x.first;
}
for (const auto x : FractionalDigits(d))
{
result = result * scaler1[x.second] + x.first;
exp += x.second;
}
result /= scaler10[exp / 10u] * scaler1[exp % 10u];
return d.sign ? -result : result;
}
std::string slave::decimal::to_string(const Decimal& d)
{
IntegerDigits idigits(d);
size_t intg_len = d.intg;
if (0 != d.intg)
{
auto x = *idigits.begin();
intg_len -= count_leading_zeroes(x.second, x.first);
}
// unsigned because with size_t -Werror=alloca-larger-than gives an error with gcc 10.2.0.
// I don't know why.
unsigned len = intg_len + d.frac + d.sign + (0 == intg_len ? 1 : 0) + (0 != d.frac ? 1 : 0);
if (len > 0xffff)
return "slave::decimal::to_string: len is too big = " + std::to_string(len);
char* buf = reinterpret_cast<char*>(alloca(len));
char *s = buf;
if (0 != d.sign)
*s++ = '-';
// assign 0 at begin if there is no integer part
if (0 == intg_len)
*s++ = '0';
if (0 != d.frac)
{
char* s0 = s + intg_len;
*s0 = '.';
char* s1 = s0;
for (auto i : FractionalDigits(d))
{
digit_t x = i.first;
for (size_t j = i.second; 0 != j; --j)
{
digit_t y = x / powers10[j - 1];
*++s1 = '0' + static_cast<char>(y);
x -= y * powers10[j - 1];
}
}
// cut trailing zeros
while (s0 != s1 && '0' == *s1) { --s1; }
len = (s0 == s1) ? (s0 - buf) : ((s1 - buf) + 1);
}
if (0 != intg_len)
{
char* s1 = s + intg_len;
for (auto it = idigits.rbegin(), end = idigits.rend(); end != it; ++it)
{
auto vc = *it;
digit_t x = vc.first;
// for last item there may be less than vc.second digits, since we cat zero leading
for (size_t j = vc.second; 0 != j && s != s1; --j)
{
digit_t y = x / 10;
*--s1 = '0' + static_cast<char>(x - y * 10);
x = y;
}
}
}
return std::string(buf, len);
}