1
|
1 // pr-output.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
127
|
30 #include <stdio.h> |
1
|
31 #include <string.h> |
|
32 #include <math.h> |
|
33 #include <float.h> |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "Range.h" |
|
37 #include "Matrix.h" |
|
38 |
|
39 #include "tree-const.h" |
|
40 #include "variables.h" |
|
41 #include "user-prefs.h" |
|
42 #include "pr-output.h" |
|
43 #include "mappers.h" |
|
44 #include "pager.h" |
|
45 #include "error.h" |
|
46 #include "utils.h" |
|
47 |
|
48 // Current format string for real numbers and the real part of complex |
|
49 // numbers. |
|
50 static char *curr_real_fmt = (char *) NULL; |
|
51 |
|
52 // Current format string for the imaginary part of complex numbers. |
|
53 static char *curr_imag_fmt = (char *) NULL; |
|
54 |
|
55 // Nonzero means don\'t do any fancy formatting. |
|
56 static int free_format = 0; |
|
57 |
|
58 // Nonzero means print plus sign for nonzero, blank for zero. |
|
59 static int plus_format = 0; |
|
60 |
|
61 // Nonzero means always print like dollars and cents. |
|
62 static int bank_format = 0; |
|
63 |
|
64 // Nonzero means use an e format. |
|
65 static int print_e = 0; |
|
66 |
|
67 // Nonzero means print E instead of e for exponent field. |
|
68 static int print_big_e = 0; |
|
69 |
|
70 static int |
|
71 any_element_is_negative (const Matrix& a) |
|
72 { |
|
73 int nr = a.rows (); |
|
74 int nc = a.columns (); |
|
75 for (int j = 0; j < nc; j++) |
|
76 for (int i = 0; i < nr; i++) |
|
77 if (a.elem (i, j) < 0.0) |
|
78 return 1; |
|
79 return 0; |
|
80 } |
|
81 |
|
82 static int |
|
83 any_element_is_inf_or_nan (const Matrix& a) |
|
84 { |
|
85 int nr = a.rows (); |
|
86 int nc = a.columns (); |
|
87 for (int j = 0; j < nc; j++) |
|
88 for (int i = 0; i < nr; i++) |
|
89 { |
|
90 double val = a.elem (i, j); |
|
91 if (xisinf (val) || xisnan (val)) |
|
92 return 1; |
|
93 } |
|
94 return 0; |
|
95 } |
|
96 |
|
97 static int |
|
98 any_element_is_inf_or_nan (const ComplexMatrix& a) |
|
99 { |
|
100 int nr = a.rows (); |
|
101 int nc = a.columns (); |
|
102 for (int j = 0; j < nc; j++) |
|
103 for (int i = 0; i < nr; i++) |
|
104 { |
|
105 Complex val = a.elem (i, j); |
|
106 if (xisinf (val) || xisnan (val)) |
|
107 return 1; |
|
108 } |
|
109 return 0; |
|
110 } |
|
111 |
|
112 static int |
|
113 all_elements_are_int_or_inf_or_nan (const Matrix& a) |
|
114 { |
|
115 int nr = a.rows (); |
|
116 int nc = a.columns (); |
|
117 for (int j = 0; j < nc; j++) |
|
118 for (int i = 0; i < nr; i++) |
|
119 { |
|
120 double val = a.elem (i, j); |
|
121 if (xisnan (val) || D_NINT (val) == val) |
|
122 continue; |
|
123 else |
|
124 return 0; |
|
125 } |
|
126 return 1; |
|
127 } |
|
128 |
|
129 static Matrix |
|
130 abs (const Matrix& a) |
|
131 { |
|
132 int nr = a.rows (); |
|
133 int nc = a.columns (); |
|
134 Matrix retval (nr, nc); |
|
135 for (int j = 0; j < nc; j++) |
|
136 for (int i = 0; i < nr; i++) |
|
137 retval.elem (i, j) = fabs (a.elem (i, j)); |
|
138 return retval; |
|
139 } |
|
140 |
|
141 static double |
164
|
142 pr_max_internal (const Matrix& m) |
1
|
143 { |
|
144 int nr = m.rows (); |
|
145 int nc = m.columns (); |
|
146 |
|
147 double result = DBL_MIN; |
|
148 |
|
149 for (int j = 0; j < nc; j++) |
|
150 for (int i = 0; i < nr; i++) |
|
151 { |
|
152 double val = m.elem (i, j); |
|
153 if (xisinf (val) || xisnan (val)) |
|
154 continue; |
|
155 |
|
156 if (val > result) |
|
157 result = val; |
|
158 } |
|
159 return result; |
|
160 } |
|
161 |
|
162 static double |
164
|
163 pr_min_internal (const Matrix& m) |
1
|
164 { |
|
165 int nr = m.rows (); |
|
166 int nc = m.columns (); |
|
167 |
|
168 double result = DBL_MAX; |
|
169 |
|
170 for (int j = 0; j < nc; j++) |
|
171 for (int i = 0; i < nr; i++) |
|
172 { |
|
173 double val = m.elem (i, j); |
|
174 if (xisinf (val) || xisnan (val)) |
|
175 continue; |
|
176 |
|
177 if (val < result) |
|
178 result = val; |
|
179 } |
|
180 return result; |
|
181 } |
|
182 |
|
183 static void |
|
184 set_format (double d, int& fw) |
|
185 { |
|
186 curr_real_fmt = (char *) NULL; |
|
187 curr_imag_fmt = (char *) NULL; |
|
188 |
|
189 if (free_format) |
|
190 return; |
|
191 |
|
192 static char fmt_buf[32]; |
|
193 |
|
194 int sign = (d < 0.0); |
|
195 |
|
196 int inf_or_nan = (xisinf (d) || xisnan (d)); |
|
197 |
|
198 double d_abs = d < 0.0 ? -d : d; |
|
199 |
|
200 int digits = d_abs == 0.0 ? 0 : (int) floor (log10 (d_abs) + 1.0); |
|
201 |
|
202 int prec = user_pref.output_precision; |
|
203 |
|
204 int ld, rd; |
|
205 |
|
206 if (bank_format) |
|
207 { |
|
208 fw = digits < 0 ? 4 : digits + 3; |
|
209 if (inf_or_nan && fw < 3) |
|
210 fw = 3; |
|
211 fw += sign; |
|
212 rd = 2; |
|
213 } |
|
214 else if (xisnan (d) || D_NINT (d) == d) |
|
215 { |
|
216 fw = digits; |
|
217 if (inf_or_nan && fw < 3) |
|
218 fw = 3; |
|
219 fw += sign; |
|
220 rd = 0; |
|
221 } |
|
222 else |
|
223 { |
|
224 if (digits > 0) |
|
225 { |
|
226 ld = digits; |
|
227 rd = prec - digits; |
|
228 digits++; |
|
229 } |
|
230 else |
|
231 { |
|
232 ld = 1; |
|
233 rd = prec - digits; |
|
234 digits = -digits + 1; |
|
235 } |
|
236 |
|
237 fw = ld + 1 + rd; |
|
238 if (inf_or_nan && fw < 3) |
|
239 fw = 3; |
|
240 fw += sign; |
|
241 } |
|
242 |
|
243 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
244 { |
|
245 int exp_field = 4; |
|
246 if (digits > 100) |
|
247 exp_field++; |
|
248 |
|
249 fw = 2 + prec + exp_field; |
|
250 if (inf_or_nan && fw < 3) |
|
251 fw = 3; |
|
252 fw += sign; |
|
253 |
|
254 if (print_big_e) |
|
255 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
256 else |
|
257 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
258 } |
|
259 else |
|
260 { |
|
261 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
262 } |
|
263 |
|
264 curr_real_fmt = &fmt_buf[0]; |
|
265 } |
|
266 |
|
267 static inline void |
|
268 set_format (double d) |
|
269 { |
|
270 int fw; |
|
271 set_format (d, fw); |
|
272 } |
|
273 |
|
274 static void |
164
|
275 set_format (const Matrix& m, int& fw) |
1
|
276 { |
|
277 curr_real_fmt = (char *) NULL; |
|
278 curr_imag_fmt = (char *) NULL; |
|
279 |
|
280 if (free_format) |
|
281 return; |
|
282 |
|
283 static char fmt_buf[32]; |
|
284 |
|
285 int sign = any_element_is_negative (m); |
|
286 |
|
287 int inf_or_nan = any_element_is_inf_or_nan (m); |
|
288 |
|
289 Matrix m_abs = abs (m); |
|
290 double max_abs = pr_max_internal (m_abs); |
|
291 double min_abs = pr_min_internal (m_abs); |
|
292 |
|
293 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
294 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
295 |
|
296 int prec = user_pref.output_precision; |
|
297 |
|
298 int ld, rd; |
|
299 |
|
300 if (bank_format) |
|
301 { |
|
302 int digits = x_max > x_min ? x_max : x_min; |
|
303 fw = digits <= 0 ? 4 : digits + 3; |
|
304 if (inf_or_nan && fw < 3) |
|
305 fw = 3; |
|
306 fw += sign; |
|
307 rd = 2; |
|
308 } |
|
309 else if (all_elements_are_int_or_inf_or_nan (m)) |
|
310 { |
|
311 int digits = x_max > x_min ? x_max : x_min; |
|
312 fw = digits <= 0 ? 1 : digits; |
|
313 if (inf_or_nan && fw < 3) |
|
314 fw = 3; |
|
315 fw += sign; |
|
316 rd = 0; |
|
317 } |
|
318 else |
|
319 { |
|
320 int ld_max, rd_max; |
|
321 if (x_max > 0) |
|
322 { |
|
323 ld_max = x_max; |
|
324 rd_max = prec - x_max; |
|
325 x_max++; |
|
326 } |
|
327 else |
|
328 { |
|
329 ld_max = 1; |
|
330 rd_max = prec - x_max; |
|
331 x_max = -x_max + 1; |
|
332 } |
|
333 |
|
334 int ld_min, rd_min; |
|
335 if (x_min > 0) |
|
336 { |
|
337 ld_min = x_min; |
|
338 rd_min = prec - x_min; |
|
339 x_min++; |
|
340 } |
|
341 else |
|
342 { |
|
343 ld_min = 1; |
|
344 rd_min = prec - x_min; |
|
345 x_min = -x_min + 1; |
|
346 } |
|
347 |
|
348 ld = ld_max > ld_min ? ld_max : ld_min; |
|
349 rd = rd_max > rd_min ? rd_max : rd_min; |
|
350 |
|
351 fw = ld + 1 + rd; |
|
352 if (inf_or_nan && fw < 3) |
|
353 fw = 3; |
|
354 fw += sign; |
|
355 } |
|
356 |
|
357 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
358 { |
|
359 int exp_field = 4; |
|
360 if (x_max > 100 || x_min > 100) |
|
361 exp_field++; |
|
362 |
|
363 fw = 2 + prec + exp_field; |
|
364 if (inf_or_nan && fw < 3) |
|
365 fw = 3; |
|
366 fw += sign; |
|
367 |
|
368 if (print_big_e) |
|
369 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
370 else |
|
371 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
372 } |
|
373 else |
|
374 { |
|
375 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
376 } |
|
377 |
|
378 curr_real_fmt = &fmt_buf[0]; |
|
379 } |
|
380 |
|
381 static inline void |
164
|
382 set_format (const Matrix& m) |
1
|
383 { |
|
384 int fw; |
|
385 set_format (m, fw); |
|
386 } |
|
387 |
|
388 static void |
164
|
389 set_format (const Complex& c, int& r_fw, int& i_fw) |
1
|
390 { |
|
391 curr_real_fmt = (char *) NULL; |
|
392 curr_imag_fmt = (char *) NULL; |
|
393 |
|
394 if (free_format) |
|
395 return; |
|
396 |
|
397 static char r_fmt_buf[32]; |
|
398 static char i_fmt_buf[32]; |
|
399 |
|
400 double rp = c.real (); |
|
401 double ip = c.imag (); |
|
402 |
|
403 int sign = (rp < 0.0); |
|
404 |
|
405 int inf_or_nan = (xisinf (c) || xisnan (c)); |
|
406 |
|
407 double r_abs = rp < 0.0 ? -rp : rp; |
|
408 double i_abs = ip < 0.0 ? -ip : ip; |
|
409 |
|
410 int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0); |
|
411 int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0); |
|
412 |
|
413 int x_max, x_min; |
|
414 |
|
415 if (r_x > i_x) |
|
416 { |
|
417 x_max = r_x; |
|
418 x_min = i_x; |
|
419 } |
|
420 else |
|
421 { |
|
422 x_max = i_x; |
|
423 x_min = r_x; |
|
424 } |
|
425 |
|
426 int prec = user_pref.output_precision; |
|
427 |
|
428 int ld, rd; |
|
429 |
|
430 if (bank_format) |
|
431 { |
|
432 int digits = r_x; |
|
433 i_fw = 0; |
|
434 r_fw = digits <= 0 ? 4 : digits + 3; |
|
435 if (inf_or_nan && r_fw < 3) |
|
436 r_fw = 3; |
|
437 r_fw += sign; |
|
438 rd = 2; |
|
439 } |
|
440 else if (inf_or_nan || (D_NINT (rp) == rp && D_NINT (ip) == ip)) |
|
441 { |
|
442 int digits = x_max > x_min ? x_max : x_min; |
|
443 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
444 if (inf_or_nan && i_fw < 3) |
|
445 i_fw = r_fw = 3; |
|
446 r_fw += sign; |
|
447 rd = 0; |
|
448 } |
|
449 else |
|
450 { |
|
451 int ld_max, rd_max; |
|
452 if (x_max > 0) |
|
453 { |
|
454 ld_max = x_max; |
|
455 rd_max = prec - x_max; |
|
456 x_max++; |
|
457 } |
|
458 else |
|
459 { |
|
460 ld_max = 1; |
|
461 rd_max = prec - x_max; |
|
462 x_max = -x_max + 1; |
|
463 } |
|
464 |
|
465 int ld_min, rd_min; |
|
466 if (x_min > 0) |
|
467 { |
|
468 ld_min = x_min; |
|
469 rd_min = prec - x_min; |
|
470 x_min++; |
|
471 } |
|
472 else |
|
473 { |
|
474 ld_min = 1; |
|
475 rd_min = prec - x_min; |
|
476 x_min = -x_min + 1; |
|
477 } |
|
478 |
|
479 ld = ld_max > ld_min ? ld_max : ld_min; |
|
480 rd = rd_max > rd_min ? rd_max : rd_min; |
|
481 |
|
482 i_fw = r_fw = ld + 1 + rd; |
|
483 if (inf_or_nan && i_fw < 3) |
|
484 i_fw = r_fw = 3; |
|
485 r_fw += sign; |
|
486 } |
|
487 |
|
488 if (! bank_format && (r_fw > user_pref.output_max_field_width || print_e)) |
|
489 { |
|
490 int exp_field = 4; |
|
491 if (x_max > 100 || x_min > 100) |
|
492 exp_field++; |
|
493 |
|
494 i_fw = r_fw = 1 + prec + exp_field; |
|
495 if (inf_or_nan && i_fw < 3) |
|
496 i_fw = r_fw = 3; |
|
497 r_fw += sign; |
|
498 |
|
499 if (print_big_e) |
|
500 { |
|
501 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
502 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
503 } |
|
504 else |
|
505 { |
|
506 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
507 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
508 } |
|
509 } |
|
510 else |
|
511 { |
|
512 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
513 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
514 } |
|
515 |
|
516 curr_real_fmt = &r_fmt_buf[0]; |
|
517 curr_imag_fmt = &i_fmt_buf[0]; |
|
518 } |
|
519 |
|
520 static inline void |
164
|
521 set_format (const Complex& c) |
1
|
522 { |
|
523 int r_fw, i_fw; |
|
524 set_format (c, r_fw, i_fw); |
|
525 } |
|
526 |
|
527 static void |
164
|
528 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw) |
1
|
529 { |
|
530 curr_real_fmt = (char *) NULL; |
|
531 curr_imag_fmt = (char *) NULL; |
|
532 |
|
533 if (free_format) |
|
534 return; |
|
535 |
|
536 static char r_fmt_buf[32]; |
|
537 static char i_fmt_buf[32]; |
|
538 |
|
539 Matrix rp = real (cm); |
|
540 Matrix ip = imag (cm); |
|
541 |
|
542 int sign = any_element_is_negative (rp); |
|
543 |
|
544 int inf_or_nan = any_element_is_inf_or_nan (cm); |
|
545 |
|
546 Matrix r_m_abs = abs (rp); |
|
547 double r_max_abs = pr_max_internal (r_m_abs); |
|
548 double r_min_abs = pr_min_internal (r_m_abs); |
|
549 |
|
550 Matrix i_m_abs = abs (ip); |
|
551 double i_max_abs = pr_max_internal (i_m_abs); |
|
552 double i_min_abs = pr_min_internal (i_m_abs); |
|
553 |
|
554 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
555 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
556 |
|
557 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
558 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
559 |
|
560 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
561 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
562 |
|
563 int prec = user_pref.output_precision; |
|
564 |
|
565 int ld, rd; |
|
566 |
|
567 if (bank_format) |
|
568 { |
|
569 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
570 i_fw = 0; |
|
571 r_fw = digits <= 0 ? 4 : digits + 3; |
|
572 if (inf_or_nan && i_fw < 3) |
|
573 i_fw = r_fw = 3; |
|
574 r_fw += sign; |
|
575 rd = 2; |
|
576 } |
|
577 else if (all_elements_are_int_or_inf_or_nan (rp) |
|
578 && all_elements_are_int_or_inf_or_nan (ip)) |
|
579 { |
|
580 int digits = x_max > x_min ? x_max : x_min; |
|
581 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
582 if (inf_or_nan && i_fw < 3) |
|
583 i_fw = r_fw = 3; |
|
584 r_fw += sign; |
|
585 rd = 0; |
|
586 } |
|
587 else |
|
588 { |
|
589 int ld_max, rd_max; |
|
590 if (x_max > 0) |
|
591 { |
|
592 ld_max = x_max; |
|
593 rd_max = prec - x_max; |
|
594 x_max++; |
|
595 } |
|
596 else |
|
597 { |
|
598 ld_max = 1; |
|
599 rd_max = prec - x_max; |
|
600 x_max = -x_max + 1; |
|
601 } |
|
602 |
|
603 int ld_min, rd_min; |
|
604 if (x_min > 0) |
|
605 { |
|
606 ld_min = x_min; |
|
607 rd_min = prec - x_min; |
|
608 x_min++; |
|
609 } |
|
610 else |
|
611 { |
|
612 ld_min = 1; |
|
613 rd_min = prec - x_min; |
|
614 x_min = -x_min + 1; |
|
615 } |
|
616 |
|
617 ld = ld_max > ld_min ? ld_max : ld_min; |
|
618 rd = rd_max > rd_min ? rd_max : rd_min; |
|
619 |
|
620 i_fw = r_fw = ld + 1 + rd; |
|
621 if (inf_or_nan && i_fw < 3) |
|
622 i_fw = r_fw = 3; |
|
623 r_fw += sign; |
|
624 } |
|
625 |
|
626 if (! bank_format && (r_fw > user_pref.output_max_field_width || print_e)) |
|
627 { |
|
628 int exp_field = 4; |
|
629 if (x_max > 100 || x_min > 100) |
|
630 exp_field++; |
|
631 |
|
632 i_fw = r_fw = 1 + prec + exp_field; |
|
633 if (inf_or_nan && i_fw < 3) |
|
634 i_fw = r_fw = 3; |
|
635 r_fw += sign; |
|
636 |
|
637 if (print_big_e) |
|
638 { |
|
639 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
640 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
641 } |
|
642 else |
|
643 { |
|
644 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
645 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
646 } |
|
647 } |
|
648 else |
|
649 { |
|
650 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
651 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
652 } |
|
653 |
|
654 curr_real_fmt = &r_fmt_buf[0]; |
|
655 curr_imag_fmt = &i_fmt_buf[0]; |
|
656 } |
|
657 |
|
658 static int |
164
|
659 all_elements_are_ints (const Range& r) |
1
|
660 { |
|
661 // If the base and increment are ints, the final value in the range |
|
662 // will also be an integer, even if the limit is not. |
|
663 |
|
664 double b = r.base (); |
|
665 double i = r.inc (); |
|
666 |
|
667 return ((double) NINT (b) == b && (double) NINT (i) == i); |
|
668 } |
|
669 |
|
670 static inline void |
164
|
671 set_format (const ComplexMatrix& cm) |
1
|
672 { |
|
673 int r_fw, i_fw; |
|
674 set_format (cm, r_fw, i_fw); |
|
675 } |
|
676 |
|
677 static void |
164
|
678 set_format (const Range& r, int& fw) |
1
|
679 { |
|
680 curr_real_fmt = (char *) NULL; |
|
681 curr_imag_fmt = (char *) NULL; |
|
682 |
|
683 if (free_format) |
|
684 return; |
|
685 |
|
686 static char fmt_buf[32]; |
|
687 |
|
688 double r_min = r.base (); |
|
689 double r_max = r.limit (); |
|
690 |
|
691 if (r_max < r_min) |
|
692 { |
|
693 double tmp = r_max; |
|
694 r_max = r_min; |
|
695 r_min = tmp; |
|
696 } |
|
697 |
|
698 int sign = (r_min < 0.0); |
|
699 |
|
700 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
701 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
702 |
|
703 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
704 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
705 |
|
706 int prec = user_pref.output_precision; |
|
707 |
|
708 int ld, rd; |
|
709 |
|
710 if (bank_format) |
|
711 { |
|
712 int digits = x_max > x_min ? x_max : x_min; |
|
713 fw = sign + digits < 0 ? 4 : digits + 3; |
|
714 rd = 2; |
|
715 } |
|
716 else if (all_elements_are_ints (r)) |
|
717 { |
|
718 int digits = x_max > x_min ? x_max : x_min; |
|
719 fw = sign + digits; |
|
720 rd = 0; |
|
721 } |
|
722 else |
|
723 { |
|
724 int ld_max, rd_max; |
|
725 if (x_max > 0) |
|
726 { |
|
727 ld_max = x_max; |
|
728 rd_max = prec - x_max; |
|
729 x_max++; |
|
730 } |
|
731 else |
|
732 { |
|
733 ld_max = 1; |
|
734 rd_max = prec - x_max; |
|
735 x_max = -x_max + 1; |
|
736 } |
|
737 |
|
738 int ld_min, rd_min; |
|
739 if (x_min > 0) |
|
740 { |
|
741 ld_min = x_min; |
|
742 rd_min = prec - x_min; |
|
743 x_min++; |
|
744 } |
|
745 else |
|
746 { |
|
747 ld_min = 1; |
|
748 rd_min = prec - x_min; |
|
749 x_min = -x_min + 1; |
|
750 } |
|
751 |
|
752 ld = ld_max > ld_min ? ld_max : ld_min; |
|
753 rd = rd_max > rd_min ? rd_max : rd_min; |
|
754 |
|
755 fw = sign + ld + 1 + rd; |
|
756 } |
|
757 |
|
758 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
759 { |
|
760 int exp_field = 4; |
|
761 if (x_max > 100 || x_min > 100) |
|
762 exp_field++; |
|
763 |
|
764 fw = sign + 2 + prec + exp_field; |
|
765 |
|
766 if (print_big_e) |
|
767 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
768 else |
|
769 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
770 } |
|
771 else |
|
772 { |
|
773 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
774 } |
|
775 |
|
776 curr_real_fmt = &fmt_buf[0]; |
|
777 } |
|
778 |
|
779 static inline void |
164
|
780 set_format (const Range& r) |
1
|
781 { |
|
782 int fw; |
|
783 set_format (r, fw); |
|
784 } |
|
785 |
|
786 static inline void |
164
|
787 pr_any_float (const char *fmt, ostrstream& os, double d, int fw = 0) |
1
|
788 { |
|
789 if (d == -0.0) |
|
790 d = 0.0; |
|
791 |
|
792 if (fmt == (char *) NULL) |
|
793 os << d; |
|
794 else |
|
795 { |
|
796 if (xisinf (d)) |
|
797 { |
|
798 char *s; |
|
799 if (d < 0.0) |
|
800 s = "-Inf"; |
|
801 else |
|
802 s = "Inf"; |
|
803 |
|
804 if (fw > 0) |
|
805 os.form ("%*s", fw, s); |
|
806 else |
|
807 os << s; |
|
808 } |
|
809 else if (xisnan (d)) |
|
810 { |
|
811 if (fw > 0) |
|
812 os.form ("%*s", fw, "NaN"); |
|
813 else |
|
814 os << "NaN"; |
|
815 } |
|
816 else |
|
817 os.form (fmt, d); |
|
818 } |
|
819 } |
|
820 |
|
821 static inline void |
|
822 pr_float (ostrstream& os, double d, int fw = 0) |
|
823 { |
|
824 pr_any_float (curr_real_fmt, os, d, fw); |
|
825 } |
|
826 |
|
827 static inline void |
|
828 pr_imag_float (ostrstream& os, double d, int fw = 0) |
|
829 { |
|
830 pr_any_float (curr_imag_fmt, os, d, fw); |
|
831 } |
|
832 |
|
833 static inline void |
164
|
834 pr_complex (ostrstream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
835 { |
|
836 double r = c.real (); |
|
837 pr_float (os, r, r_fw); |
|
838 if (! bank_format) |
|
839 { |
|
840 double i = c.imag (); |
|
841 if (i < 0) |
|
842 { |
|
843 os << " - "; |
|
844 i = -i; |
|
845 pr_imag_float (os, i, i_fw); |
|
846 } |
|
847 else |
|
848 { |
|
849 os << " + "; |
|
850 pr_imag_float (os, i, i_fw); |
|
851 } |
|
852 os << "i"; |
|
853 } |
|
854 } |
|
855 |
|
856 void |
|
857 octave_print_internal (ostrstream& os, double d) |
|
858 { |
|
859 if (plus_format) |
|
860 { |
|
861 if (d == 0.0) |
|
862 os << " "; |
|
863 else |
|
864 os << "+"; |
|
865 } |
|
866 else |
|
867 { |
|
868 set_format (d); |
|
869 if (free_format) |
|
870 os << d; |
|
871 else |
|
872 pr_float (os, d); |
|
873 } |
|
874 os << "\n"; |
|
875 } |
|
876 |
|
877 void |
164
|
878 octave_print_internal (ostrstream& os, const Matrix& m) |
1
|
879 { |
|
880 int nr = m.rows (); |
|
881 int nc = m.columns (); |
|
882 |
|
883 if (plus_format) |
|
884 { |
|
885 for (int i = 0; i < nr; i++) |
|
886 { |
|
887 for (int j = 0; j < nc; j++) |
|
888 { |
|
889 if (j == 0) |
|
890 os << " "; |
|
891 |
|
892 if (m.elem (i, j) == 0.0) |
|
893 os << " "; |
|
894 else |
|
895 os << "+"; |
|
896 } |
|
897 os << "\n"; |
|
898 } |
|
899 } |
|
900 else |
|
901 { |
|
902 int fw; |
|
903 set_format (m, fw); |
|
904 int column_width = fw + 2; |
|
905 int total_width = nc * column_width; |
|
906 int max_width = terminal_columns (); |
|
907 |
|
908 if (free_format) |
|
909 { |
|
910 os << m; |
|
911 return; |
|
912 } |
|
913 |
|
914 int inc = nc; |
|
915 if (total_width > max_width && user_pref.split_long_rows) |
|
916 { |
|
917 inc = max_width / column_width; |
|
918 if (inc == 0) |
|
919 inc++; |
|
920 } |
|
921 |
|
922 int col = 0; |
|
923 while (col < nc) |
|
924 { |
|
925 int lim = col + inc < nc ? col + inc : nc; |
|
926 |
|
927 if (total_width > max_width && user_pref.split_long_rows) |
|
928 { |
|
929 if (col != 0) |
|
930 os << "\n"; |
|
931 |
|
932 int num_cols = lim - col; |
|
933 if (num_cols == 1) |
|
934 os << " Column " << col + 1 << ":\n\n"; |
|
935 else if (num_cols == 2) |
|
936 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
937 else |
|
938 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
939 } |
|
940 |
|
941 for (int i = 0; i < nr; i++) |
|
942 { |
|
943 for (int j = col; j < lim; j++) |
|
944 { |
|
945 os << " "; |
|
946 pr_float(os, m.elem (i, j), fw); |
|
947 } |
|
948 os << "\n"; |
|
949 } |
|
950 col += inc; |
|
951 } |
|
952 } |
|
953 } |
|
954 |
|
955 void |
164
|
956 octave_print_internal (ostrstream& os, const Complex& c) |
1
|
957 { |
|
958 if (plus_format) |
|
959 { |
|
960 if (c == 0.0) |
|
961 os << " "; |
|
962 else |
|
963 os << "+"; |
|
964 } |
|
965 else |
|
966 { |
|
967 set_format (c); |
|
968 if (free_format) |
|
969 os << c; |
|
970 else |
|
971 pr_complex (os, c); |
|
972 } |
|
973 os << "\n"; |
|
974 } |
|
975 |
|
976 void |
164
|
977 octave_print_internal (ostrstream& os, const ComplexMatrix& cm) |
1
|
978 { |
|
979 int nr = cm.rows (); |
|
980 int nc = cm.columns (); |
|
981 |
|
982 if (plus_format) |
|
983 { |
|
984 for (int i = 0; i < nr; i++) |
|
985 { |
|
986 for (int j = 0; j < nc; j++) |
|
987 { |
|
988 if (j == 0) |
|
989 os << " "; |
|
990 |
|
991 if (cm.elem (i, j) == 0.0) |
|
992 os << " "; |
|
993 else |
|
994 os << "+"; |
|
995 } |
|
996 os << "\n"; |
|
997 } |
|
998 } |
|
999 else |
|
1000 { |
|
1001 int r_fw, i_fw; |
|
1002 set_format (cm, r_fw, i_fw); |
|
1003 int column_width = i_fw + r_fw; |
|
1004 column_width += bank_format ? 2 : 7; |
|
1005 int total_width = nc * column_width; |
|
1006 int max_width = terminal_columns (); |
|
1007 |
|
1008 if (free_format) |
|
1009 { |
|
1010 os << cm; |
|
1011 return; |
|
1012 } |
|
1013 |
|
1014 int inc = nc; |
|
1015 if (total_width > max_width && user_pref.split_long_rows) |
|
1016 { |
|
1017 inc = max_width / column_width; |
|
1018 if (inc == 0) |
|
1019 inc++; |
|
1020 } |
|
1021 |
|
1022 int col = 0; |
|
1023 while (col < nc) |
|
1024 { |
|
1025 int lim = col + inc < nc ? col + inc : nc; |
|
1026 |
|
1027 if (total_width > max_width && user_pref.split_long_rows) |
|
1028 { |
|
1029 if (col != 0) |
|
1030 os << "\n"; |
|
1031 |
|
1032 int num_cols = lim - col; |
|
1033 if (num_cols == 1) |
|
1034 os << " Column " << col + 1 << ":\n\n"; |
|
1035 else if (num_cols == 2) |
|
1036 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
1037 else |
|
1038 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
1039 } |
|
1040 |
|
1041 for (int i = 0; i < nr; i++) |
|
1042 { |
|
1043 for (int j = col; j < lim; j++) |
|
1044 { |
|
1045 if (bank_format) |
|
1046 os << " "; |
|
1047 else |
|
1048 os << " "; |
|
1049 pr_complex (os, cm.elem (i, j), r_fw, i_fw); |
|
1050 } |
|
1051 os << "\n"; |
|
1052 } |
|
1053 col += inc; |
|
1054 } |
|
1055 } |
|
1056 } |
|
1057 |
|
1058 void |
164
|
1059 octave_print_internal (ostrstream& os, const Range& r) |
1
|
1060 { |
|
1061 double b = r.base (); |
|
1062 double increment = r.inc (); |
|
1063 int num_elem = r.nelem (); |
|
1064 |
|
1065 if (plus_format) |
|
1066 { |
|
1067 os << " "; |
|
1068 for (int i = 0; i < num_elem; i++) |
|
1069 { |
|
1070 double val = b + i * increment; |
|
1071 if (val == 0.0) |
|
1072 os << " "; |
|
1073 else |
|
1074 os << "+"; |
|
1075 } |
|
1076 } |
|
1077 else |
|
1078 { |
|
1079 int fw; |
|
1080 set_format (r, fw); |
|
1081 int column_width = fw + 2; |
|
1082 int total_width = num_elem * column_width; |
|
1083 int max_width = terminal_columns (); |
|
1084 |
|
1085 if (free_format) |
|
1086 { |
|
1087 os << r; |
|
1088 return; |
|
1089 } |
|
1090 |
|
1091 int inc = num_elem; |
|
1092 if (total_width > max_width && user_pref.split_long_rows) |
|
1093 { |
|
1094 inc = max_width / column_width; |
|
1095 if (inc == 0) |
|
1096 inc++; |
|
1097 } |
|
1098 |
|
1099 int col = 0; |
|
1100 while (col < num_elem) |
|
1101 { |
|
1102 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1103 |
|
1104 if (total_width > max_width && user_pref.split_long_rows) |
|
1105 { |
|
1106 if (col != 0) |
|
1107 os << "\n"; |
|
1108 |
|
1109 int num_cols = lim - col; |
|
1110 if (num_cols == 1) |
|
1111 os << " Column " << col + 1 << ":\n\n"; |
|
1112 else if (num_cols == 2) |
|
1113 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
1114 else |
|
1115 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
1116 } |
|
1117 |
|
1118 for (int i = col; i < lim; i++) |
|
1119 { |
|
1120 double val = b + i * increment; |
|
1121 os << " "; |
|
1122 pr_float (os, val, fw); |
|
1123 } |
|
1124 |
|
1125 os << "\n"; |
|
1126 |
|
1127 col += inc; |
|
1128 } |
|
1129 } |
|
1130 } |
|
1131 |
|
1132 static void |
|
1133 init_format_state (void) |
|
1134 { |
|
1135 free_format = 0; |
|
1136 plus_format = 0; |
|
1137 bank_format = 0; |
|
1138 print_e = 0; |
|
1139 print_big_e = 0; |
|
1140 } |
|
1141 |
|
1142 static void |
|
1143 set_output_prec_and_fw (int prec, int fw) |
|
1144 { |
|
1145 tree_constant *tmp = NULL_TREE_CONST; |
|
1146 |
|
1147 tmp = new tree_constant ((double) prec); |
|
1148 bind_variable ("output_precision", tmp); |
|
1149 |
|
1150 tmp = new tree_constant ((double) fw); |
|
1151 bind_variable ("output_max_field_width", tmp); |
|
1152 } |
|
1153 |
|
1154 void |
|
1155 set_format_style (int argc, char **argv) |
|
1156 { |
|
1157 if (--argc > 0) |
|
1158 { |
|
1159 argv++; |
|
1160 if (*argv[0]) |
|
1161 { |
|
1162 if (strcmp (*argv, "short") == 0) |
|
1163 { |
|
1164 if (--argc > 0) |
|
1165 { |
|
1166 argv++; |
|
1167 if (strcmp (*argv, "e") == 0) |
|
1168 { |
|
1169 init_format_state (); |
|
1170 print_e = 1; |
|
1171 } |
|
1172 else if (strcmp (*argv, "E") == 0) |
|
1173 { |
|
1174 init_format_state (); |
|
1175 print_e = 1; |
|
1176 print_big_e = 1; |
|
1177 } |
|
1178 else |
|
1179 { |
|
1180 message ("format", |
|
1181 "unrecognized option `short %s'", *argv); |
|
1182 return; |
|
1183 } |
|
1184 } |
|
1185 else |
|
1186 init_format_state (); |
|
1187 |
|
1188 set_output_prec_and_fw (3, 8); |
|
1189 } |
|
1190 else if (strcmp (*argv, "long") == 0) |
|
1191 { |
|
1192 if (--argc > 0) |
|
1193 { |
|
1194 argv++; |
|
1195 if (strcmp (*argv, "e") == 0) |
|
1196 { |
|
1197 init_format_state (); |
|
1198 print_e = 1; |
|
1199 } |
|
1200 else if (strcmp (*argv, "E") == 0) |
|
1201 { |
|
1202 init_format_state (); |
|
1203 print_e = 1; |
|
1204 print_big_e = 1; |
|
1205 } |
|
1206 else |
|
1207 { |
|
1208 message ("format", |
|
1209 "unrecognized option `long %s'", *argv); |
|
1210 return; |
|
1211 } |
|
1212 } |
|
1213 else |
|
1214 init_format_state (); |
|
1215 |
|
1216 set_output_prec_and_fw (15, 24); |
|
1217 } |
|
1218 else if (strcmp (*argv, "hex") == 0) |
|
1219 message ("format", "format state `hex' not implemented"); |
|
1220 else if (strcmp (*argv, "+") == 0) |
|
1221 { |
|
1222 init_format_state (); |
|
1223 plus_format = 1; |
|
1224 } |
|
1225 else if (strcmp (*argv, "bank") == 0) |
|
1226 { |
|
1227 init_format_state (); |
|
1228 bank_format = 1; |
|
1229 } |
|
1230 else if (strcmp (*argv, "free") == 0) |
|
1231 { |
|
1232 init_format_state (); |
|
1233 free_format = 1; |
|
1234 } |
|
1235 else if (strcmp (*argv, "none") == 0) |
|
1236 { |
|
1237 init_format_state (); |
|
1238 free_format = 1; |
|
1239 } |
|
1240 else if (strcmp (*argv, "compact") == 0) |
|
1241 message ("format", "format state `compact' not implemented"); |
|
1242 else if (strcmp (*argv, "loose") == 0) |
|
1243 message ("format", "format state `loose' not implemented"); |
|
1244 else |
|
1245 message ("format", "unrecognized format state `%s'", *argv); |
|
1246 } |
|
1247 else |
|
1248 usage ("format [format_state]"); |
|
1249 } |
|
1250 else |
|
1251 { |
|
1252 init_format_state (); |
|
1253 set_output_prec_and_fw (5, 10); |
|
1254 } |
|
1255 } |
|
1256 |
|
1257 /* |
|
1258 ;;; Local Variables: *** |
|
1259 ;;; mode: C++ *** |
|
1260 ;;; page-delimiter: "^/\\*" *** |
|
1261 ;;; End: *** |
|
1262 */ |