-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharound.cpp
227 lines (198 loc) · 7.51 KB
/
around.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
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
#include "around.h"
#include <algorithm>
#include <cstring>
#include <cmath>
// around_detail
// - implementation details for 'around' function
//
namespace {
// 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
//
suggestion suggest (char * string, std::size_t precision) {
suggestion result;
if (auto p = std::strchr (string, '.')) {
++p;
result.decimals = p;
char needle [2][16];
if (precision > 15) {
precision = 15;
}
for (std::size_t i = 0; i != precision; ++i) {
needle [0][i] = '9';
needle [1][i] = '0';
}
needle [0][precision] = '\0';
needle [1][precision] = '\0';
auto p9 = std::strstr (p, needle [0]);
auto p0 = std::strstr (p, needle [1]);
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
//
suggestion suggest (double value, char * buffer, std::size_t length, std::size_t precision) {
if (std::snprintf (buffer, length, "%.*f", (unsigned int) length, value)) {
return suggest (buffer, precision);
} 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
//
char * round (suggestion suggested, std::size_t minimum, std::size_t maximum) {
auto n = suggested.precision;
auto decimals = suggested.decimals;
if (n > maximum) {
n = maximum;
}
if (n != (std::size_t) -1) {
switch (decimals [n]) {
case '0':
if (n < minimum) {
n = minimum;
}
if (n == 0) {
return &decimals [-1];
} else {
return &decimals [n];
}
case '9':
do {
--n;
} while (decimals [n] == '9');
if (decimals [n] == '.') {
char * endptr = nullptr;
if (minimum) {
std::size_t i = 1;
while (i != minimum + 1) {
decimals [n + i++] = '0';
}
endptr = &decimals [n + i];
} else {
endptr = &decimals [n];
}
--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];
}
return endptr;
} else {
decimals [n++]++;
while (n < minimum) {
decimals [n++] = '0';
}
return &decimals [n];
}
}
}
return &decimals [n];
}
}
// around_suggest
// - suggests rounding precision
// - returns: suggested precision or (std::size_t) -1
//
std::size_t ext::around_suggest (double value, std::size_t precision) {
char string [64];
return suggest (value, string, sizeof string, precision).precision;
}
// around_suggest
// - suggests rounding precision
// - returns: suggested precision or (std::size_t) -1
//
std::size_t ext::around_suggest (const char * value, std::size_t precision) {
return suggest ((char *) value, precision).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
//
double ext::around (double value, std::size_t precision, std::size_t minimum, std::size_t maximum) {
auto n = around_suggest (value, precision);
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' (NOT NUL-terminated)
// - minimum/maximum can limit the number of actual decimals should the algorithm decide outside of the range
// - returns number of characters in the final string
//
std::size_t ext::around (const char * value, char * buffer, std::size_t length, std::size_t precision, std::size_t minimum, std::size_t maximum) {
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';
auto rv = round (suggest (buffer + 1, precision), minimum, maximum) - buffer;
if (buffer [0] == ' ') {
std::memmove (&buffer [0], &buffer [1], length - 1);
--rv;
}
return rv;
} else
return 0;
}
// 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' (NOT NUL-terminated)
// - minimum/maximum can limit the number of actual decimals should the algorithm decide outside of the range
// - returns number of characters in the final string
//
std::size_t ext::around (double value, char * buffer, std::size_t length, std::size_t precision, std::size_t minimum, std::size_t maximum) {
if (length > 1) {
buffer [0] = ' ';
auto rv = round (suggest (value, buffer + 1, length - 1, precision), minimum, maximum) - buffer;
if (buffer [0] == ' ') {
std::memmove (&buffer [0], &buffer [1], length - 1);
--rv;
}
return rv;
} else
return 0;
}