1
|
1 // pr-output.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
|
30 #include <string.h> |
|
31 #include <math.h> |
|
32 #include <float.h> |
|
33 #include <Complex.h> |
|
34 |
453
|
35 #include "dMatrix.h" |
|
36 #include "CMatrix.h" |
1
|
37 #include "Range.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" |
1282
|
44 #include "sysdep.h" |
1
|
45 #include "pager.h" |
542
|
46 #include "help.h" |
1
|
47 #include "error.h" |
|
48 #include "utils.h" |
529
|
49 #include "defun.h" |
1
|
50 |
|
51 // Current format string for real numbers and the real part of complex |
|
52 // numbers. |
529
|
53 static char *curr_real_fmt = 0; |
1
|
54 |
|
55 // Current format string for the imaginary part of complex numbers. |
529
|
56 static char *curr_imag_fmt = 0; |
1
|
57 |
1186
|
58 // Nonzero means don't do any fancy formatting. |
1
|
59 static int free_format = 0; |
|
60 |
|
61 // Nonzero means print plus sign for nonzero, blank for zero. |
|
62 static int plus_format = 0; |
|
63 |
1282
|
64 // Nonzero means always print like dollars and cents. |
|
65 static int bank_format = 0; |
|
66 |
|
67 // Nonzero means print data in hexadecimal format. |
|
68 static int hex_format = 0; |
|
69 |
1309
|
70 // Nonzero means print data in binary-bit-pattern format. |
|
71 static int bit_format = 0; |
|
72 |
1186
|
73 // Nonzero means don't put newlines around the column number headers. |
|
74 static int compact_format = 0; |
|
75 |
1
|
76 // Nonzero means use an e format. |
|
77 static int print_e = 0; |
|
78 |
|
79 // Nonzero means print E instead of e for exponent field. |
|
80 static int print_big_e = 0; |
|
81 |
|
82 static int |
|
83 any_element_is_negative (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 if (a.elem (i, j) < 0.0) |
|
90 return 1; |
|
91 return 0; |
|
92 } |
|
93 |
1309
|
94 // XXX FIXME XXX -- these should probably be somewhere else. |
|
95 |
|
96 int |
1
|
97 any_element_is_inf_or_nan (const Matrix& a) |
|
98 { |
|
99 int nr = a.rows (); |
|
100 int nc = a.columns (); |
|
101 for (int j = 0; j < nc; j++) |
|
102 for (int i = 0; i < nr; i++) |
|
103 { |
|
104 double val = a.elem (i, j); |
|
105 if (xisinf (val) || xisnan (val)) |
|
106 return 1; |
|
107 } |
|
108 return 0; |
|
109 } |
|
110 |
1309
|
111 int |
1
|
112 any_element_is_inf_or_nan (const ComplexMatrix& a) |
|
113 { |
|
114 int nr = a.rows (); |
|
115 int nc = a.columns (); |
|
116 for (int j = 0; j < nc; j++) |
|
117 for (int i = 0; i < nr; i++) |
|
118 { |
|
119 Complex val = a.elem (i, j); |
|
120 if (xisinf (val) || xisnan (val)) |
|
121 return 1; |
|
122 } |
|
123 return 0; |
|
124 } |
|
125 |
|
126 static int |
|
127 all_elements_are_int_or_inf_or_nan (const Matrix& a) |
|
128 { |
|
129 int nr = a.rows (); |
|
130 int nc = a.columns (); |
|
131 for (int j = 0; j < nc; j++) |
|
132 for (int i = 0; i < nr; i++) |
|
133 { |
|
134 double val = a.elem (i, j); |
|
135 if (xisnan (val) || D_NINT (val) == val) |
|
136 continue; |
|
137 else |
|
138 return 0; |
|
139 } |
|
140 return 1; |
|
141 } |
|
142 |
|
143 static Matrix |
|
144 abs (const Matrix& a) |
|
145 { |
|
146 int nr = a.rows (); |
|
147 int nc = a.columns (); |
|
148 Matrix retval (nr, nc); |
|
149 for (int j = 0; j < nc; j++) |
|
150 for (int i = 0; i < nr; i++) |
|
151 retval.elem (i, j) = fabs (a.elem (i, j)); |
|
152 return retval; |
|
153 } |
|
154 |
|
155 static double |
164
|
156 pr_max_internal (const Matrix& m) |
1
|
157 { |
|
158 int nr = m.rows (); |
|
159 int nc = m.columns (); |
|
160 |
|
161 double result = DBL_MIN; |
|
162 |
|
163 for (int j = 0; j < nc; j++) |
|
164 for (int i = 0; i < nr; i++) |
|
165 { |
|
166 double val = m.elem (i, j); |
|
167 if (xisinf (val) || xisnan (val)) |
|
168 continue; |
|
169 |
|
170 if (val > result) |
|
171 result = val; |
|
172 } |
|
173 return result; |
|
174 } |
|
175 |
|
176 static double |
164
|
177 pr_min_internal (const Matrix& m) |
1
|
178 { |
|
179 int nr = m.rows (); |
|
180 int nc = m.columns (); |
|
181 |
|
182 double result = DBL_MAX; |
|
183 |
|
184 for (int j = 0; j < nc; j++) |
|
185 for (int i = 0; i < nr; i++) |
|
186 { |
|
187 double val = m.elem (i, j); |
|
188 if (xisinf (val) || xisnan (val)) |
|
189 continue; |
|
190 |
|
191 if (val < result) |
|
192 result = val; |
|
193 } |
|
194 return result; |
|
195 } |
|
196 |
|
197 static void |
|
198 set_format (double d, int& fw) |
|
199 { |
529
|
200 curr_real_fmt = 0; |
|
201 curr_imag_fmt = 0; |
1
|
202 |
|
203 if (free_format) |
|
204 return; |
|
205 |
278
|
206 static char fmt_buf[128]; |
1
|
207 |
|
208 int sign = (d < 0.0); |
|
209 |
|
210 int inf_or_nan = (xisinf (d) || xisnan (d)); |
|
211 |
|
212 double d_abs = d < 0.0 ? -d : d; |
|
213 |
575
|
214 int digits = (inf_or_nan || d_abs == 0.0) ? 0 |
|
215 : (int) floor (log10 (d_abs) + 1.0); |
1
|
216 |
|
217 int prec = user_pref.output_precision; |
|
218 |
|
219 int ld, rd; |
|
220 |
|
221 if (bank_format) |
|
222 { |
|
223 fw = digits < 0 ? 4 : digits + 3; |
|
224 if (inf_or_nan && fw < 3) |
|
225 fw = 3; |
|
226 fw += sign; |
|
227 rd = 2; |
|
228 } |
1282
|
229 else if (hex_format) |
|
230 { |
|
231 fw = 2 * sizeof (double); |
|
232 rd = 0; |
|
233 } |
1309
|
234 else if (bit_format) |
|
235 { |
|
236 fw = 8 * sizeof (double); |
|
237 rd = 0; |
|
238 } |
1
|
239 else if (xisnan (d) || D_NINT (d) == d) |
|
240 { |
|
241 fw = digits; |
|
242 if (inf_or_nan && fw < 3) |
|
243 fw = 3; |
|
244 fw += sign; |
|
245 rd = 0; |
|
246 } |
|
247 else |
|
248 { |
|
249 if (digits > 0) |
|
250 { |
|
251 ld = digits; |
|
252 rd = prec - digits; |
|
253 digits++; |
|
254 } |
|
255 else |
|
256 { |
|
257 ld = 1; |
|
258 rd = prec - digits; |
|
259 digits = -digits + 1; |
|
260 } |
|
261 |
|
262 fw = ld + 1 + rd; |
|
263 if (inf_or_nan && fw < 3) |
|
264 fw = 3; |
|
265 fw += sign; |
|
266 } |
|
267 |
1309
|
268 if (! (bank_format || hex_format || bit_format) |
1282
|
269 && (fw > user_pref.output_max_field_width || print_e)) |
1
|
270 { |
|
271 int exp_field = 4; |
|
272 if (digits > 100) |
|
273 exp_field++; |
|
274 |
|
275 fw = 2 + prec + exp_field; |
|
276 if (inf_or_nan && fw < 3) |
|
277 fw = 3; |
|
278 fw += sign; |
|
279 |
|
280 if (print_big_e) |
|
281 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
282 else |
|
283 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
284 } |
|
285 else |
|
286 { |
|
287 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
288 } |
|
289 |
|
290 curr_real_fmt = &fmt_buf[0]; |
|
291 } |
|
292 |
|
293 static inline void |
|
294 set_format (double d) |
|
295 { |
|
296 int fw; |
|
297 set_format (d, fw); |
|
298 } |
|
299 |
|
300 static void |
164
|
301 set_format (const Matrix& m, int& fw) |
1
|
302 { |
529
|
303 curr_real_fmt = 0; |
|
304 curr_imag_fmt = 0; |
1
|
305 |
|
306 if (free_format) |
|
307 return; |
|
308 |
278
|
309 static char fmt_buf[128]; |
1
|
310 |
|
311 int sign = any_element_is_negative (m); |
|
312 |
|
313 int inf_or_nan = any_element_is_inf_or_nan (m); |
|
314 |
|
315 Matrix m_abs = abs (m); |
|
316 double max_abs = pr_max_internal (m_abs); |
|
317 double min_abs = pr_min_internal (m_abs); |
|
318 |
|
319 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
320 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
321 |
|
322 int prec = user_pref.output_precision; |
|
323 |
|
324 int ld, rd; |
|
325 |
|
326 if (bank_format) |
|
327 { |
|
328 int digits = x_max > x_min ? x_max : x_min; |
|
329 fw = digits <= 0 ? 4 : digits + 3; |
|
330 if (inf_or_nan && fw < 3) |
|
331 fw = 3; |
|
332 fw += sign; |
|
333 rd = 2; |
|
334 } |
1282
|
335 else if (hex_format) |
|
336 { |
|
337 fw = 2 * sizeof (double); |
|
338 rd = 0; |
|
339 } |
1309
|
340 else if (bit_format) |
|
341 { |
|
342 fw = 8 * sizeof (double); |
|
343 rd = 0; |
|
344 } |
1
|
345 else if (all_elements_are_int_or_inf_or_nan (m)) |
|
346 { |
|
347 int digits = x_max > x_min ? x_max : x_min; |
|
348 fw = digits <= 0 ? 1 : digits; |
|
349 if (inf_or_nan && fw < 3) |
|
350 fw = 3; |
|
351 fw += sign; |
|
352 rd = 0; |
|
353 } |
|
354 else |
|
355 { |
|
356 int ld_max, rd_max; |
|
357 if (x_max > 0) |
|
358 { |
|
359 ld_max = x_max; |
|
360 rd_max = prec - x_max; |
|
361 x_max++; |
|
362 } |
|
363 else |
|
364 { |
|
365 ld_max = 1; |
|
366 rd_max = prec - x_max; |
|
367 x_max = -x_max + 1; |
|
368 } |
|
369 |
|
370 int ld_min, rd_min; |
|
371 if (x_min > 0) |
|
372 { |
|
373 ld_min = x_min; |
|
374 rd_min = prec - x_min; |
|
375 x_min++; |
|
376 } |
|
377 else |
|
378 { |
|
379 ld_min = 1; |
|
380 rd_min = prec - x_min; |
|
381 x_min = -x_min + 1; |
|
382 } |
|
383 |
|
384 ld = ld_max > ld_min ? ld_max : ld_min; |
|
385 rd = rd_max > rd_min ? rd_max : rd_min; |
|
386 |
|
387 fw = ld + 1 + rd; |
|
388 if (inf_or_nan && fw < 3) |
|
389 fw = 3; |
|
390 fw += sign; |
|
391 } |
|
392 |
1309
|
393 if (! (bank_format ||hex_format || bit_format) |
1282
|
394 && (fw > user_pref.output_max_field_width || print_e)) |
1
|
395 { |
|
396 int exp_field = 4; |
|
397 if (x_max > 100 || x_min > 100) |
|
398 exp_field++; |
|
399 |
|
400 fw = 2 + prec + exp_field; |
|
401 if (inf_or_nan && fw < 3) |
|
402 fw = 3; |
|
403 fw += sign; |
|
404 |
|
405 if (print_big_e) |
|
406 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
407 else |
|
408 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
409 } |
|
410 else |
|
411 { |
|
412 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
413 } |
|
414 |
|
415 curr_real_fmt = &fmt_buf[0]; |
|
416 } |
|
417 |
|
418 static inline void |
164
|
419 set_format (const Matrix& m) |
1
|
420 { |
|
421 int fw; |
|
422 set_format (m, fw); |
|
423 } |
|
424 |
|
425 static void |
164
|
426 set_format (const Complex& c, int& r_fw, int& i_fw) |
1
|
427 { |
529
|
428 curr_real_fmt = 0; |
|
429 curr_imag_fmt = 0; |
1
|
430 |
|
431 if (free_format) |
|
432 return; |
|
433 |
278
|
434 static char r_fmt_buf[128]; |
|
435 static char i_fmt_buf[128]; |
1
|
436 |
|
437 double rp = c.real (); |
|
438 double ip = c.imag (); |
|
439 |
|
440 int sign = (rp < 0.0); |
|
441 |
|
442 int inf_or_nan = (xisinf (c) || xisnan (c)); |
|
443 |
|
444 double r_abs = rp < 0.0 ? -rp : rp; |
|
445 double i_abs = ip < 0.0 ? -ip : ip; |
|
446 |
|
447 int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0); |
|
448 int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0); |
|
449 |
|
450 int x_max, x_min; |
|
451 |
|
452 if (r_x > i_x) |
|
453 { |
|
454 x_max = r_x; |
|
455 x_min = i_x; |
|
456 } |
|
457 else |
|
458 { |
|
459 x_max = i_x; |
|
460 x_min = r_x; |
|
461 } |
|
462 |
|
463 int prec = user_pref.output_precision; |
|
464 |
|
465 int ld, rd; |
|
466 |
|
467 if (bank_format) |
|
468 { |
|
469 int digits = r_x; |
|
470 i_fw = 0; |
|
471 r_fw = digits <= 0 ? 4 : digits + 3; |
|
472 if (inf_or_nan && r_fw < 3) |
|
473 r_fw = 3; |
|
474 r_fw += sign; |
|
475 rd = 2; |
|
476 } |
1282
|
477 else if (hex_format) |
|
478 { |
|
479 r_fw = 2 * sizeof (double); |
|
480 i_fw = 2 * sizeof (double); |
|
481 rd = 0; |
|
482 } |
1309
|
483 else if (bit_format) |
|
484 { |
|
485 r_fw = 8 * sizeof (double); |
|
486 i_fw = 8 * sizeof (double); |
|
487 rd = 0; |
|
488 } |
1
|
489 else if (inf_or_nan || (D_NINT (rp) == rp && D_NINT (ip) == ip)) |
|
490 { |
|
491 int digits = x_max > x_min ? x_max : x_min; |
|
492 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
493 if (inf_or_nan && i_fw < 3) |
|
494 i_fw = r_fw = 3; |
|
495 r_fw += sign; |
|
496 rd = 0; |
|
497 } |
|
498 else |
|
499 { |
|
500 int ld_max, rd_max; |
|
501 if (x_max > 0) |
|
502 { |
|
503 ld_max = x_max; |
|
504 rd_max = prec - x_max; |
|
505 x_max++; |
|
506 } |
|
507 else |
|
508 { |
|
509 ld_max = 1; |
|
510 rd_max = prec - x_max; |
|
511 x_max = -x_max + 1; |
|
512 } |
|
513 |
|
514 int ld_min, rd_min; |
|
515 if (x_min > 0) |
|
516 { |
|
517 ld_min = x_min; |
|
518 rd_min = prec - x_min; |
|
519 x_min++; |
|
520 } |
|
521 else |
|
522 { |
|
523 ld_min = 1; |
|
524 rd_min = prec - x_min; |
|
525 x_min = -x_min + 1; |
|
526 } |
|
527 |
|
528 ld = ld_max > ld_min ? ld_max : ld_min; |
|
529 rd = rd_max > rd_min ? rd_max : rd_min; |
|
530 |
|
531 i_fw = r_fw = ld + 1 + rd; |
|
532 if (inf_or_nan && i_fw < 3) |
|
533 i_fw = r_fw = 3; |
|
534 r_fw += sign; |
|
535 } |
|
536 |
1309
|
537 if (! (bank_format || hex_format || bit_format) |
1282
|
538 && (r_fw > user_pref.output_max_field_width || print_e)) |
1
|
539 { |
|
540 int exp_field = 4; |
|
541 if (x_max > 100 || x_min > 100) |
|
542 exp_field++; |
|
543 |
|
544 i_fw = r_fw = 1 + prec + exp_field; |
|
545 if (inf_or_nan && i_fw < 3) |
|
546 i_fw = r_fw = 3; |
|
547 r_fw += sign; |
|
548 |
|
549 if (print_big_e) |
|
550 { |
|
551 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
552 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
553 } |
|
554 else |
|
555 { |
|
556 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
557 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
558 } |
|
559 } |
|
560 else |
|
561 { |
|
562 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
563 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
564 } |
|
565 |
|
566 curr_real_fmt = &r_fmt_buf[0]; |
|
567 curr_imag_fmt = &i_fmt_buf[0]; |
|
568 } |
|
569 |
|
570 static inline void |
164
|
571 set_format (const Complex& c) |
1
|
572 { |
|
573 int r_fw, i_fw; |
|
574 set_format (c, r_fw, i_fw); |
|
575 } |
|
576 |
|
577 static void |
164
|
578 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw) |
1
|
579 { |
529
|
580 curr_real_fmt = 0; |
|
581 curr_imag_fmt = 0; |
1
|
582 |
|
583 if (free_format) |
|
584 return; |
|
585 |
278
|
586 static char r_fmt_buf[128]; |
|
587 static char i_fmt_buf[128]; |
1
|
588 |
|
589 Matrix rp = real (cm); |
|
590 Matrix ip = imag (cm); |
|
591 |
|
592 int sign = any_element_is_negative (rp); |
|
593 |
|
594 int inf_or_nan = any_element_is_inf_or_nan (cm); |
|
595 |
|
596 Matrix r_m_abs = abs (rp); |
|
597 double r_max_abs = pr_max_internal (r_m_abs); |
|
598 double r_min_abs = pr_min_internal (r_m_abs); |
|
599 |
|
600 Matrix i_m_abs = abs (ip); |
|
601 double i_max_abs = pr_max_internal (i_m_abs); |
|
602 double i_min_abs = pr_min_internal (i_m_abs); |
|
603 |
|
604 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
605 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
606 |
|
607 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
608 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
609 |
|
610 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
611 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
612 |
|
613 int prec = user_pref.output_precision; |
|
614 |
|
615 int ld, rd; |
|
616 |
|
617 if (bank_format) |
|
618 { |
|
619 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
620 i_fw = 0; |
|
621 r_fw = digits <= 0 ? 4 : digits + 3; |
|
622 if (inf_or_nan && i_fw < 3) |
|
623 i_fw = r_fw = 3; |
|
624 r_fw += sign; |
|
625 rd = 2; |
|
626 } |
1282
|
627 else if (hex_format) |
|
628 { |
|
629 r_fw = 2 * sizeof (double); |
|
630 i_fw = 2 * sizeof (double); |
|
631 rd = 0; |
|
632 } |
1309
|
633 else if (bit_format) |
|
634 { |
|
635 r_fw = 8 * sizeof (double); |
|
636 i_fw = 8 * sizeof (double); |
|
637 rd = 0; |
|
638 } |
1
|
639 else if (all_elements_are_int_or_inf_or_nan (rp) |
|
640 && all_elements_are_int_or_inf_or_nan (ip)) |
|
641 { |
|
642 int digits = x_max > x_min ? x_max : x_min; |
|
643 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
644 if (inf_or_nan && i_fw < 3) |
|
645 i_fw = r_fw = 3; |
|
646 r_fw += sign; |
|
647 rd = 0; |
|
648 } |
|
649 else |
|
650 { |
|
651 int ld_max, rd_max; |
|
652 if (x_max > 0) |
|
653 { |
|
654 ld_max = x_max; |
|
655 rd_max = prec - x_max; |
|
656 x_max++; |
|
657 } |
|
658 else |
|
659 { |
|
660 ld_max = 1; |
|
661 rd_max = prec - x_max; |
|
662 x_max = -x_max + 1; |
|
663 } |
|
664 |
|
665 int ld_min, rd_min; |
|
666 if (x_min > 0) |
|
667 { |
|
668 ld_min = x_min; |
|
669 rd_min = prec - x_min; |
|
670 x_min++; |
|
671 } |
|
672 else |
|
673 { |
|
674 ld_min = 1; |
|
675 rd_min = prec - x_min; |
|
676 x_min = -x_min + 1; |
|
677 } |
|
678 |
|
679 ld = ld_max > ld_min ? ld_max : ld_min; |
|
680 rd = rd_max > rd_min ? rd_max : rd_min; |
|
681 |
|
682 i_fw = r_fw = ld + 1 + rd; |
|
683 if (inf_or_nan && i_fw < 3) |
|
684 i_fw = r_fw = 3; |
|
685 r_fw += sign; |
|
686 } |
|
687 |
1309
|
688 if (! (bank_format || hex_format || bit_format) |
1282
|
689 && (r_fw > user_pref.output_max_field_width || print_e)) |
1
|
690 { |
|
691 int exp_field = 4; |
|
692 if (x_max > 100 || x_min > 100) |
|
693 exp_field++; |
|
694 |
|
695 i_fw = r_fw = 1 + prec + exp_field; |
|
696 if (inf_or_nan && i_fw < 3) |
|
697 i_fw = r_fw = 3; |
|
698 r_fw += sign; |
|
699 |
|
700 if (print_big_e) |
|
701 { |
|
702 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
703 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
704 } |
|
705 else |
|
706 { |
|
707 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
708 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
709 } |
|
710 } |
|
711 else |
|
712 { |
|
713 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
714 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
715 } |
|
716 |
|
717 curr_real_fmt = &r_fmt_buf[0]; |
|
718 curr_imag_fmt = &i_fmt_buf[0]; |
|
719 } |
|
720 |
|
721 static int |
164
|
722 all_elements_are_ints (const Range& r) |
1
|
723 { |
1282
|
724 // If the base and increment are ints, the final value in the range |
|
725 // will also be an integer, even if the limit is not. |
1
|
726 |
|
727 double b = r.base (); |
|
728 double i = r.inc (); |
|
729 |
1086
|
730 return (! (xisnan (b) || xisnan (i)) |
|
731 && (double) NINT (b) == b && (double) NINT (i) == i); |
1
|
732 } |
|
733 |
|
734 static inline void |
164
|
735 set_format (const ComplexMatrix& cm) |
1
|
736 { |
|
737 int r_fw, i_fw; |
|
738 set_format (cm, r_fw, i_fw); |
|
739 } |
|
740 |
|
741 static void |
164
|
742 set_format (const Range& r, int& fw) |
1
|
743 { |
529
|
744 curr_real_fmt = 0; |
|
745 curr_imag_fmt = 0; |
1
|
746 |
|
747 if (free_format) |
|
748 return; |
|
749 |
278
|
750 static char fmt_buf[128]; |
1
|
751 |
|
752 double r_min = r.base (); |
|
753 double r_max = r.limit (); |
|
754 |
|
755 if (r_max < r_min) |
|
756 { |
|
757 double tmp = r_max; |
|
758 r_max = r_min; |
|
759 r_min = tmp; |
|
760 } |
|
761 |
|
762 int sign = (r_min < 0.0); |
|
763 |
|
764 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
765 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
766 |
|
767 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
768 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
769 |
|
770 int prec = user_pref.output_precision; |
|
771 |
|
772 int ld, rd; |
|
773 |
|
774 if (bank_format) |
|
775 { |
|
776 int digits = x_max > x_min ? x_max : x_min; |
|
777 fw = sign + digits < 0 ? 4 : digits + 3; |
|
778 rd = 2; |
|
779 } |
1282
|
780 else if (hex_format) |
|
781 { |
|
782 fw = 2 * sizeof (double); |
|
783 rd = 0; |
|
784 } |
1309
|
785 else if (bit_format) |
|
786 { |
|
787 fw = 8 * sizeof (double); |
|
788 rd = 0; |
|
789 } |
1
|
790 else if (all_elements_are_ints (r)) |
|
791 { |
|
792 int digits = x_max > x_min ? x_max : x_min; |
|
793 fw = sign + digits; |
|
794 rd = 0; |
|
795 } |
|
796 else |
|
797 { |
|
798 int ld_max, rd_max; |
|
799 if (x_max > 0) |
|
800 { |
|
801 ld_max = x_max; |
|
802 rd_max = prec - x_max; |
|
803 x_max++; |
|
804 } |
|
805 else |
|
806 { |
|
807 ld_max = 1; |
|
808 rd_max = prec - x_max; |
|
809 x_max = -x_max + 1; |
|
810 } |
|
811 |
|
812 int ld_min, rd_min; |
|
813 if (x_min > 0) |
|
814 { |
|
815 ld_min = x_min; |
|
816 rd_min = prec - x_min; |
|
817 x_min++; |
|
818 } |
|
819 else |
|
820 { |
|
821 ld_min = 1; |
|
822 rd_min = prec - x_min; |
|
823 x_min = -x_min + 1; |
|
824 } |
|
825 |
|
826 ld = ld_max > ld_min ? ld_max : ld_min; |
|
827 rd = rd_max > rd_min ? rd_max : rd_min; |
|
828 |
|
829 fw = sign + ld + 1 + rd; |
|
830 } |
|
831 |
1309
|
832 if (! (bank_format || hex_format || bit_format) |
1282
|
833 && (fw > user_pref.output_max_field_width || print_e)) |
1
|
834 { |
|
835 int exp_field = 4; |
|
836 if (x_max > 100 || x_min > 100) |
|
837 exp_field++; |
|
838 |
|
839 fw = sign + 2 + prec + exp_field; |
|
840 |
|
841 if (print_big_e) |
|
842 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
843 else |
|
844 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
845 } |
|
846 else |
|
847 { |
|
848 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
849 } |
|
850 |
|
851 curr_real_fmt = &fmt_buf[0]; |
|
852 } |
|
853 |
|
854 static inline void |
164
|
855 set_format (const Range& r) |
1
|
856 { |
|
857 int fw; |
|
858 set_format (r, fw); |
|
859 } |
|
860 |
1282
|
861 union equiv |
|
862 { |
|
863 double d; |
|
864 unsigned char i[sizeof (double)]; |
|
865 }; |
|
866 |
1309
|
867 #define PRINT_CHAR_BITS(os, c) \ |
|
868 do \ |
|
869 { \ |
|
870 unsigned char ctmp = c; \ |
|
871 char stmp[9]; \ |
|
872 stmp[0] = (c & 0x80) ? '1' : '0'; \ |
|
873 stmp[1] = (c & 0x40) ? '1' : '0'; \ |
|
874 stmp[2] = (c & 0x20) ? '1' : '0'; \ |
|
875 stmp[3] = (c & 0x10) ? '1' : '0'; \ |
|
876 stmp[4] = (c & 0x08) ? '1' : '0'; \ |
|
877 stmp[5] = (c & 0x04) ? '1' : '0'; \ |
|
878 stmp[6] = (c & 0x02) ? '1' : '0'; \ |
|
879 stmp[7] = (c & 0x01) ? '1' : '0'; \ |
|
880 stmp[8] = '\0'; \ |
|
881 os.form ("%s", stmp); \ |
|
882 } \ |
|
883 while (0) |
|
884 |
|
885 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
886 do \ |
|
887 { \ |
|
888 unsigned char ctmp = c; \ |
|
889 char stmp[9]; \ |
|
890 stmp[0] = (c & 0x01) ? '1' : '0'; \ |
|
891 stmp[1] = (c & 0x02) ? '1' : '0'; \ |
|
892 stmp[2] = (c & 0x04) ? '1' : '0'; \ |
|
893 stmp[3] = (c & 0x08) ? '1' : '0'; \ |
|
894 stmp[4] = (c & 0x10) ? '1' : '0'; \ |
|
895 stmp[5] = (c & 0x20) ? '1' : '0'; \ |
|
896 stmp[6] = (c & 0x40) ? '1' : '0'; \ |
|
897 stmp[7] = (c & 0x80) ? '1' : '0'; \ |
|
898 stmp[8] = '\0'; \ |
|
899 os.form ("%s", stmp); \ |
|
900 } \ |
|
901 while (0) |
|
902 |
1
|
903 static inline void |
626
|
904 pr_any_float (const char *fmt, ostream& os, double d, int fw = 0) |
1
|
905 { |
|
906 if (d == -0.0) |
|
907 d = 0.0; |
|
908 |
529
|
909 if (fmt) |
1
|
910 { |
1282
|
911 if (hex_format) |
|
912 { |
|
913 equiv tmp; |
|
914 tmp.d = d; |
|
915 |
|
916 // Unless explicitly asked for, always print in big-endian |
|
917 // format. |
|
918 |
|
919 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
920 // formats and not for Cray? |
|
921 |
|
922 if (hex_format > 1 |
|
923 || native_float_format == OCTAVE_IEEE_BIG |
|
924 || native_float_format == OCTAVE_CRAY |
|
925 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
926 { |
1322
|
927 for (size_t i = 0; i < sizeof (double); i++) |
1282
|
928 os.form ("%02x", (int) tmp.i[i]); |
|
929 } |
|
930 else |
|
931 { |
1328
|
932 for (int i = sizeof (double) - 1; i >= 0; i--) |
1282
|
933 os.form ("%02x", (int) tmp.i[i]); |
|
934 } |
|
935 } |
1309
|
936 else if (bit_format) |
|
937 { |
|
938 equiv tmp; |
|
939 tmp.d = d; |
|
940 |
|
941 // Unless explicitly asked for, always print in big-endian |
|
942 // format. |
|
943 |
|
944 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
945 // formats and not for Cray? |
|
946 |
|
947 if (native_float_format == OCTAVE_IEEE_BIG |
|
948 || native_float_format == OCTAVE_CRAY |
|
949 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
950 { |
1322
|
951 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
952 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
953 } |
|
954 else |
|
955 { |
|
956 if (bit_format > 1) |
|
957 { |
1328
|
958 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
959 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
960 } |
|
961 else |
|
962 { |
|
963 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
964 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
965 } |
|
966 } |
|
967 } |
1282
|
968 else if (xisinf (d)) |
1
|
969 { |
|
970 char *s; |
|
971 if (d < 0.0) |
|
972 s = "-Inf"; |
|
973 else |
|
974 s = "Inf"; |
|
975 |
|
976 if (fw > 0) |
|
977 os.form ("%*s", fw, s); |
|
978 else |
|
979 os << s; |
|
980 } |
|
981 else if (xisnan (d)) |
|
982 { |
|
983 if (fw > 0) |
|
984 os.form ("%*s", fw, "NaN"); |
|
985 else |
|
986 os << "NaN"; |
|
987 } |
|
988 else |
|
989 os.form (fmt, d); |
|
990 } |
529
|
991 else |
|
992 os << d; |
1
|
993 } |
|
994 |
|
995 static inline void |
626
|
996 pr_float (ostream& os, double d, int fw = 0) |
1
|
997 { |
|
998 pr_any_float (curr_real_fmt, os, d, fw); |
|
999 } |
|
1000 |
|
1001 static inline void |
626
|
1002 pr_imag_float (ostream& os, double d, int fw = 0) |
1
|
1003 { |
|
1004 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1005 } |
|
1006 |
|
1007 static inline void |
626
|
1008 pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
1009 { |
|
1010 double r = c.real (); |
|
1011 pr_float (os, r, r_fw); |
|
1012 if (! bank_format) |
|
1013 { |
|
1014 double i = c.imag (); |
1309
|
1015 if (! (hex_format || bit_format) && i < 0) |
1
|
1016 { |
|
1017 os << " - "; |
|
1018 i = -i; |
|
1019 pr_imag_float (os, i, i_fw); |
|
1020 } |
|
1021 else |
|
1022 { |
1309
|
1023 if (hex_format || bit_format) |
1282
|
1024 os << " "; |
|
1025 else |
|
1026 os << " + "; |
|
1027 |
1
|
1028 pr_imag_float (os, i, i_fw); |
|
1029 } |
|
1030 os << "i"; |
|
1031 } |
|
1032 } |
|
1033 |
626
|
1034 static void |
|
1035 print_empty_matrix (ostream& os, int nr, int nc, int pr_as_read_syntax) |
|
1036 { |
|
1037 assert (nr == 0 || nc == 0); |
|
1038 |
|
1039 if (pr_as_read_syntax) |
|
1040 { |
|
1041 if (nr == 0 && nc == 0) |
|
1042 os << "[]"; |
|
1043 else |
|
1044 os << "zeros (" << nr << ", " << nc << ")"; |
|
1045 } |
|
1046 else |
|
1047 { |
|
1048 os << "[]"; |
|
1049 if (user_pref.print_empty_dimensions) |
|
1050 os << "(" << nr << "x" << nc << ")"; |
|
1051 os << "\n"; |
|
1052 } |
|
1053 } |
|
1054 |
1186
|
1055 static void |
|
1056 pr_col_num_header (ostream& os, int total_width, int max_width, |
|
1057 int lim, int col) |
|
1058 { |
|
1059 if (total_width > max_width && user_pref.split_long_rows) |
|
1060 { |
|
1061 if (col != 0 && ! compact_format) |
|
1062 os << "\n"; |
|
1063 |
|
1064 int num_cols = lim - col; |
|
1065 |
|
1066 if (num_cols == 1) |
|
1067 os << " Column " << col + 1 << ":\n"; |
|
1068 else if (num_cols == 2) |
|
1069 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1070 else |
|
1071 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
|
1072 |
|
1073 if (! compact_format) |
|
1074 os << "\n"; |
|
1075 } |
|
1076 } |
|
1077 |
1
|
1078 void |
719
|
1079 octave_print_internal (ostream& os, double d, int pr_as_read_syntax) |
1
|
1080 { |
|
1081 if (plus_format) |
|
1082 { |
|
1083 if (d == 0.0) |
|
1084 os << " "; |
|
1085 else |
|
1086 os << "+"; |
|
1087 } |
|
1088 else |
|
1089 { |
|
1090 set_format (d); |
|
1091 if (free_format) |
|
1092 os << d; |
|
1093 else |
|
1094 pr_float (os, d); |
|
1095 } |
626
|
1096 |
|
1097 if (! pr_as_read_syntax) |
|
1098 os << "\n"; |
1
|
1099 } |
|
1100 |
|
1101 void |
719
|
1102 octave_print_internal (ostream& os, const Matrix& m, int pr_as_read_syntax) |
1
|
1103 { |
|
1104 int nr = m.rows (); |
|
1105 int nc = m.columns (); |
|
1106 |
626
|
1107 if (nr == 0 || nc == 0) |
|
1108 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1109 else if (plus_format && ! pr_as_read_syntax) |
1
|
1110 { |
|
1111 for (int i = 0; i < nr; i++) |
|
1112 { |
|
1113 for (int j = 0; j < nc; j++) |
|
1114 { |
|
1115 if (j == 0) |
|
1116 os << " "; |
|
1117 |
|
1118 if (m.elem (i, j) == 0.0) |
|
1119 os << " "; |
|
1120 else |
|
1121 os << "+"; |
|
1122 } |
|
1123 os << "\n"; |
|
1124 } |
|
1125 } |
|
1126 else |
|
1127 { |
|
1128 int fw; |
|
1129 set_format (m, fw); |
|
1130 int column_width = fw + 2; |
|
1131 int total_width = nc * column_width; |
|
1132 int max_width = terminal_columns (); |
|
1133 |
626
|
1134 if (pr_as_read_syntax) |
|
1135 max_width -= 4; |
|
1136 |
1
|
1137 if (free_format) |
|
1138 { |
626
|
1139 if (pr_as_read_syntax) |
|
1140 os << "[\n"; |
|
1141 |
1
|
1142 os << m; |
626
|
1143 |
|
1144 if (pr_as_read_syntax) |
|
1145 os << "]"; |
|
1146 |
1
|
1147 return; |
|
1148 } |
|
1149 |
|
1150 int inc = nc; |
|
1151 if (total_width > max_width && user_pref.split_long_rows) |
|
1152 { |
|
1153 inc = max_width / column_width; |
|
1154 if (inc == 0) |
|
1155 inc++; |
|
1156 } |
|
1157 |
626
|
1158 if (pr_as_read_syntax) |
1
|
1159 { |
|
1160 for (int i = 0; i < nr; i++) |
|
1161 { |
626
|
1162 int col = 0; |
|
1163 while (col < nc) |
1
|
1164 { |
626
|
1165 int lim = col + inc < nc ? col + inc : nc; |
|
1166 |
|
1167 for (int j = col; j < lim; j++) |
|
1168 { |
|
1169 if (i == 0 && j == 0) |
|
1170 os << "[ "; |
|
1171 else |
|
1172 { |
|
1173 if (j > col && j < lim) |
|
1174 os << ", "; |
|
1175 else |
|
1176 os << " "; |
|
1177 } |
|
1178 |
|
1179 pr_float (os, m.elem (i, j)); |
|
1180 } |
|
1181 |
|
1182 col += inc; |
|
1183 |
|
1184 if (col >= nc) |
|
1185 { |
|
1186 if (i == nr - 1) |
|
1187 os << " ]"; |
|
1188 else |
|
1189 os << ";\n"; |
|
1190 } |
|
1191 else |
|
1192 os << " ...\n"; |
1
|
1193 } |
|
1194 } |
626
|
1195 } |
|
1196 else |
|
1197 { |
|
1198 for (int col = 0; col < nc; col += inc) |
|
1199 { |
|
1200 int lim = col + inc < nc ? col + inc : nc; |
|
1201 |
1186
|
1202 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1203 |
|
1204 for (int i = 0; i < nr; i++) |
|
1205 { |
|
1206 for (int j = col; j < lim; j++) |
|
1207 { |
|
1208 os << " "; |
|
1209 |
|
1210 pr_float (os, m.elem (i, j), fw); |
|
1211 } |
|
1212 |
|
1213 os << "\n"; |
|
1214 } |
|
1215 } |
1
|
1216 } |
|
1217 } |
|
1218 } |
|
1219 |
|
1220 void |
626
|
1221 octave_print_internal (ostream& os, const Complex& c, |
|
1222 int pr_as_read_syntax) |
1
|
1223 { |
|
1224 if (plus_format) |
|
1225 { |
|
1226 if (c == 0.0) |
|
1227 os << " "; |
|
1228 else |
|
1229 os << "+"; |
|
1230 } |
|
1231 else |
|
1232 { |
|
1233 set_format (c); |
|
1234 if (free_format) |
|
1235 os << c; |
|
1236 else |
|
1237 pr_complex (os, c); |
|
1238 } |
626
|
1239 |
|
1240 if (! pr_as_read_syntax) |
|
1241 os << "\n"; |
1
|
1242 } |
|
1243 |
|
1244 void |
626
|
1245 octave_print_internal (ostream& os, const ComplexMatrix& cm, |
|
1246 int pr_as_read_syntax) |
1
|
1247 { |
|
1248 int nr = cm.rows (); |
|
1249 int nc = cm.columns (); |
|
1250 |
626
|
1251 if (nr == 0 || nc == 0) |
|
1252 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1253 else if (plus_format && ! pr_as_read_syntax) |
1
|
1254 { |
|
1255 for (int i = 0; i < nr; i++) |
|
1256 { |
|
1257 for (int j = 0; j < nc; j++) |
|
1258 { |
|
1259 if (j == 0) |
|
1260 os << " "; |
|
1261 |
|
1262 if (cm.elem (i, j) == 0.0) |
|
1263 os << " "; |
|
1264 else |
|
1265 os << "+"; |
|
1266 } |
|
1267 os << "\n"; |
|
1268 } |
|
1269 } |
|
1270 else |
|
1271 { |
|
1272 int r_fw, i_fw; |
|
1273 set_format (cm, r_fw, i_fw); |
|
1274 int column_width = i_fw + r_fw; |
1309
|
1275 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1276 int total_width = nc * column_width; |
|
1277 int max_width = terminal_columns (); |
|
1278 |
626
|
1279 if (pr_as_read_syntax) |
|
1280 max_width -= 4; |
|
1281 |
1
|
1282 if (free_format) |
|
1283 { |
626
|
1284 if (pr_as_read_syntax) |
|
1285 os << "[\n"; |
|
1286 |
1
|
1287 os << cm; |
626
|
1288 |
|
1289 if (pr_as_read_syntax) |
|
1290 os << "]"; |
|
1291 |
1
|
1292 return; |
|
1293 } |
|
1294 |
|
1295 int inc = nc; |
|
1296 if (total_width > max_width && user_pref.split_long_rows) |
|
1297 { |
|
1298 inc = max_width / column_width; |
|
1299 if (inc == 0) |
|
1300 inc++; |
|
1301 } |
|
1302 |
626
|
1303 if (pr_as_read_syntax) |
1
|
1304 { |
|
1305 for (int i = 0; i < nr; i++) |
|
1306 { |
626
|
1307 int col = 0; |
|
1308 while (col < nc) |
1
|
1309 { |
626
|
1310 int lim = col + inc < nc ? col + inc : nc; |
|
1311 |
|
1312 for (int j = col; j < lim; j++) |
|
1313 { |
|
1314 if (i == 0 && j == 0) |
|
1315 os << "[ "; |
|
1316 else |
|
1317 { |
|
1318 if (j > col && j < lim) |
|
1319 os << ", "; |
|
1320 else |
|
1321 os << " "; |
|
1322 } |
|
1323 |
|
1324 pr_complex (os, cm.elem (i, j)); |
|
1325 } |
|
1326 |
|
1327 col += inc; |
|
1328 |
|
1329 if (col >= nc) |
|
1330 { |
|
1331 if (i == nr - 1) |
|
1332 os << " ]"; |
|
1333 else |
|
1334 os << ";\n"; |
|
1335 } |
1
|
1336 else |
626
|
1337 os << " ...\n"; |
1
|
1338 } |
|
1339 } |
626
|
1340 } |
|
1341 else |
|
1342 { |
|
1343 for (int col = 0; col < nc; col += inc) |
|
1344 { |
|
1345 int lim = col + inc < nc ? col + inc : nc; |
|
1346 |
1186
|
1347 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1348 |
|
1349 for (int i = 0; i < nr; i++) |
|
1350 { |
|
1351 for (int j = col; j < lim; j++) |
|
1352 { |
|
1353 os << " "; |
|
1354 |
|
1355 pr_complex (os, cm.elem (i, j)); |
|
1356 } |
|
1357 os << "\n"; |
|
1358 } |
|
1359 } |
1
|
1360 } |
|
1361 } |
|
1362 } |
|
1363 |
|
1364 void |
626
|
1365 octave_print_internal (ostream& os, const Range& r, |
|
1366 int pr_as_read_syntax) |
1
|
1367 { |
626
|
1368 double base = r.base (); |
1
|
1369 double increment = r.inc (); |
626
|
1370 double limit = r.limit (); |
1
|
1371 int num_elem = r.nelem (); |
|
1372 |
626
|
1373 if (plus_format && ! pr_as_read_syntax) |
1
|
1374 { |
|
1375 os << " "; |
|
1376 for (int i = 0; i < num_elem; i++) |
|
1377 { |
626
|
1378 double val = base + i * increment; |
1
|
1379 if (val == 0.0) |
|
1380 os << " "; |
|
1381 else |
|
1382 os << "+"; |
|
1383 } |
|
1384 } |
|
1385 else |
|
1386 { |
|
1387 int fw; |
|
1388 set_format (r, fw); |
|
1389 |
626
|
1390 if (pr_as_read_syntax) |
1
|
1391 { |
626
|
1392 |
|
1393 if (free_format) |
|
1394 { |
|
1395 os << base << " : "; |
|
1396 if (increment != 1.0) |
|
1397 os << increment << " : "; |
|
1398 os << limit; |
|
1399 } |
|
1400 else |
|
1401 { |
|
1402 pr_float (os, base, fw); |
|
1403 os << " : "; |
|
1404 if (increment != 1.0) |
|
1405 { |
|
1406 pr_float (os, increment, fw); |
|
1407 os << " : "; |
|
1408 } |
|
1409 pr_float (os, limit, fw); |
|
1410 } |
1
|
1411 } |
626
|
1412 else |
|
1413 { |
|
1414 int column_width = fw + 2; |
|
1415 int total_width = num_elem * column_width; |
|
1416 int max_width = terminal_columns (); |
1
|
1417 |
626
|
1418 if (free_format) |
|
1419 { |
|
1420 os << r; |
|
1421 return; |
|
1422 } |
1
|
1423 |
626
|
1424 int inc = num_elem; |
1
|
1425 if (total_width > max_width && user_pref.split_long_rows) |
|
1426 { |
626
|
1427 inc = max_width / column_width; |
|
1428 if (inc == 0) |
|
1429 inc++; |
1
|
1430 } |
|
1431 |
626
|
1432 int col = 0; |
|
1433 while (col < num_elem) |
1
|
1434 { |
626
|
1435 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1436 |
1186
|
1437 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1438 |
|
1439 for (int i = col; i < lim; i++) |
|
1440 { |
|
1441 double val = base + i * increment; |
|
1442 os << " "; |
|
1443 pr_float (os, val, fw); |
|
1444 } |
|
1445 |
|
1446 os << "\n"; |
|
1447 |
|
1448 col += inc; |
1
|
1449 } |
|
1450 } |
|
1451 } |
|
1452 } |
|
1453 |
712
|
1454 DEFUN ("disp", Fdisp, Sdisp, 1, 1, |
529
|
1455 "disp (X): display value without name tag") |
|
1456 { |
|
1457 Octave_object retval; |
|
1458 |
|
1459 int nargin = args.length (); |
|
1460 |
712
|
1461 if (nargin == 1) |
|
1462 args(0).eval (1); |
529
|
1463 else |
|
1464 print_usage ("disp"); |
|
1465 |
|
1466 return retval; |
|
1467 } |
|
1468 |
1
|
1469 static void |
|
1470 init_format_state (void) |
|
1471 { |
|
1472 free_format = 0; |
|
1473 plus_format = 0; |
|
1474 bank_format = 0; |
1282
|
1475 hex_format = 0; |
1309
|
1476 bit_format = 0; |
1
|
1477 print_e = 0; |
|
1478 print_big_e = 0; |
|
1479 } |
|
1480 |
|
1481 static void |
|
1482 set_output_prec_and_fw (int prec, int fw) |
|
1483 { |
529
|
1484 tree_constant *tmp = 0; |
1
|
1485 |
|
1486 tmp = new tree_constant ((double) prec); |
195
|
1487 bind_builtin_variable ("output_precision", tmp); |
1
|
1488 |
|
1489 tmp = new tree_constant ((double) fw); |
195
|
1490 bind_builtin_variable ("output_max_field_width", tmp); |
1
|
1491 } |
|
1492 |
|
1493 void |
|
1494 set_format_style (int argc, char **argv) |
|
1495 { |
|
1496 if (--argc > 0) |
|
1497 { |
|
1498 argv++; |
|
1499 if (*argv[0]) |
|
1500 { |
|
1501 if (strcmp (*argv, "short") == 0) |
|
1502 { |
|
1503 if (--argc > 0) |
|
1504 { |
|
1505 argv++; |
|
1506 if (strcmp (*argv, "e") == 0) |
|
1507 { |
|
1508 init_format_state (); |
|
1509 print_e = 1; |
|
1510 } |
|
1511 else if (strcmp (*argv, "E") == 0) |
|
1512 { |
|
1513 init_format_state (); |
|
1514 print_e = 1; |
|
1515 print_big_e = 1; |
|
1516 } |
|
1517 else |
|
1518 { |
217
|
1519 error ("format: unrecognized option `short %s'", *argv); |
1
|
1520 return; |
|
1521 } |
|
1522 } |
|
1523 else |
|
1524 init_format_state (); |
|
1525 |
|
1526 set_output_prec_and_fw (3, 8); |
|
1527 } |
|
1528 else if (strcmp (*argv, "long") == 0) |
|
1529 { |
|
1530 if (--argc > 0) |
|
1531 { |
|
1532 argv++; |
|
1533 if (strcmp (*argv, "e") == 0) |
|
1534 { |
|
1535 init_format_state (); |
|
1536 print_e = 1; |
|
1537 } |
|
1538 else if (strcmp (*argv, "E") == 0) |
|
1539 { |
|
1540 init_format_state (); |
|
1541 print_e = 1; |
|
1542 print_big_e = 1; |
|
1543 } |
|
1544 else |
|
1545 { |
217
|
1546 error ("format: unrecognized option `long %s'", *argv); |
1
|
1547 return; |
|
1548 } |
|
1549 } |
|
1550 else |
|
1551 init_format_state (); |
|
1552 |
|
1553 set_output_prec_and_fw (15, 24); |
|
1554 } |
|
1555 else if (strcmp (*argv, "hex") == 0) |
1282
|
1556 { |
|
1557 init_format_state (); |
|
1558 hex_format = 1; |
|
1559 } |
|
1560 else if (strcmp (*argv, "native-hex") == 0) |
|
1561 { |
|
1562 init_format_state (); |
|
1563 hex_format = 2; |
|
1564 } |
1309
|
1565 else if (strcmp (*argv, "bit") == 0) |
|
1566 { |
|
1567 init_format_state (); |
|
1568 bit_format = 1; |
|
1569 } |
|
1570 else if (strcmp (*argv, "native-bit") == 0) |
|
1571 { |
|
1572 init_format_state (); |
|
1573 bit_format = 2; |
|
1574 } |
1282
|
1575 else if (strcmp (*argv, "+") == 0 || strcmp (*argv, "plus") == 0) |
1
|
1576 { |
|
1577 init_format_state (); |
|
1578 plus_format = 1; |
|
1579 } |
|
1580 else if (strcmp (*argv, "bank") == 0) |
|
1581 { |
|
1582 init_format_state (); |
|
1583 bank_format = 1; |
|
1584 } |
|
1585 else if (strcmp (*argv, "free") == 0) |
|
1586 { |
|
1587 init_format_state (); |
|
1588 free_format = 1; |
|
1589 } |
|
1590 else if (strcmp (*argv, "none") == 0) |
|
1591 { |
|
1592 init_format_state (); |
|
1593 free_format = 1; |
|
1594 } |
|
1595 else if (strcmp (*argv, "compact") == 0) |
1186
|
1596 { |
|
1597 compact_format = 1; |
|
1598 } |
1
|
1599 else if (strcmp (*argv, "loose") == 0) |
1186
|
1600 { |
|
1601 compact_format = 0; |
|
1602 } |
1
|
1603 else |
217
|
1604 error ("format: unrecognized format state `%s'", *argv); |
1
|
1605 } |
|
1606 else |
|
1607 usage ("format [format_state]"); |
|
1608 } |
|
1609 else |
|
1610 { |
|
1611 init_format_state (); |
|
1612 set_output_prec_and_fw (5, 10); |
|
1613 } |
|
1614 } |
|
1615 |
529
|
1616 DEFUN_TEXT ("format", Fformat, Sformat, -1, 1, |
|
1617 "format [style]\n\ |
|
1618 \n\ |
|
1619 set output formatting style") |
|
1620 { |
|
1621 Octave_object retval; |
|
1622 |
|
1623 DEFINE_ARGV("format"); |
|
1624 |
|
1625 set_format_style (argc, argv); |
|
1626 |
|
1627 DELETE_ARGV; |
|
1628 |
|
1629 return retval; |
|
1630 } |
|
1631 |
1
|
1632 /* |
|
1633 ;;; Local Variables: *** |
|
1634 ;;; mode: C++ *** |
|
1635 ;;; page-delimiter: "^/\\*" *** |
|
1636 ;;; End: *** |
|
1637 */ |