/
around.h
223 lines (191 loc) · 7.79 KB
/
around.h
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
#include <cstdio>
#include <cmath>
namespace ext {
// around_detail
// - implementation details for 'around' function
//
namespace around_detail {
// needle
// - generates static NUL-terminated N-character long string of C characters
// - example: needle<3,'a'>::string is static NUL-terminated string of three 'a' characters, i.e: "aaa"
//
template <std::size_t N, char C>
struct needle {
constexpr needle () {
for (std::size_t i = 0; i != N; ++i) {
string [i] = C;
}
string [N] = '\0';
}
char string [N + 1];
};
// suggestion
// - internal helper, result of 'suggest' function
// - 'precision' is the determined/suggested precision for rounding
// - 'decimals' points to first decimal digit in the input string
//
struct suggestion {
std::size_t precision = (std::size_t) -1;
char * decimals = nullptr;
};
// suggest
// - internal helper, suggests rounding precision for decimal number passed as string
// - this is the nutshell of the algorithm
//
template <std::size_t precision = 4>
suggestion suggest (char * string) {
suggestion result;
if (auto p = std::strchr (string, '.')) {
++p;
result.decimals = p;
auto p9 = std::strstr (p, around_detail::needle <precision, '9'> ().string);
auto p0 = std::strstr (p, around_detail::needle <precision, '0'> ().string);
if (p0 && p9) {
result.precision = std::min (p0 - p, p9 - p);
} else
if (p9) {
result.precision = p9 - p;
} else
if (p0) {
result.precision = p0 - p;
}
} else {
result.decimals = std::strchr (string, '\0');
result.precision = 0;
}
return result;
}
// suggest
// - internal helper, suggests rounding precision for the floating-point value
// - converts it to string and uses the decision above
//
template <std::size_t precision = 4>
suggestion suggest (double value, char * buffer, std::size_t length) {
if (std::snprintf (buffer, length, "%.*f", (unsigned int) length, value)) {
return suggest <precision> (buffer);
} else
return suggestion {};
}
// round
// - internal helper, performs the actual rounding on the string
// - 'suggested.decimals' MUST point into valid string representation
// of the floating-point value PREFIXED with space character
//
inline void round (suggestion suggested,
std::size_t minimum, std::size_t maximum) {
auto n = suggested.precision;
auto decimals = suggested.decimals;
if (n < minimum) n = minimum;
if (n > maximum) n = maximum;
if (n != (std::size_t) -1) {
switch (decimals [n]) {
case '0':
if (n == 0) {
decimals [-1] = '\0';
} else {
decimals [n] = '\0';
}
break;
case '9':
do {
--n;
} while (decimals [n] == '9');
if (decimals [n] == '.') {
decimals [n] = '\0';
--n;
while (decimals [n] == '9') {
decimals [n] = '0';
--n;
}
switch (decimals [n]) {
case '-':
decimals [n - 1] = '-';
[[ fallthrough ]];
case ' ':
decimals [n] = '1';
break;
default:
++decimals [n];
}
} else {
decimals [n + 0]++;
decimals [n + 1] = '\0';
}
break;
}
}
}
}
// around_suggest
// - suggests rounding precision
// - returns: suggested precision or (std::size_t) -1
//
template <std::size_t precision = 4>
std::size_t around_suggest (double value) {
char string [64];
return around_detail::suggest <precision> (value, string, sizeof string).precision;
}
// around_suggest
// - suggests rounding precision
// - returns: suggested precision or (std::size_t) -1
//
template <std::size_t precision = 4>
std::size_t around_suggest (const char * value) {
return around_detail::suggest <precision> (value).precision;
}
// around
// - adaptive rounding operation; attempts nice rounding of a floating-point number, like a human would do
// - rounds 'value' to 'around_suggest'ed precision and returns it
// - prefer string output versions of the call
//
template <std::size_t precision = 4>
double around (double value,
std::size_t minimum = 0, std::size_t maximum = (std::size_t) -1) {
auto n = around_suggest <precision> (value);
if (n < minimum) n = minimum;
if (n > maximum) n = maximum;
auto m = std::pow (10, n);
return std::round (value * m) / m;
}
// around
// - adaptive rounding operation; attempts nice rounding of a floating-point number, like a human would do
// - 'value' must be in "-123.456" format, no extra spaces, simple minus, dot for decimal separator
// - rounded result is stored as string into 'buffer'
// - minimum/maximum can limit the number of actual decimals should the algorithm decide outside of the range
//
template <std::size_t precision = 4>
void around (const char * value, char * buffer, std::size_t length,
std::size_t minimum = 0, std::size_t maximum = (std::size_t) -1) {
using namespace around_detail;
if (length > 1) {
#ifdef _CRT_SECURE_NO_WARNINGS
std::strncpy (&buffer [1], value, length - 2);
#else
strncpy_s (&buffer [1], length - 1, value, length - 2);
#endif
buffer [0] = ' ';
buffer [length - 1] = '\0';
round (suggest <precision> (buffer + 1), minimum, maximum);
if (buffer [0] == ' ') {
std::memmove (&buffer [0], &buffer [1], length - 1);
}
}
}
// around
// - adaptive rounding operation; attempts nice rounding of a floating-point number, like a human would do
// - rounded result is stored as string into 'buffer'
// - minimum/maximum can limit the number of actual decimals should the algorithm decide outside of the range
//
template <std::size_t precision = 4>
void around (double value, char * buffer, std::size_t length,
std::size_t minimum = 0, std::size_t maximum = (std::size_t) -1) {
using namespace around_detail;
if (length > 1) {
buffer [0] = ' ';
round (suggest <precision> (value, buffer + 1, length - 1), minimum, maximum);
if (buffer [0] == ' ') {
std::memmove (&buffer [0], &buffer [1], length - 1);
}
}
}
}