/src/r-source/src/main/format.c
Line | Count | Source |
1 | | /* |
2 | | * R : A Computer Language for Statistical Data Analysis |
3 | | * Copyright (C) 1997--2025 The R Core Team |
4 | | * Copyright (C) 2003--2016 The R Foundation |
5 | | * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, a copy is available at |
19 | | * https://www.R-project.org/Licenses/ |
20 | | * |
21 | | * |
22 | | * Object Formatting |
23 | | * |
24 | | * See ./paste.c for do_paste() , do_format() and do_formatinfo() and |
25 | | * ./util.c for do_formatC() |
26 | | * See ./printutils.c for general remarks on Printing and the Encode.. utils. |
27 | | * See ./print.c for do_printdefault, do_prmatrix, etc. |
28 | | * |
29 | | * Exports |
30 | | * formatString |
31 | | * formatStringS |
32 | | * formatLogical |
33 | | * formatLogicalS |
34 | | * formatInteger |
35 | | * formatIntegerS |
36 | | * formatReal |
37 | | * formatRealS |
38 | | * formatComplex |
39 | | * formatComplexS |
40 | | * |
41 | | * These formatFOO() functions determine the proper width, digits, etc. |
42 | | * |
43 | | * formatFOOS() functions behave identically to formatFOO |
44 | | * except that they accept a SEXP rather than a data pointer |
45 | | */ |
46 | | |
47 | | #ifdef HAVE_CONFIG_H |
48 | | #include <config.h> |
49 | | #endif |
50 | | |
51 | | #include <Defn.h> |
52 | | #include <float.h> /* for DBL_EPSILON */ |
53 | | #include <Rmath.h> |
54 | | #include <Print.h> |
55 | | #include <R_ext/Itermacros.h> /* for ITERATE_BY_REGION */ |
56 | | |
57 | | /* this is just for conformity with other types */ |
58 | | attribute_hidden |
59 | | void formatRaw(const Rbyte *x, R_xlen_t n, int *fieldwidth) |
60 | 0 | { |
61 | 0 | *fieldwidth = 2; |
62 | 0 | } |
63 | | |
64 | | attribute_hidden |
65 | | void formatRawS(SEXP x, R_xlen_t n, int *fieldwidth) |
66 | 0 | { |
67 | 0 | *fieldwidth = 2; |
68 | 0 | } |
69 | | |
70 | | |
71 | | attribute_hidden |
72 | | void formatString(const SEXP *x, R_xlen_t n, int *fieldwidth, int quote) |
73 | 98.5k | { |
74 | 98.5k | int xmax = 0; |
75 | 98.5k | int l; |
76 | | |
77 | 235k | for (R_xlen_t i = 0; i < n; i++) { |
78 | 137k | if (x[i] == NA_STRING) { |
79 | 526 | l = quote ? R_print.na_width : R_print.na_width_noquote; |
80 | 136k | } else l = Rstrlen(x[i], quote) + (quote ? 2 : 0); |
81 | 137k | if (l > xmax) xmax = l; |
82 | 137k | } |
83 | 98.5k | *fieldwidth = xmax; |
84 | 98.5k | } |
85 | | |
86 | | /* currently there is no STRING_GET_REGION */ |
87 | | |
88 | | attribute_hidden |
89 | | void formatStringS(SEXP x, R_xlen_t n, int *fieldwidth, int quote) |
90 | 2.25k | { |
91 | 2.25k | int xmax = 0; |
92 | 2.25k | int l; |
93 | | |
94 | 20.7k | for (R_xlen_t i = 0; i < n; i++) { |
95 | 18.5k | if (STRING_ELT(x, i) == NA_STRING) { |
96 | 0 | l = quote ? R_print.na_width : R_print.na_width_noquote; |
97 | 18.5k | } else l = Rstrlen(STRING_ELT(x, i), quote) + (quote ? 2 : 0); |
98 | 18.5k | if (l > xmax) xmax = l; |
99 | 18.5k | } |
100 | 2.25k | *fieldwidth = xmax; |
101 | 2.25k | } |
102 | | |
103 | | |
104 | | |
105 | | attribute_hidden |
106 | | void formatLogical(const int *x, R_xlen_t n, int *fieldwidth) |
107 | 101k | { |
108 | 101k | *fieldwidth = 1; |
109 | 194k | for(R_xlen_t i = 0 ; i < n; i++) { |
110 | 103k | if (x[i] == NA_LOGICAL) { |
111 | 10.6k | if(*fieldwidth < R_print.na_width) |
112 | 9.63k | *fieldwidth = R_print.na_width; |
113 | 92.6k | } else if (x[i] != 0 && *fieldwidth < 4) { // 'TRUE' |
114 | 81.7k | *fieldwidth = 4; |
115 | 81.7k | } else if (x[i] == 0 && *fieldwidth < 5 ) {// 'FALSE' |
116 | 10.1k | *fieldwidth = 5; |
117 | 10.1k | break; |
118 | | // FIXME: Need to ensure that na_width > 5 is not allowed (*and* document in ?print.default) |
119 | | /* this is the widest it can be, so stop */ |
120 | 10.1k | } |
121 | 103k | } |
122 | 101k | } |
123 | | |
124 | | attribute_hidden |
125 | 0 | void formatLogicalS(SEXP x, R_xlen_t n, int *fieldwidth) { |
126 | 0 | *fieldwidth = 1; |
127 | 0 | int tmpfieldwidth = 1; |
128 | 0 | ITERATE_BY_REGION_PARTIAL(x, px, idx, nb, int, LOGICAL, 0, n, |
129 | 0 | { |
130 | 0 | formatLogical(px, nb, &tmpfieldwidth); |
131 | 0 | if( tmpfieldwidth > *fieldwidth ) |
132 | 0 | *fieldwidth = tmpfieldwidth; |
133 | 0 | if( *fieldwidth == 5) |
134 | 0 | break; /* break iteration loop */ |
135 | 0 | }); |
136 | 0 | return; |
137 | 0 | } |
138 | | |
139 | | |
140 | | /* needed in 2 places so rolled out into macro |
141 | | to avoid divergence |
142 | | */ |
143 | 15.0M | #define FORMATINT_RETLOGIC do { \ |
144 | 15.0M | if (naflag) *fieldwidth = R_print.na_width; \ |
145 | 15.0M | else *fieldwidth = 1; \ |
146 | 15.0M | \ |
147 | 15.0M | if (xmin < 0) { \ |
148 | 169k | l = IndexWidth(-xmin) + 1; /* +1 for sign */ \ |
149 | 169k | if (l > *fieldwidth) *fieldwidth = l; \ |
150 | 169k | } \ |
151 | 15.0M | if (xmax > 0) { \ |
152 | 6.10M | l = IndexWidth(xmax); \ |
153 | 6.10M | if (l > *fieldwidth) *fieldwidth = l; \ |
154 | 6.10M | } \ |
155 | 15.0M | } while(0) |
156 | | |
157 | | attribute_hidden |
158 | | void formatInteger(const int *x, R_xlen_t n, int *fieldwidth) |
159 | 15.0M | { |
160 | 15.0M | int xmin = INT_MAX, xmax = INT_MIN, naflag = 0; |
161 | 15.0M | int l; |
162 | | |
163 | 30.1M | for (R_xlen_t i = 0; i < n; i++) { |
164 | 15.0M | if (x[i] == NA_INTEGER) |
165 | 423k | naflag = 1; |
166 | 14.6M | else { |
167 | 14.6M | if (x[i] < xmin) xmin = x[i]; |
168 | 14.6M | if (x[i] > xmax) xmax = x[i]; |
169 | 14.6M | } |
170 | 15.0M | } |
171 | 15.0M | FORMATINT_RETLOGIC; |
172 | 15.0M | } |
173 | | |
174 | | attribute_hidden |
175 | | void formatIntegerS(SEXP x, R_xlen_t n, int *fieldwidth) |
176 | 188 | { |
177 | | |
178 | 188 | int xmin = INT_MAX, xmax = INT_MIN, naflag = 0, |
179 | 188 | sorted; |
180 | 188 | SEXP tmpmin = NULL, tmpmax = NULL; |
181 | | /* |
182 | | min and max should be VERY cheap when sortedness |
183 | | is known, so better to call them both than loop |
184 | | through whole vector even once |
185 | | |
186 | | Shouldn't need to check for ALTREPness here |
187 | | because KNOWN_SORTED(sorted) will never be |
188 | | true for non-ALTREPs or "exploded" ALTREPs |
189 | | */ |
190 | 188 | sorted = INTEGER_IS_SORTED(x); |
191 | | /* if we're not formatting/printing the whole thing |
192 | | ALTINTEGER_MIN/MAX will give us the wrong thing |
193 | | anyway */ |
194 | 188 | if(n == XLENGTH(x) && KNOWN_SORTED(sorted)) { |
195 | 0 | tmpmin = PROTECT(ALTINTEGER_MIN(x, TRUE)); |
196 | 0 | tmpmax = PROTECT(ALTINTEGER_MAX(x, TRUE)); |
197 | 0 | naflag = KNOWN_NA_1ST(sorted) ? |
198 | 0 | INTEGER_ELT(x, 0) == NA_INTEGER : |
199 | 0 | INTEGER_ELT(x, XLENGTH(x) - 1) == NA_INTEGER; |
200 | 0 | UNPROTECT(2); /* tmpmin, tmpmax */ |
201 | 0 | } |
202 | | |
203 | | /* |
204 | | If we don't have min/max methods or they need to punt |
205 | | for some reason we will get NULL. |
206 | | |
207 | | The data are integers, so the only reason we would not |
208 | | get INTSXP answers is if we got Inf/-Inf because |
209 | | everything was NA. |
210 | | |
211 | | In both of the above cases we will |
212 | | do things the hard way below |
213 | | */ |
214 | 188 | if(tmpmin != NULL && tmpmax != NULL && |
215 | 0 | TYPEOF(tmpmin) == INTSXP && TYPEOF(tmpmax) == INTSXP) { |
216 | 0 | int l; /* only needed here so defined locally */ |
217 | 0 | PROTECT(tmpmin); |
218 | 0 | PROTECT(tmpmax); |
219 | 0 | xmin = INTEGER_ELT(tmpmin, 0); |
220 | 0 | xmax = INTEGER_ELT(tmpmax, 0); |
221 | 0 | UNPROTECT(2); /* tmpmin, tmpmax */ |
222 | | /* naflag set above */ |
223 | | |
224 | | /* this is identical logic to what formatInteger |
225 | | does */ |
226 | 0 | FORMATINT_RETLOGIC; |
227 | 188 | } else { |
228 | | /* |
229 | | no fastpass so just format using formatInteger |
230 | | by region. No need for FORMATINT_RETLOGIC |
231 | | here because it happens inside all the |
232 | | formatInteger calls. |
233 | | */ |
234 | 188 | int tmpfw = 1; |
235 | 188 | *fieldwidth = 1; |
236 | 188 | ITERATE_BY_REGION_PARTIAL(x, px, idx, nb, int, INTEGER, 0, n, |
237 | 188 | { |
238 | 188 | formatInteger(px, nb, &tmpfw); |
239 | 188 | if(tmpfw > *fieldwidth) |
240 | 188 | *fieldwidth = tmpfw; |
241 | 188 | }); |
242 | 188 | } |
243 | 188 | } |
244 | | |
245 | | /*--------------------------------------------------------------------------- |
246 | | * scientific format determination for real numbers. |
247 | | * This is time-critical code. It is worth optimizing. |
248 | | * |
249 | | * nsig digits altogether |
250 | | * kpower+1 digits to the left of "." |
251 | | * kpower+1+sgn including sign |
252 | | * |
253 | | * Using GLOBAL R_print.digits -- had #define MAXDIG R_print.digits |
254 | | */ |
255 | | |
256 | | /* Very likely everyone has nearbyintl now (2018), but it took until |
257 | | 2012 for FreeBSD to get it, and longer for Cygwin. |
258 | | */ |
259 | | #if defined(HAVE_LONG_DOUBLE) && (SIZEOF_LONG_DOUBLE > SIZEOF_DOUBLE) |
260 | | # ifdef HAVE_NEARBYINTL |
261 | 10.8M | # define R_nearbyintl nearbyintl |
262 | | # elif defined(HAVE_RINTL) |
263 | | # define R_nearbyintl rintl |
264 | | # else |
265 | | # define R_nearbyintl private_nearbyintl |
266 | | LDOUBLE private_nearbyintl(LDOUBLE x) |
267 | | { |
268 | | LDOUBLE x1; |
269 | | x1 = - floorl(-x + 0.5); |
270 | | x = floorl(x + 0.5); |
271 | | if (x == x1) return(x); |
272 | | else { |
273 | | /* FIXME: we should really test for floorl, also C99. |
274 | | But FreeBSD 7.x does have it, but not nearbyintl */ |
275 | | if (x/2.0 == floorl(x/2.0)) return(x); else return(x1); |
276 | | } |
277 | | } |
278 | | # endif |
279 | | #endif |
280 | | |
281 | 2.80k | #define NB 1000 |
282 | | static void format_via_sprintf(double r, int d, int *kpower, int *nsig) |
283 | 2.80k | { |
284 | 2.80k | static char buff[NB]; |
285 | 2.80k | int i; |
286 | 2.80k | snprintf(buff, NB, "%#.*e", d - 1, r); |
287 | 2.80k | *kpower = (int) strtol(buff + (d + 2), NULL, 10); |
288 | 41.5k | for (i = d; i >= 2; i--) |
289 | 40.0k | if (buff[i] != '0') break; |
290 | 2.80k | *nsig = i; |
291 | 2.80k | } |
292 | | |
293 | | |
294 | | #if defined(HAVE_LONG_DOUBLE) && (SIZEOF_LONG_DOUBLE > SIZEOF_DOUBLE) |
295 | | static const long double tbl[] = |
296 | | { |
297 | | /* Powers exactly representable with 64 bit mantissa */ |
298 | | 1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09, |
299 | | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
300 | | 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27 |
301 | | }; |
302 | 37.4M | #define KP_MAX 27 |
303 | | #else |
304 | | static const double tbl[] = |
305 | | { |
306 | | 1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09, |
307 | | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
308 | | 1e20, 1e21, 1e22 |
309 | | }; |
310 | | #define KP_MAX 22 |
311 | | #endif |
312 | | |
313 | | static void |
314 | | scientific(const double *x, int *neg, int *kpower, int *nsig, bool *roundingwidens) |
315 | 15.0M | { |
316 | | /* for a number x , determine |
317 | | * neg = 1_{x < 0} {0/1} |
318 | | * kpower = Exponent of 10; |
319 | | * nsig = min(R_print.digits, #{significant digits of alpha}) |
320 | | * roundingwidens = true iff rounding causes x to increase in width |
321 | | * |
322 | | * where |x| = alpha * 10^kpower and 1 <= alpha < 10 |
323 | | */ |
324 | 15.0M | register double alpha; |
325 | 15.0M | register double r; |
326 | 15.0M | register int kp; |
327 | 15.0M | int j; |
328 | | |
329 | 15.0M | if (*x == 0.0) { |
330 | 4.24M | *kpower = 0; |
331 | 4.24M | *nsig = 1; |
332 | 4.24M | *neg = 0; |
333 | 4.24M | *roundingwidens = false; |
334 | 10.8M | } else { |
335 | 10.8M | if(*x < 0.0) { |
336 | 675k | *neg = 1; r = -*x; |
337 | 10.1M | } else { |
338 | 10.1M | *neg = 0; r = *x; |
339 | 10.1M | } |
340 | 10.8M | if (R_print.digits >= DBL_DIG + 1) { |
341 | 2.80k | format_via_sprintf(r, R_print.digits, kpower, nsig); |
342 | 2.80k | *roundingwidens = false; |
343 | 2.80k | return; |
344 | 2.80k | } |
345 | 10.8M | kp = (int) floor(log10(r)) - R_print.digits + 1;/* r = |x|; 10^(kp + digits - 1) <= r */ |
346 | 10.8M | #if defined(HAVE_LONG_DOUBLE) && (SIZEOF_LONG_DOUBLE > SIZEOF_DOUBLE) |
347 | 10.8M | long double r_prec = r; |
348 | | /* use exact scaling factor in long double precision, if possible */ |
349 | 10.8M | if (abs(kp) <= KP_MAX) { |
350 | 9.70M | if (kp > 0) r_prec /= tbl[kp]; else if (kp < 0) r_prec *= tbl[ -kp]; |
351 | 9.70M | } |
352 | 1.10M | # ifdef HAVE_POWL |
353 | | // powl is C99 but only added to FreeBSD in 2017. |
354 | 1.10M | else |
355 | 1.10M | r_prec /= powl(10.0, (long double) kp); |
356 | | # else |
357 | | else if (kp <= R_dec_min_exponent) |
358 | | r_prec = (r_prec * 1e+303)/Rexp10((double)(kp+303)); |
359 | | else |
360 | | r_prec /= Rexp10((double) kp); |
361 | | # endif |
362 | 10.8M | if (r_prec < tbl[R_print.digits - 1]) { |
363 | 11.0k | r_prec *= 10.0; |
364 | 11.0k | kp--; |
365 | 11.0k | } |
366 | | /* round alpha to integer, 10^(digits-1) <= alpha <= 10^digits |
367 | | accuracy limited by double rounding problem, |
368 | | alpha already rounded to 64 bits */ |
369 | 10.8M | alpha = (double) R_nearbyintl(r_prec); |
370 | | #else /* not using long doubles */ |
371 | | double r_prec = r; |
372 | | /* use exact scaling factor in double precision, if possible */ |
373 | | if (abs(kp) <= KP_MAX) { |
374 | | if (kp >= 0) r_prec /= tbl[kp]; else r_prec *= tbl[ -kp]; |
375 | | } |
376 | | /* For IEC60559 1e-308 is not representable except by gradual underflow. |
377 | | Shifting by 303 allows for any potential denormalized numbers x, |
378 | | and makes the reasonable assumption that R_dec_min_exponent+303 |
379 | | is in range. Representation of 1e+303 has low error. |
380 | | */ |
381 | | else if (kp <= R_dec_min_exponent) |
382 | | r_prec = (r_prec * 1e+303)/Rexp10((double)(kp+303)); |
383 | | else |
384 | | r_prec /= Rexp10((double)kp); |
385 | | if (r_prec < tbl[R_print.digits - 1]) { |
386 | | r_prec *= 10.0; |
387 | | kp--; |
388 | | } |
389 | | /* round alpha to integer, 10^(digits-1) <= alpha <= 10^digits */ |
390 | | /* accuracy limited by double rounding problem, |
391 | | alpha already rounded to 53 bits */ |
392 | | alpha = nearbyint(r_prec); |
393 | | #endif |
394 | 10.8M | *nsig = R_print.digits; |
395 | 88.4M | for (j = 1; j <= R_print.digits; j++) { |
396 | 88.4M | alpha /= 10.0; |
397 | 88.4M | if (alpha == floor(alpha)) { |
398 | 77.6M | (*nsig)--; |
399 | 77.6M | } else { |
400 | 10.7M | break; |
401 | 10.7M | } |
402 | 88.4M | } |
403 | 10.8M | if (*nsig == 0 && R_print.digits > 0) { |
404 | 10.1k | *nsig = 1; |
405 | 10.1k | kp += 1; |
406 | 10.1k | } |
407 | 10.8M | *kpower = kp + R_print.digits - 1; |
408 | | |
409 | | /* Scientific format may do more rounding than fixed format, e.g. |
410 | | 9996 with 3 digits is 1e+04 in scientific, but 9996 in fixed. |
411 | | This happens when the true value r is less than 10^(kpower+1) |
412 | | and would not round up to it in fixed format. |
413 | | Here rgt is the decimal place that will be cut off by rounding */ |
414 | | |
415 | 10.8M | int rgt = R_print.digits - *kpower; |
416 | | /* bound rgt by 0 and KP_MAX */ |
417 | 10.8M | rgt = rgt < 0 ? 0 : rgt > KP_MAX ? KP_MAX : rgt; |
418 | 10.8M | double fuzz = 0.5/(double)tbl[rgt]; |
419 | | // kpower can be bigger than the table. |
420 | 10.8M | *roundingwidens = *kpower > 0 && *kpower <= KP_MAX && r < tbl[*kpower] - fuzz; |
421 | 10.8M | } |
422 | 15.0M | } |
423 | | |
424 | | /* |
425 | | The return values are |
426 | | w : the required field width |
427 | | d : use %w.df in fixed format, %#w.de in scientific format |
428 | | e : use scientific format if != 0, value is number of exp digits - 1 |
429 | | |
430 | | nsmall specifies the minimum number of decimal digits in fixed format: |
431 | | it is 0 except when called from do_format. |
432 | | */ |
433 | | |
434 | | /* not hidden: used in graphics/src/plot.c */ |
435 | | void formatReal(const double *x, R_xlen_t n, int *w, int *d, int *e, int nsmall) |
436 | 15.6M | { |
437 | 15.6M | bool |
438 | 15.6M | naflag = false, nanflag = false, |
439 | 15.6M | posinf = false, neginf = false; |
440 | 15.6M | int neg = 0; |
441 | 15.6M | int mnl = INT_MAX, |
442 | 15.6M | mxl, rgt, mxsl, mxns; |
443 | 15.6M | mxl = rgt = mxsl = mxns = INT_MIN; |
444 | | |
445 | 31.3M | for (R_xlen_t i = 0; i < n; i++) { |
446 | 15.6M | if (!R_FINITE(x[i])) { |
447 | 625k | if(ISNA(x[i])) naflag = true; |
448 | 411k | else if(ISNAN(x[i])) nanflag = true; |
449 | 252k | else if(x[i] > 0) posinf = true; |
450 | 16.8k | else neginf = true; |
451 | 15.0M | } else { |
452 | 15.0M | int neg_i, kpower, nsig; |
453 | 15.0M | bool roundingwidens; |
454 | 15.0M | scientific(&x[i], &neg_i, &kpower, &nsig, &roundingwidens); |
455 | | |
456 | 15.0M | int left = kpower + 1; |
457 | 15.0M | if (roundingwidens) left--; |
458 | | |
459 | 15.0M | int sleft = neg_i + ((left <= 0) ? 1 : left); /* >= 1 */ |
460 | 15.0M | int right = nsig - left; /* #{digits} right of '.' ( > 0 often)*/ |
461 | 15.0M | if (neg_i) neg = 1; /* if any < 0, need extra space for sign */ |
462 | | |
463 | | /* Infinite precision "F" Format : */ |
464 | 15.0M | if (right > rgt) rgt = right; /* max digits to right of . */ |
465 | 15.0M | if (left > mxl) mxl = left; /* max digits to left of . */ |
466 | 15.0M | if (left < mnl) mnl = left; /* min digits to left of . */ |
467 | 15.0M | if (sleft > mxsl) mxsl = sleft; /* max left including sign(s)*/ |
468 | 15.0M | if (nsig > mxns) mxns = nsig; /* max sig digits */ |
469 | 15.0M | } |
470 | 15.6M | } |
471 | | /* F Format: use "F" format WHENEVER we use not more space than 'E' + scipen |
472 | | * and still satisfy 'R_print.digits' {but as if nsmall==0 !} |
473 | | * |
474 | | * E Format has the form [S]X[.XXX]E+XX[X] |
475 | | * |
476 | | * This is indicated by setting *e to non-zero (usually 1) |
477 | | * If the additional exponent digit is required *e is set to 2 |
478 | | */ |
479 | | |
480 | | /*-- These 'mxsl' & 'rgt' are used in F Format |
481 | | * AND in the ____ if(.) "F" else "E" ___ below: */ |
482 | 15.6M | if (R_print.digits == 0) rgt = 0; |
483 | 15.6M | if (mxl < 0) mxsl = 1 + neg; /* we use %#w.dg, so have leading zero */ |
484 | | |
485 | | /* use nsmall only *after* comparing "F" vs "E": */ |
486 | 15.6M | if (rgt < 0) rgt = 0;// rgt != 0 <==> needs decimal point (OutDec) "." |
487 | 15.6M | int wF = mxsl + rgt + (rgt != 0); /* width for F format */ |
488 | | |
489 | | /*-- 'see' how "E" Exponential format would be like : */ |
490 | 15.6M | *e = (mxl > 100 || mnl <= -99) ? 2 /* 3 digit exponent */ : 1; |
491 | 15.6M | if (mxns != INT_MIN) { |
492 | 14.8M | *d = mxns - 1; |
493 | 14.8M | *w = neg + (*d > 0) + *d + 4 + *e; /* width for E format */ |
494 | 14.8M | if (wF <= *w + R_print.scipen) { /* Fixpoint if it needs less space */ |
495 | 13.4M | *e = 0; |
496 | 13.4M | if (nsmall > rgt) { |
497 | 298 | rgt = nsmall; |
498 | 298 | wF = mxsl + rgt + (rgt != 0); |
499 | 298 | } |
500 | 13.4M | *d = rgt; |
501 | 13.4M | *w = wF; |
502 | 13.4M | } /* else : "E" Exponential format -- all done above */ |
503 | 14.8M | } |
504 | 826k | else { /* when all x[i] are non-finite */ |
505 | 826k | *w = 0;/* to be increased */ |
506 | 826k | *d = 0; |
507 | 826k | *e = 0; |
508 | 826k | } |
509 | 15.6M | if (naflag && *w < R_print.na_width) |
510 | 168k | *w = R_print.na_width; |
511 | 15.6M | if (nanflag && *w < 3) *w = 3; |
512 | 15.6M | if (posinf && *w < 3) *w = 3; |
513 | 15.6M | if (neginf && *w < 4) *w = 4; |
514 | 15.6M | } |
515 | | |
516 | | attribute_hidden |
517 | | void formatRealS(SEXP x, R_xlen_t n, int *w, int *d, int *e, int nsmall) |
518 | 2.76k | { |
519 | | /* |
520 | | * iterate by region and just take the most extreme |
521 | | * values across all the regions for final w, d, and e |
522 | | */ |
523 | 2.76k | int tmpw, tmpd, tmpe; |
524 | | |
525 | 2.76k | *w = 0; |
526 | 2.76k | *d = 0; |
527 | 2.76k | *e = 0; |
528 | | |
529 | 2.76k | ITERATE_BY_REGION_PARTIAL(x, px, idx, nb, double, REAL, 0, n, |
530 | 2.76k | { |
531 | 2.76k | formatReal(px, nb, &tmpw, &tmpd, &tmpe, nsmall); |
532 | 2.76k | if( tmpw > *w ) *w = tmpw; |
533 | 2.76k | if(!*d && tmpd) *d = tmpd; |
534 | 2.76k | if( tmpe > *e ) *e = tmpe; |
535 | 2.76k | }); |
536 | 2.76k | } |
537 | | |
538 | | #ifdef formatComplex_tricky |
539 | | #ifdef formatComplex_USING_signif |
540 | | /* From complex.c. */ |
541 | | void z_prec_r(Rcomplex *r, const Rcomplex *x, double digits); |
542 | | #endif |
543 | | #endif |
544 | | |
545 | | /* From R 2.2.0 to 4.3.z, the number of digits applied to real and imaginary parts |
546 | | together, not separately. Since R 4.4.0, Re(.) and Im(.) are treated separately. */ |
547 | | void formatComplex(const Rcomplex *x, R_xlen_t n, |
548 | | int *wr, int *dr, int *er, // (w,d,e) for Re(.) |
549 | | int *wi, int *di, int *ei, // (w,d,e) for Im(.) |
550 | | int nsmall) |
551 | 4.53M | { |
552 | | /* format.info() for x[1..n] for both Re & Im */ |
553 | | #ifdef formatComplex_tricky // R 4.3.z and earlier |
554 | | bool all_re_zero = true, all_im_zero = true, |
555 | | naflag = false, |
556 | | rnan = false, rposinf = false, rneginf = false, |
557 | | inan = false, iposinf = false; |
558 | | int neg = 0; |
559 | | int rt, mnl, mxl, mxsl, mxns, wF, i_wF; |
560 | | int i_rt, i_mnl, i_mxl, i_mxsl, i_mxns; |
561 | | rt = mxl = mxsl = mxns = INT_MIN; |
562 | | i_rt= i_mxl= i_mxsl= i_mxns= INT_MIN; |
563 | | i_mnl = mnl = INT_MAX; |
564 | | |
565 | | for (R_xlen_t i = 0; i < n; i++) { |
566 | | Rcomplex tmp; |
567 | | #ifdef formatComplex_USING_signif |
568 | | /* Now round */ |
569 | | z_prec_r(&tmp, &(x[i]), R_print.digits); |
570 | | #else |
571 | | tmp.r = x[i].r; |
572 | | tmp.i = x[i].i; |
573 | | #endif |
574 | | if(ISNA(tmp.r) || ISNA(tmp.i)) { |
575 | | naflag = true; |
576 | | } else { |
577 | | bool roundingwidens; |
578 | | int left, right, sleft, |
579 | | neg_i, kpower, nsig; |
580 | | |
581 | | /* real part */ |
582 | | |
583 | | if(!R_FINITE(tmp.r)) { |
584 | | if (ISNAN(tmp.r)) rnan = true; |
585 | | else if (tmp.r > 0) rposinf = true; |
586 | | else rneginf = true; |
587 | | } else { |
588 | | if(x[i].r != 0) all_re_zero = false; |
589 | | scientific(&(tmp.r), &neg_i, &kpower, &nsig, &roundingwidens); |
590 | | |
591 | | left = kpower + 1; |
592 | | if (roundingwidens) left--; |
593 | | sleft = neg_i + ((left <= 0) ? 1 : left); /* >= 1 */ |
594 | | right = nsig - left; /* #{digits} right of '.' ( > 0 often)*/ |
595 | | if (neg_i) neg = 1; /* if any < 0, need extra space for sign */ |
596 | | |
597 | | if (right > rt) rt = right; /* max digits to right of . */ |
598 | | if (left > mxl) mxl = left; /* max digits to left of . */ |
599 | | if (left < mnl) mnl = left; /* min digits to left of . */ |
600 | | if (sleft> mxsl) mxsl = sleft; /* max left including sign(s) */ |
601 | | if (nsig > mxns) mxns = nsig; /* max sig digits */ |
602 | | |
603 | | } |
604 | | /* imaginary part */ |
605 | | |
606 | | /* this is always unsigned */ |
607 | | /* we explicitly put the sign in when we print */ |
608 | | |
609 | | if(!R_FINITE(tmp.i)) { |
610 | | if (ISNAN(tmp.i)) inan = true; |
611 | | else iposinf = true; |
612 | | } else { |
613 | | if(x[i].i != 0) all_im_zero = false; |
614 | | scientific(&(tmp.i), &neg_i, &kpower, &nsig, &roundingwidens); |
615 | | |
616 | | left = kpower + 1; |
617 | | if (roundingwidens) left--; |
618 | | sleft = ((left <= 0) ? 1 : left); |
619 | | right = nsig - left; |
620 | | |
621 | | if (right > i_rt) i_rt = right; |
622 | | if (left > i_mxl) i_mxl = left; |
623 | | if (left < i_mnl) i_mnl = left; |
624 | | if (sleft> i_mxsl) i_mxsl = sleft; |
625 | | if (nsig > i_mxns) i_mxns = nsig; |
626 | | } |
627 | | /* done: ; */ |
628 | | } |
629 | | } |
630 | | |
631 | | /* see comments in formatReal() for details on this */ |
632 | | |
633 | | /* overall format for real part */ |
634 | | |
635 | | if (R_print.digits == 0) rt = 0; |
636 | | if (mxl != INT_MIN) { |
637 | | if (mxl < 0) mxsl = 1 + neg; |
638 | | if (rt < 0) rt = 0; |
639 | | wF = mxsl + rt + (rt != 0); |
640 | | |
641 | | *er = (mxl > 100 || mnl < -99) ? 2 : 1; |
642 | | *dr = mxns - 1; |
643 | | *wr = neg + (*dr > 0) + *dr + 4 + *er; |
644 | | } else { |
645 | | *er = 0; |
646 | | *wr = 0; |
647 | | *dr = 0; |
648 | | wF = 0; |
649 | | } |
650 | | |
651 | | /* overall format for imaginary part */ |
652 | | |
653 | | if (R_print.digits == 0) i_rt = 0; |
654 | | if (i_mxl != INT_MIN) { |
655 | | if (i_mxl < 0) i_mxsl = 1; |
656 | | if (i_rt < 0) i_rt = 0; |
657 | | i_wF = i_mxsl + i_rt + (i_rt != 0); |
658 | | |
659 | | *ei = (i_mxl > 100 || i_mnl < -99) ? 2 : 1; |
660 | | *di = i_mxns - 1; |
661 | | *wi = (*di > 0) + *di + 4 + *ei; |
662 | | } else { |
663 | | *ei = 0; |
664 | | *wi = 0; |
665 | | *di = 0; |
666 | | i_wF = 0; |
667 | | } |
668 | | |
669 | | /* Now make the fixed/scientific decision */ |
670 | | if(all_re_zero) { |
671 | | *er = *dr = 0; |
672 | | *wr = wF; |
673 | | if (i_wF <= *wi + R_print.scipen) { |
674 | | *ei = 0; |
675 | | if (nsmall > i_rt) {i_rt = nsmall; i_wF = i_mxsl + i_rt + (i_rt != 0);} |
676 | | *di = i_rt; |
677 | | *wi = i_wF; |
678 | | } |
679 | | } else if(all_im_zero) { |
680 | | if (wF <= *wr + R_print.scipen) { |
681 | | *er = 0; |
682 | | if (nsmall > rt) {rt = nsmall; wF = mxsl + rt + (rt != 0);} |
683 | | *dr = rt; |
684 | | *wr = wF; |
685 | | } |
686 | | *ei = *di = 0; |
687 | | *wi = i_wF; |
688 | | } else if(wF + i_wF < *wr + *wi + 2*R_print.scipen) { |
689 | | *er = 0; |
690 | | if (nsmall > rt) {rt = nsmall; wF = mxsl + rt + (rt != 0);} |
691 | | *dr = rt; |
692 | | *wr = wF; |
693 | | |
694 | | *ei = 0; |
695 | | if (nsmall > i_rt) { |
696 | | i_rt = nsmall; |
697 | | i_wF = i_mxsl + i_rt + (i_rt != 0); |
698 | | } |
699 | | *di = i_rt; |
700 | | *wi = i_wF; |
701 | | } /* else scientific for both */ |
702 | | if(*wr < 0) *wr = 0; |
703 | | if(*wi < 0) *wi = 0; |
704 | | |
705 | | /* Ensure space for Inf and NaN */ |
706 | | if (rnan && *wr < 3) *wr = 3; |
707 | | if (rposinf && *wr < 3) *wr = 3; |
708 | | if (rneginf && *wr < 4) *wr = 4; |
709 | | if (inan && *wi < 3) *wi = 3; |
710 | | if (iposinf && *wi < 3) *wi = 3; |
711 | | |
712 | | #else // R >= 4.4.0 : no longer "tricky" |
713 | 4.53M | double |
714 | 4.53M | *Re = (double *) R_alloc(n, sizeof(double)), |
715 | 4.53M | *Im = (double *) R_alloc(n, sizeof(double)); |
716 | | |
717 | 4.53M | # ifdef formatComplex_NA_give_NA // as previously in all S and R versions: |
718 | 4.53M | bool naflag = false; |
719 | 4.53M | R_xlen_t i1 = 0; |
720 | 9.08M | for (R_xlen_t i = 0; i < n; i++) { |
721 | 4.54M | if(ISNA(x[i].r) || ISNA(x[i].i)) { |
722 | 133k | naflag |= true; |
723 | 4.41M | } else { |
724 | 4.41M | Re[i1] = x[i].r; |
725 | 4.41M | Im[i1] = fabs(x[i].i); // in "Re +/- Im", the '-' does not take more space |
726 | 4.41M | i1++; |
727 | 4.41M | } // 0 <= i1 <= i < n; i1 == length(Re) == length(Im) |
728 | 4.54M | } |
729 | 4.53M | formatReal(Re, i1, wr, dr, er, nsmall); |
730 | 4.53M | formatReal(Im, i1, wi, di, ei, nsmall); |
731 | | |
732 | | /* finally, ensure that there is space for NA */ |
733 | 4.53M | if (naflag && *wr+*wi+2 < R_print.na_width) |
734 | 0 | *wr += (R_print.na_width -(*wr + *wi + 2)); |
735 | | |
736 | | # else // even simpler: can lead to extra " " e.g. for c(NA, 1+2i) |
737 | | for (R_xlen_t i = 0; i < n; i++) { |
738 | | Re[i] = x[i].r; |
739 | | Im[i] = fabs(x[i].i); // in "Re +/- Im", the '-' does not take more space |
740 | | } |
741 | | formatReal(Re, n, wr, dr, er, nsmall); |
742 | | formatReal(Im, n, wi, di, ei, nsmall); |
743 | | # endif |
744 | 4.53M | #endif |
745 | 4.53M | } |
746 | | |
747 | | |
748 | | |
749 | | attribute_hidden |
750 | | void formatComplexS(SEXP x, R_xlen_t n, int *wr, int *dr, int *er, |
751 | | int *wi, int *di, int *ei, int nsmall) |
752 | 188 | { |
753 | | /* format.info() for x[1..n] for both Re & Im */ |
754 | 188 | int tmpwr, tmpdr, tmper, tmpwi, tmpdi, tmpei; |
755 | | |
756 | 188 | *wr = 0; |
757 | 188 | *wi = 0; |
758 | 188 | *dr = 0; |
759 | 188 | *di = 0; |
760 | 188 | *er = 0; |
761 | 188 | *ei = 0; |
762 | | ITERATE_BY_REGION_PARTIAL(x, px, idx, nb, Rcomplex, COMPLEX, 0, n, |
763 | 188 | { |
764 | 188 | formatComplex(px, nb, &tmpwr, &tmpdr, &tmper, |
765 | 188 | &tmpwi, &tmpdi, &tmpei, nsmall); |
766 | 188 | if(tmpwr > *wr) *wr = tmpwr; |
767 | 188 | if(tmpdr && !*dr) *dr = tmpdr; |
768 | 188 | if(tmper > *er) *er = tmper; |
769 | 188 | if(tmpwi > *wi) *wi = tmpwi; |
770 | 188 | if(tmpdi && !*di) *di = tmpdi; |
771 | 188 | if(tmpei > *ei) *ei = tmpei; |
772 | 188 | }); |
773 | 188 | } |