Debian Codegraph @

src/ITGSend/newran/format.cpp

raw symbols SCIP
1
2
3 #define WANT_STREAM
4 #define WANT_MATH
5 #define WANT_STRING
6
7
8 #include "include.h"
9 #include "str.h"
10 #include "format.h"
11
12 #ifdef use_namespace
13 namespace RBD_STRING { using namespace RBD_COMMON; }
14 namespace RBD_LIBRARIES { using namespace RBD_STRING; }
15 namespace RBD_STRING {
16 #endif
17
18
19 #ifdef use_namespace
20
21 OstreamWithFormat& endl(OstreamWithFormat& osf)
22 { osf.os << ::endl; return osf; }
23
24 OstreamWithFormat& ends(OstreamWithFormat& osf)
25 { osf.os << ::ends; return osf; }
26
27 OstreamWithFormat& flush(OstreamWithFormat& osf)
28 { osf.os << ::flush; return osf; }
29
30 #else
31
32 OstreamWithFormat& endl(OstreamWithFormat& osf)
33 { osf.os << endl; return osf; }
34
35 OstreamWithFormat& ends(OstreamWithFormat& osf)
36 { osf.os << ends; return osf; }
37
38 OstreamWithFormat& flush(OstreamWithFormat& osf)
39 { osf.os << flush; return osf; }
40
41 #endif
42
43
44 OstreamWithFormat& OstreamWithFormat::operator<<(const char* value)
45 { f.StringPrint(os, value, strlen(value)); return *this; }
46
47
48 void Format::FormatOut(ostream& os, double value) const
49 {
50 os << prefix_val;
51 switch (format_type_val)
52 {
53 case Format::SIG_FIGS:
54 SigFig(os, value, precision_val);
55 break;
56
57 case Format::DEC_FIGS:
58 DecFig(os, value, precision_val);
59 break;
60
61 case Format::SCIENTIFIC:
62 Scientific(os, value, precision_val);
63 break;
64
65 case Format::INTEGER:
66 DecFig(os, value, 0);
67 break;
68 }
69 os << suffix_val;
70 }
71
72 void Format::IntegerPrint(ostream& os, double value) const
73 { os << prefix_val; DecFig(os, value, 0); os << suffix_val; }
74
75
76 void Format::StringPrint(ostream& os, const char* value, int size) const
77 {
78 int i;
79
80 // prefix
81 os << prefix_val;
82
83 // will it fit
84 if (size > max_width_val) { ErrorOut(os); return; }
85
86 // padding
87 int spaces;
88 if (size < min_width_val) spaces = min_width_val - size; else spaces = 0;
89 int s1, s2; // leading and following spaces
90 if (alignment_val == Format::LEFT) { s1 = 0; s2 = spaces; }
91 else if (alignment_val == Format::RIGHT) { s1 = spaces; s2 = 0; }
92 else { s1 = spaces / 2; s2 = spaces - s1; }
93
94 // leading spaces
95 for (i = 0; i < s1; i++) os << ' ';
96
97 // the string
98 for (i = 0; i < size; i++) os << value[i];
99
100 // trailing blanks
101 for (i = 0; i < s2; i++) os << ' ';
102
103 // suffix
104 os << suffix_val;
105 }
106
107 void Format::SigFig(ostream& os, double value, int nsig) const
108 {
109 bool variant1 = (variant_val == Format::VAR1);
110 double v = value;
111 if (v == 0.0)
112 {
113 if (variant1) DecFig(os, 0.0, nsig);
114 else DecFig(os, 0.0, nsig-1);
115 return;
116 }
117
118 // sign
119 bool neg;
120 if (v < 0) { v = -v; neg = true; }
121 else neg = false;
122
123 // exponent - first try
124 int ndec = nsig - (int)floor(log10(v)) - 1; // number of decimals
125
126 // round
127 double p = pow(10.0, ndec); // 10**ndec
128 double rv = floor(v * p + 0.5); // rounded value
129
130 // exponent - adjust
131 if (rv >= pow(10.0, nsig)) { ndec--; p /= 10; rv = floor(v * p + 0.5); }
132
133 // make rounded value
134 v = rv / p;
135 if (neg) v = -v;
136
137 // width for decimal format
138 int w = nsig;
139 if (ndec > 0)
140 {
141 w++; // for decimal
142 if (ndec >= nsig)
143 {
144 if (!variant1) ++w; // leading zero
145 w += (ndec - nsig); // additional zeros
146 }
147 }
148 else { w -= ndec; ndec = 0; } // additional zeros
149 if (neg || positive_val != Format::NX) w++; // for sign
150
151 // use scientific or decimal format
152 // use Scientific
153 // if number is large and won't fit
154 // number is small and won't fit and underflow policy is to use sci.
155 if (w > max_width_val)
156 {
157 if (ndec == 0) Scientific(os, value, nsig);
158 else if (underflow_policy_val == Format::E)
159 Scientific(os, value, nsig);
160 else DecFig(os, value, ndec, true);
161 }
162 else DecFig(os, v, ndec, true);
163 }
164
165 void Format::DecFig(ostream& os, double value, int ndec,
166 bool force_decimal) const
167 {
168 int i;
169 double v = value;
170 bool variant1 = (variant_val == Format::VAR1);
171
172 // sign
173 bool neg;
174 if (v < 0) { v = -v; neg = true; } else neg = false;
175
176 // round and break into integer and decimal bits
177 // iv will contain the integer part
178 // v will contain the rounded decimal part, multiplied up to make an integer
179 double p = pow(10.0,ndec); // 10 ** ndec
180 double iv; // integer part
181 v = modf(v, &iv); // decimal parts
182 v = floor(v * p + 0.5); // rounded decimal part
183 if (v >= p) { iv += 1.0; v -= p; } // decimal part over 1
184
185 // check for underflow
186 if ( iv == 0 &&
187 value != 0.0 &&
188 underflow_policy_val == Format::E &&
189 !force_decimal &&
190 (!variant1 || v < 0.1) )
191 { Scientific(os, value, ndec+1); return; }
192
193 // space available for integer part
194 int li = max_width_val - ndec; // max space for integer part
195 int lj = min_width_val - ndec; // min space for integer part
196
197 // space for sign
198 if (neg || positive_val != Format::NX) { li--; lj--; }
199
200 // are we going to show the decimal point
201 bool dp = (ndec > 0);
202 if (dp) { li--; lj--; } // space for decimal point
203
204 // try again with reduced number of decimals
205 if (variant1)
206 {
207 if (li < 0)
208 { DecFig(os, value, ndec+li, force_decimal); return; }
209 }
210 else
211 {
212 if (li < 1)
213 { DecFig(os, value, ndec+li-1, force_decimal); return; }
214 }
215
216 String si(li, ' ');
217 int nchar = 0;
218 for (i = li-1; i >= 0; i--)
219 {
220 if (iv < 0.5) break;
221 nchar++; // chars in integer part
222 double x; x = modf(iv/10, &iv);
223 si[i] = (char)('0' + (int)floor(x * 10 + 0.5));
224 }
225 if (iv > 0.5) // won't fit
226 {
227 if (overflow_policy_val == Format::HASH) ErrorOut(os);
228 else Scientific(os, value, max_width_val-4);// maybe do precision better
229 return;
230 }
231 if (nchar == 0 && !variant1)
232 { nchar = 1; si[li-1] = '0'; } // integer part is zero
233
234 int spaces = lj - nchar; // number of spaces
235 if (spaces < 0) spaces = 0;
236 int s1, s2; // leading and following sp
237 if (alignment_val == Format::LEFT) { s1 = 0; s2 = spaces; }
238 else if (alignment_val == Format::RIGHT) { s1 = spaces; s2 = 0; }
239 else { s1 = spaces / 2; s2 = spaces - s1; }
240
241 // in VAR1 variant, if there are no decimals but there is room for
242 // the decimal point, put it in.
243 if (ndec == 0 && variant1 && s1 > 0) { dp = true; --s1; }
244
245 // leading spaces
246 for (i = 0; i < s1; i++) os << ' ';
247
248 // sign
249 if (neg) os << '-';
250 else if (positive_val == Format::SPACE) os << ' ';
251 else if (positive_val == Format::PLUS) os << '+';
252
253 // integer part
254 for (i = 0; i < nchar; i++) os << si[li - nchar + i];
255
256 // decimal part
257 if (dp)
258 {
259 os << '.';
260 String sv(ndec, '0');
261 for (i = ndec-1; i >= 0; i--)
262 {
263 double x; x = modf(v/10, &v);
264 sv[i] = (char)('0' + (int)floor(x * 10 + 0.5));
265 }
266 for (i = 0; i < ndec; i++) os << sv[i];
267 }
268
269 // trailing blanks
270 for (i = 0; i < s2; i++) os << ' ';
271
272 }
273
274 void Format::Scientific(ostream& os, double value, int nsig) const
275 {
276 int i;
277 double v = value;
278
279 // check nsig >= 0
280 if (nsig < 0) { ErrorOut(os); return; }
281
282 // sign
283 bool neg;
284 if (v < 0) { v = -v; neg = true; }
285 else neg = false;
286
287 // exponent - first try
288 int lv;
289 if (v == 0.0) lv = 0;
290 else lv = (int)floor(log10(v)); // power of 10 - first try
291 v /= pow(10.0,lv); // normalise by power of 10
292
293 // round
294 double p = pow(10.0,nsig-1); // aiming at nsig-1 decimal places
295 double rv = floor(v * p + 0.5); // rounded value * p
296
297 // exponent - adjust
298 if (rv >= 10.0 * p)
299 { lv++; v /= 10.0; rv = floor(v * p + 0.5); }
300
301 // will it fit
302 int w = nsig;
303 if (nsig > 1) w++; // for decimal
304 if (neg || positive_val != Format::NX) w++; // for sign
305 int np; // decimals in power part
306 if (lv >=100 || lv <= -100)
307 { if (lv >=1000 || lv <= -1000) np = 4; else np = 3; }
308 else np = 2;
309 w += np + 2; // +2 is for e and sign
310 if (w > max_width_val)
311 {
312 int ov = w - max_width_val; // overflow
313 // can rounding shift us from E-100 to E-99
314 if ((lv == -100 || lv == -1000) && ov == 1)
315 {
316 double p1 = p / 10.0;
317 double rv1 = floor(v * p1 + 0.5);
318 if (rv1 >= p)
319 {
320 double v1 = rv1 * pow(10.0, lv) / p1;
321 if (neg) v1 = -v1;
322 Scientific(os, v1, nsig);
323 return;
324 }
325 }
326 if (overflow_policy_val == Format::HASH) { ErrorOut(os); return; }
327 if (ov > 1) --ov; // in case characteristic length
328 // drops when we round
329 if (ov > 1) --ov; // in case we can drop decimal
330 nsig -= ov; // how many sig figures
331 if (nsig <= 0) { ErrorOut(os); return; } // can't print
332 Scientific(os, value, nsig); // try again with reduced nsig
333 return;
334 }
335
336 // spaces
337 int spaces = min_width_val - w;
338 if (spaces < 0) spaces = 0;
339 int s1, s2; // leading and following sp
340 if (alignment_val == Format::LEFT) { s1 = 0; s2 = spaces; }
341 else if (alignment_val == Format::RIGHT) { s1 = spaces; s2 = 0; }
342 else { s1 = spaces / 2; s2 = spaces - s1; }
343
344 // leading spaces
345 for (i = 0; i < s1; i++) os << ' ';
346
347 // sign
348 if (neg) os << '-';
349 else if (positive_val == Format::SPACE) os << ' ';
350 else if (positive_val == Format::PLUS) os << '+';
351
352 // decimal part
353 String sv; sv.resize(nsig);
354 for (i = nsig-1; i >= 0; i--)
355 {
356 double x; x = modf(rv/10, &rv);
357 sv[i] = (char)('0' + (int)floor(x * 10 + 0.5));
358 }
359 os << sv[0]; // first digit
360 if (nsig > 1) // need decimal
361 { os << '.'; for (int i = 1; i < nsig; i++) os << sv[i]; }
362
363 // power part
364 os << 'e';
365 if (lv >= 0) os << '+'; else { os << '-'; lv = -lv; }
366 sv.resize(np);
367 for (i = np-1; i >= 0; i--)
368 {
369 int lv1 = lv / 10; int x = lv - lv1 * 10; lv = lv1;
370 sv[i] = (char)('0' + x);
371 }
372 for (i = 0; i < np; i++) os << sv[i];
373
374 // trailing blanks
375 for (i = 0; i < s2; i++) os << ' ';
376 }
377
378 void Format::ErrorOut(ostream& os) const
379 {
380 if (min_width_val == 0) os << '#';
381 else { for (int i = 0; i < min_width_val; i++) os << '#'; }
382 }
383
384
385 #ifdef use_namespace
386 }
387 #endif
388