1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1346
|
27 #include <cfloat> |
3124
|
28 #include <cmath> |
2825
|
29 #include <cstdio> |
1346
|
30 #include <cstring> |
1343
|
31 |
3503
|
32 #include <iomanip> |
|
33 #include <iostream> |
|
34 #include <strstream> |
1728
|
35 #include <string> |
|
36 |
453
|
37 #include "CMatrix.h" |
1
|
38 #include "Range.h" |
2926
|
39 #include "cmd-edit.h" |
1352
|
40 #include "dMatrix.h" |
2891
|
41 #include "lo-mappers.h" |
2317
|
42 #include "mach-info.h" |
1651
|
43 #include "oct-cmplx.h" |
1755
|
44 #include "str-vec.h" |
1
|
45 |
1352
|
46 #include "defun.h" |
|
47 #include "error.h" |
2165
|
48 #include "gripes.h" |
1755
|
49 #include "oct-obj.h" |
1352
|
50 #include "pager.h" |
|
51 #include "pr-output.h" |
1282
|
52 #include "sysdep.h" |
1
|
53 #include "utils.h" |
1352
|
54 #include "variables.h" |
1
|
55 |
3105
|
56 // TRUE means use a scaled fixed point format for `format long' and |
|
57 // `format short'. |
|
58 static bool Vfixed_point_format; |
|
59 |
2165
|
60 // The maximum field width for a number printed by the default output |
|
61 // routines. |
|
62 static int Voutput_max_field_width; |
|
63 |
|
64 // The precision of the numbers printed by the default output |
|
65 // routines. |
|
66 static int Voutput_precision; |
|
67 |
|
68 // TRUE means that the dimensions of empty matrices should be printed |
|
69 // like this: x = [](2x0). |
|
70 static bool Vprint_empty_dimensions; |
|
71 |
|
72 // TRUE means that the rows of big matrices should be split into |
|
73 // smaller slices that fit on the screen. |
|
74 static bool Vsplit_long_rows; |
|
75 |
3018
|
76 // TRUE means don't do any fancy formatting. |
2387
|
77 static bool free_format = false; |
1
|
78 |
3018
|
79 // TRUE means print plus sign for nonzero, blank for zero. |
2387
|
80 static bool plus_format = false; |
1
|
81 |
3018
|
82 // TRUE means always print like dollars and cents. |
2387
|
83 static bool bank_format = false; |
1282
|
84 |
3018
|
85 // TRUE means print data in hexadecimal format. |
3608
|
86 static int hex_format = 0; |
1282
|
87 |
3018
|
88 // TRUE means print data in binary-bit-pattern format. |
1309
|
89 static int bit_format = 0; |
|
90 |
3018
|
91 // TRUE means don't put newlines around the column number headers. |
2387
|
92 static bool compact_format = false; |
1186
|
93 |
3018
|
94 // TRUE means use an e format. |
2387
|
95 static bool print_e = false; |
1
|
96 |
3018
|
97 // TRUE means print E instead of e for exponent field. |
2387
|
98 static bool print_big_e = false; |
1
|
99 |
3608
|
100 class pr_formatted_float; |
|
101 |
|
102 class |
|
103 float_format |
|
104 { |
|
105 public: |
|
106 |
3682
|
107 float_format (int w = -1, int p = -1, int f = 0) |
3608
|
108 : fw (w), prec (p), fmt (f), up (0), sp (0) { } |
|
109 |
|
110 float_format (const float_format& ff) |
|
111 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { } |
|
112 |
|
113 float_format& operator = (const float_format& ff) |
|
114 { |
|
115 if (&ff != this) |
|
116 { |
|
117 fw = ff.fw; |
|
118 prec = ff.prec; |
|
119 fmt = ff.fmt; |
|
120 up = ff.up; |
|
121 sp = ff.sp; |
|
122 } |
|
123 |
|
124 return *this; |
|
125 } |
|
126 |
|
127 ~float_format (void) { } |
|
128 |
|
129 float_format& scientific (void) { fmt = std::ios::scientific; return *this; } |
|
130 float_format& fixed (void) { fmt = std::ios::fixed; return *this; } |
|
131 float_format& general (void) { fmt = 0; return *this; } |
|
132 |
|
133 float_format& uppercase (void) { up = std::ios::uppercase; return *this; } |
|
134 float_format& lowercase (void) { up = 0; return *this; } |
|
135 |
|
136 float_format& precision (int p) { prec = p; return *this; } |
|
137 |
|
138 float_format& width (int w) { fw = w; return *this; } |
|
139 |
|
140 float_format& trailing_zeros (bool tz = true) |
|
141 { sp = tz ? std::ios::showpoint : 0; return *this; } |
|
142 |
|
143 friend std::ostream& operator << (std::ostream& os, |
|
144 const pr_formatted_float& pff); |
|
145 |
|
146 private: |
|
147 |
|
148 // Field width. Zero means as wide as necessary. |
|
149 int fw; |
|
150 |
|
151 // Precision. |
|
152 int prec; |
|
153 |
|
154 // Format. |
|
155 int fmt; |
|
156 |
|
157 // E or e. |
|
158 int up; |
|
159 |
|
160 // Show trailing zeros. |
|
161 int sp; |
|
162 }; |
|
163 |
|
164 class |
|
165 pr_formatted_float |
|
166 { |
|
167 public: |
|
168 |
|
169 const float_format& f; |
|
170 |
|
171 double val; |
|
172 |
|
173 pr_formatted_float (const float_format& f_arg, double val_arg) |
|
174 : f (f_arg), val (val_arg) { } |
|
175 }; |
|
176 |
|
177 std::ostream& |
|
178 operator << (std::ostream& os, const pr_formatted_float& pff) |
|
179 { |
3682
|
180 if (pff.f.fw >= 0) |
3608
|
181 os << std::setw (pff.f.fw); |
|
182 |
3682
|
183 if (pff.f.prec >= 0) |
3608
|
184 os << std::setprecision (pff.f.prec); |
|
185 |
|
186 std::ios::fmtflags oflags = os.flags (pff.f.fmt | pff.f.up | pff.f.sp); |
|
187 |
|
188 os << pff.val; |
|
189 |
|
190 os.flags (oflags); |
|
191 |
|
192 return os; |
|
193 } |
|
194 |
|
195 // Current format for real numbers and the real part of complex |
|
196 // numbers. |
|
197 static float_format *curr_real_fmt = 0; |
|
198 |
|
199 // Current format for the imaginary part of complex numbers. |
|
200 static float_format *curr_imag_fmt = 0; |
1309
|
201 |
1
|
202 static double |
164
|
203 pr_max_internal (const Matrix& m) |
1
|
204 { |
|
205 int nr = m.rows (); |
|
206 int nc = m.columns (); |
|
207 |
3130
|
208 double result = -DBL_MAX; |
1
|
209 |
|
210 for (int j = 0; j < nc; j++) |
|
211 for (int i = 0; i < nr; i++) |
|
212 { |
3608
|
213 double val = m(i,j); |
1
|
214 if (xisinf (val) || xisnan (val)) |
|
215 continue; |
|
216 |
|
217 if (val > result) |
|
218 result = val; |
|
219 } |
3608
|
220 |
1
|
221 return result; |
|
222 } |
|
223 |
|
224 static double |
164
|
225 pr_min_internal (const Matrix& m) |
1
|
226 { |
|
227 int nr = m.rows (); |
|
228 int nc = m.columns (); |
|
229 |
|
230 double result = DBL_MAX; |
|
231 |
|
232 for (int j = 0; j < nc; j++) |
|
233 for (int i = 0; i < nr; i++) |
|
234 { |
3608
|
235 double val = m(i,j); |
1
|
236 if (xisinf (val) || xisnan (val)) |
|
237 continue; |
|
238 |
|
239 if (val < result) |
|
240 result = val; |
|
241 } |
3608
|
242 |
1
|
243 return result; |
|
244 } |
|
245 |
1658
|
246 // XXX FIXME XXX -- it would be nice to share more code among these |
|
247 // functions,.. |
|
248 |
1
|
249 static void |
3611
|
250 set_real_format (bool sign, int digits, bool inf_or_nan, bool int_only, |
1658
|
251 int &fw) |
1
|
252 { |
3608
|
253 static float_format fmt; |
1
|
254 |
2165
|
255 int prec = Voutput_precision; |
1
|
256 |
|
257 int ld, rd; |
|
258 |
|
259 if (bank_format) |
|
260 { |
|
261 fw = digits < 0 ? 4 : digits + 3; |
|
262 if (inf_or_nan && fw < 3) |
|
263 fw = 3; |
|
264 fw += sign; |
|
265 rd = 2; |
|
266 } |
1282
|
267 else if (hex_format) |
|
268 { |
|
269 fw = 2 * sizeof (double); |
|
270 rd = 0; |
|
271 } |
1309
|
272 else if (bit_format) |
|
273 { |
|
274 fw = 8 * sizeof (double); |
|
275 rd = 0; |
|
276 } |
3611
|
277 else if (inf_or_nan || int_only) |
1
|
278 { |
|
279 fw = digits; |
|
280 if (inf_or_nan && fw < 3) |
|
281 fw = 3; |
|
282 fw += sign; |
3682
|
283 rd = fw; |
1
|
284 } |
|
285 else |
|
286 { |
|
287 if (digits > 0) |
|
288 { |
|
289 ld = digits; |
1658
|
290 rd = prec > digits ? prec - digits : prec; |
1
|
291 digits++; |
|
292 } |
|
293 else |
|
294 { |
|
295 ld = 1; |
1658
|
296 rd = prec > digits ? prec - digits : prec; |
1
|
297 digits = -digits + 1; |
|
298 } |
|
299 |
|
300 fw = ld + 1 + rd; |
|
301 if (inf_or_nan && fw < 3) |
|
302 fw = 3; |
|
303 fw += sign; |
|
304 } |
|
305 |
1309
|
306 if (! (bank_format || hex_format || bit_format) |
2165
|
307 && (fw > Voutput_max_field_width || print_e)) |
1
|
308 { |
|
309 int exp_field = 4; |
|
310 if (digits > 100) |
|
311 exp_field++; |
|
312 |
|
313 fw = 2 + prec + exp_field; |
|
314 if (inf_or_nan && fw < 3) |
|
315 fw = 3; |
|
316 fw += sign; |
|
317 |
3608
|
318 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
319 |
1
|
320 if (print_big_e) |
3608
|
321 fmt.uppercase (); |
1
|
322 } |
3611
|
323 else if (inf_or_nan || int_only) |
|
324 fmt = float_format (fw, rd); |
1
|
325 else |
3608
|
326 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
327 |
3608
|
328 curr_real_fmt = &fmt; |
1
|
329 } |
|
330 |
1658
|
331 static void |
|
332 set_format (double d, int& fw) |
|
333 { |
|
334 curr_real_fmt = 0; |
|
335 curr_imag_fmt = 0; |
|
336 |
|
337 if (free_format) |
|
338 return; |
|
339 |
2387
|
340 bool sign = (d < 0.0); |
1658
|
341 |
2387
|
342 bool inf_or_nan = (xisinf (d) || xisnan (d)); |
1658
|
343 |
3611
|
344 bool int_only = (! inf_or_nan && D_NINT (d) == d); |
1658
|
345 |
|
346 double d_abs = d < 0.0 ? -d : d; |
|
347 |
2800
|
348 int digits = (inf_or_nan || d_abs == 0.0) |
|
349 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0)); |
1658
|
350 |
3611
|
351 set_real_format (sign, digits, inf_or_nan, int_only, fw); |
1658
|
352 } |
|
353 |
1
|
354 static inline void |
|
355 set_format (double d) |
|
356 { |
|
357 int fw; |
|
358 set_format (d, fw); |
|
359 } |
|
360 |
|
361 static void |
2387
|
362 set_real_matrix_format (bool sign, int x_max, int x_min, |
|
363 bool inf_or_nan, int int_or_inf_or_nan, int& fw) |
1
|
364 { |
3608
|
365 static float_format fmt; |
1
|
366 |
2165
|
367 int prec = Voutput_precision; |
1
|
368 |
|
369 int ld, rd; |
|
370 |
|
371 if (bank_format) |
|
372 { |
|
373 int digits = x_max > x_min ? x_max : x_min; |
|
374 fw = digits <= 0 ? 4 : digits + 3; |
|
375 if (inf_or_nan && fw < 3) |
|
376 fw = 3; |
|
377 fw += sign; |
|
378 rd = 2; |
|
379 } |
1282
|
380 else if (hex_format) |
|
381 { |
|
382 fw = 2 * sizeof (double); |
|
383 rd = 0; |
|
384 } |
1309
|
385 else if (bit_format) |
|
386 { |
|
387 fw = 8 * sizeof (double); |
|
388 rd = 0; |
|
389 } |
3268
|
390 else if (Vfixed_point_format) |
|
391 { |
|
392 rd = prec; |
|
393 fw = rd + 2; |
|
394 if (inf_or_nan && fw < 3) |
|
395 fw = 3; |
|
396 fw += sign; |
|
397 } |
1715
|
398 else if (int_or_inf_or_nan) |
1
|
399 { |
|
400 int digits = x_max > x_min ? x_max : x_min; |
|
401 fw = digits <= 0 ? 1 : digits; |
|
402 if (inf_or_nan && fw < 3) |
|
403 fw = 3; |
|
404 fw += sign; |
3682
|
405 rd = fw; |
1
|
406 } |
|
407 else |
|
408 { |
|
409 int ld_max, rd_max; |
|
410 if (x_max > 0) |
|
411 { |
|
412 ld_max = x_max; |
1658
|
413 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
414 x_max++; |
|
415 } |
|
416 else |
|
417 { |
|
418 ld_max = 1; |
1658
|
419 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
420 x_max = -x_max + 1; |
|
421 } |
|
422 |
|
423 int ld_min, rd_min; |
|
424 if (x_min > 0) |
|
425 { |
|
426 ld_min = x_min; |
1658
|
427 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
428 x_min++; |
|
429 } |
|
430 else |
|
431 { |
|
432 ld_min = 1; |
1658
|
433 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
434 x_min = -x_min + 1; |
|
435 } |
|
436 |
|
437 ld = ld_max > ld_min ? ld_max : ld_min; |
|
438 rd = rd_max > rd_min ? rd_max : rd_min; |
|
439 |
|
440 fw = ld + 1 + rd; |
|
441 if (inf_or_nan && fw < 3) |
|
442 fw = 3; |
|
443 fw += sign; |
|
444 } |
|
445 |
1658
|
446 if (! (bank_format || hex_format || bit_format) |
3105
|
447 && (print_e |
|
448 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
449 { |
|
450 int exp_field = 4; |
|
451 if (x_max > 100 || x_min > 100) |
|
452 exp_field++; |
|
453 |
|
454 fw = 2 + prec + exp_field; |
|
455 if (inf_or_nan && fw < 3) |
|
456 fw = 3; |
|
457 fw += sign; |
|
458 |
3608
|
459 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
460 |
1
|
461 if (print_big_e) |
3608
|
462 fmt.uppercase (); |
1
|
463 } |
3611
|
464 else if (int_or_inf_or_nan) |
|
465 fmt = float_format (fw, rd); |
1
|
466 else |
3608
|
467 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
468 |
3608
|
469 curr_real_fmt = &fmt; |
1
|
470 } |
|
471 |
1658
|
472 static void |
3105
|
473 set_format (const Matrix& m, int& fw, double& scale) |
1658
|
474 { |
|
475 curr_real_fmt = 0; |
|
476 curr_imag_fmt = 0; |
|
477 |
|
478 if (free_format) |
|
479 return; |
|
480 |
2387
|
481 bool sign = m.any_element_is_negative (); |
1658
|
482 |
2387
|
483 bool inf_or_nan = m.any_element_is_inf_or_nan (); |
1658
|
484 |
2387
|
485 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan (); |
1658
|
486 |
2387
|
487 Matrix m_abs = m.abs (); |
1658
|
488 double max_abs = pr_max_internal (m_abs); |
|
489 double min_abs = pr_min_internal (m_abs); |
|
490 |
2800
|
491 int x_max = max_abs == 0.0 |
|
492 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
493 |
|
494 int x_min = min_abs == 0.0 |
|
495 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
496 |
3164
|
497 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : pow (10.0, x_max - 1); |
3105
|
498 |
1658
|
499 set_real_matrix_format (sign, x_max, x_min, inf_or_nan, |
1715
|
500 int_or_inf_or_nan, fw); |
1658
|
501 } |
|
502 |
1
|
503 static inline void |
164
|
504 set_format (const Matrix& m) |
1
|
505 { |
|
506 int fw; |
3105
|
507 double scale; |
|
508 set_format (m, fw, scale); |
1
|
509 } |
|
510 |
|
511 static void |
2387
|
512 set_complex_format (bool sign, int x_max, int x_min, int r_x, |
|
513 bool inf_or_nan, int int_only, int& r_fw, int& i_fw) |
1
|
514 { |
3608
|
515 static float_format r_fmt; |
|
516 static float_format i_fmt; |
1
|
517 |
2165
|
518 int prec = Voutput_precision; |
1
|
519 |
|
520 int ld, rd; |
|
521 |
|
522 if (bank_format) |
|
523 { |
|
524 int digits = r_x; |
|
525 i_fw = 0; |
|
526 r_fw = digits <= 0 ? 4 : digits + 3; |
|
527 if (inf_or_nan && r_fw < 3) |
|
528 r_fw = 3; |
|
529 r_fw += sign; |
|
530 rd = 2; |
|
531 } |
1282
|
532 else if (hex_format) |
|
533 { |
|
534 r_fw = 2 * sizeof (double); |
|
535 i_fw = 2 * sizeof (double); |
|
536 rd = 0; |
|
537 } |
1309
|
538 else if (bit_format) |
|
539 { |
|
540 r_fw = 8 * sizeof (double); |
|
541 i_fw = 8 * sizeof (double); |
|
542 rd = 0; |
|
543 } |
1658
|
544 else if (inf_or_nan || int_only) |
1
|
545 { |
|
546 int digits = x_max > x_min ? x_max : x_min; |
|
547 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
548 if (inf_or_nan && i_fw < 3) |
|
549 i_fw = r_fw = 3; |
|
550 r_fw += sign; |
3682
|
551 rd = r_fw; |
1
|
552 } |
|
553 else |
|
554 { |
|
555 int ld_max, rd_max; |
|
556 if (x_max > 0) |
|
557 { |
|
558 ld_max = x_max; |
1658
|
559 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
560 x_max++; |
|
561 } |
|
562 else |
|
563 { |
|
564 ld_max = 1; |
1658
|
565 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
566 x_max = -x_max + 1; |
|
567 } |
|
568 |
|
569 int ld_min, rd_min; |
|
570 if (x_min > 0) |
|
571 { |
|
572 ld_min = x_min; |
1658
|
573 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
574 x_min++; |
|
575 } |
|
576 else |
|
577 { |
|
578 ld_min = 1; |
1658
|
579 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
580 x_min = -x_min + 1; |
|
581 } |
|
582 |
|
583 ld = ld_max > ld_min ? ld_max : ld_min; |
|
584 rd = rd_max > rd_min ? rd_max : rd_min; |
|
585 |
|
586 i_fw = r_fw = ld + 1 + rd; |
|
587 if (inf_or_nan && i_fw < 3) |
|
588 i_fw = r_fw = 3; |
|
589 r_fw += sign; |
|
590 } |
|
591 |
1309
|
592 if (! (bank_format || hex_format || bit_format) |
2165
|
593 && (r_fw > Voutput_max_field_width || print_e)) |
1
|
594 { |
|
595 int exp_field = 4; |
|
596 if (x_max > 100 || x_min > 100) |
|
597 exp_field++; |
|
598 |
|
599 i_fw = r_fw = 1 + prec + exp_field; |
|
600 if (inf_or_nan && i_fw < 3) |
|
601 i_fw = r_fw = 3; |
|
602 r_fw += sign; |
|
603 |
3608
|
604 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
605 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
606 |
1
|
607 if (print_big_e) |
|
608 { |
3608
|
609 r_fmt.uppercase (); |
|
610 i_fmt.uppercase (); |
1
|
611 } |
|
612 } |
3611
|
613 else if (inf_or_nan || int_only) |
|
614 { |
|
615 r_fmt = float_format (r_fw, rd); |
|
616 i_fmt = float_format (i_fw, rd); |
|
617 } |
1
|
618 else |
|
619 { |
3608
|
620 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
621 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
622 } |
|
623 |
3608
|
624 curr_real_fmt = &r_fmt; |
|
625 curr_imag_fmt = &i_fmt; |
1
|
626 } |
|
627 |
1658
|
628 static void |
|
629 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
630 { |
|
631 curr_real_fmt = 0; |
|
632 curr_imag_fmt = 0; |
|
633 |
|
634 if (free_format) |
|
635 return; |
|
636 |
|
637 double rp = c.real (); |
|
638 double ip = c.imag (); |
|
639 |
2387
|
640 bool sign = (rp < 0.0); |
1658
|
641 |
2387
|
642 bool inf_or_nan = (xisinf (c) || xisnan (c)); |
1658
|
643 |
2387
|
644 bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
1658
|
645 |
|
646 double r_abs = rp < 0.0 ? -rp : rp; |
|
647 double i_abs = ip < 0.0 ? -ip : ip; |
|
648 |
2800
|
649 int r_x = r_abs == 0.0 |
|
650 ? 0 : static_cast<int> (floor (log10 (r_abs) + 1.0)); |
|
651 |
|
652 int i_x = i_abs == 0.0 |
|
653 ? 0 : static_cast<int> (floor (log10 (i_abs) + 1.0)); |
1658
|
654 |
|
655 int x_max, x_min; |
|
656 |
|
657 if (r_x > i_x) |
|
658 { |
|
659 x_max = r_x; |
|
660 x_min = i_x; |
|
661 } |
|
662 else |
|
663 { |
|
664 x_max = i_x; |
|
665 x_min = r_x; |
|
666 } |
|
667 |
|
668 set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only, |
|
669 r_fw, i_fw); |
|
670 } |
|
671 |
1
|
672 static inline void |
164
|
673 set_format (const Complex& c) |
1
|
674 { |
|
675 int r_fw, i_fw; |
|
676 set_format (c, r_fw, i_fw); |
|
677 } |
|
678 |
|
679 static void |
2387
|
680 set_complex_matrix_format (bool sign, int x_max, int x_min, |
|
681 int r_x_max, int r_x_min, bool inf_or_nan, |
1715
|
682 int int_or_inf_or_nan, int& r_fw, int& i_fw) |
1
|
683 { |
3608
|
684 static float_format r_fmt; |
|
685 static float_format i_fmt; |
1
|
686 |
2165
|
687 int prec = Voutput_precision; |
1
|
688 |
|
689 int ld, rd; |
|
690 |
|
691 if (bank_format) |
|
692 { |
|
693 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
694 i_fw = 0; |
|
695 r_fw = digits <= 0 ? 4 : digits + 3; |
|
696 if (inf_or_nan && i_fw < 3) |
|
697 i_fw = r_fw = 3; |
|
698 r_fw += sign; |
|
699 rd = 2; |
|
700 } |
1282
|
701 else if (hex_format) |
|
702 { |
|
703 r_fw = 2 * sizeof (double); |
|
704 i_fw = 2 * sizeof (double); |
|
705 rd = 0; |
|
706 } |
1309
|
707 else if (bit_format) |
|
708 { |
|
709 r_fw = 8 * sizeof (double); |
|
710 i_fw = 8 * sizeof (double); |
|
711 rd = 0; |
|
712 } |
3268
|
713 else if (Vfixed_point_format) |
|
714 { |
|
715 rd = prec; |
|
716 i_fw = r_fw = rd + 2; |
|
717 if (inf_or_nan && i_fw < 3) |
|
718 i_fw = r_fw = 3; |
|
719 r_fw += sign; |
|
720 } |
1715
|
721 else if (int_or_inf_or_nan) |
1
|
722 { |
|
723 int digits = x_max > x_min ? x_max : x_min; |
|
724 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
725 if (inf_or_nan && i_fw < 3) |
|
726 i_fw = r_fw = 3; |
|
727 r_fw += sign; |
3682
|
728 rd = r_fw; |
1
|
729 } |
|
730 else |
|
731 { |
|
732 int ld_max, rd_max; |
|
733 if (x_max > 0) |
|
734 { |
|
735 ld_max = x_max; |
1658
|
736 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
737 x_max++; |
|
738 } |
|
739 else |
|
740 { |
|
741 ld_max = 1; |
1658
|
742 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
743 x_max = -x_max + 1; |
|
744 } |
|
745 |
|
746 int ld_min, rd_min; |
|
747 if (x_min > 0) |
|
748 { |
|
749 ld_min = x_min; |
1658
|
750 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
751 x_min++; |
|
752 } |
|
753 else |
|
754 { |
|
755 ld_min = 1; |
1658
|
756 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
757 x_min = -x_min + 1; |
|
758 } |
|
759 |
|
760 ld = ld_max > ld_min ? ld_max : ld_min; |
|
761 rd = rd_max > rd_min ? rd_max : rd_min; |
|
762 |
|
763 i_fw = r_fw = ld + 1 + rd; |
|
764 if (inf_or_nan && i_fw < 3) |
|
765 i_fw = r_fw = 3; |
|
766 r_fw += sign; |
|
767 } |
|
768 |
1309
|
769 if (! (bank_format || hex_format || bit_format) |
3105
|
770 && (print_e |
|
771 || (! Vfixed_point_format && r_fw > Voutput_max_field_width))) |
1
|
772 { |
|
773 int exp_field = 4; |
|
774 if (x_max > 100 || x_min > 100) |
|
775 exp_field++; |
|
776 |
|
777 i_fw = r_fw = 1 + prec + exp_field; |
|
778 if (inf_or_nan && i_fw < 3) |
|
779 i_fw = r_fw = 3; |
|
780 r_fw += sign; |
|
781 |
3608
|
782 r_fmt = float_format (r_fw, prec - 1, std::ios::scientific); |
|
783 i_fmt = float_format (i_fw, prec - 1, std::ios::scientific); |
|
784 |
1
|
785 if (print_big_e) |
|
786 { |
3608
|
787 r_fmt.uppercase (); |
|
788 i_fmt.uppercase (); |
1
|
789 } |
|
790 } |
3611
|
791 else if (int_or_inf_or_nan) |
|
792 { |
|
793 r_fmt = float_format (r_fw, rd); |
|
794 i_fmt = float_format (i_fw, rd); |
|
795 } |
1
|
796 else |
|
797 { |
3608
|
798 r_fmt = float_format (r_fw, rd, std::ios::fixed); |
|
799 i_fmt = float_format (i_fw, rd, std::ios::fixed); |
1
|
800 } |
|
801 |
3608
|
802 curr_real_fmt = &r_fmt; |
|
803 curr_imag_fmt = &i_fmt; |
1
|
804 } |
|
805 |
1658
|
806 static void |
3105
|
807 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw, double& scale) |
1658
|
808 { |
|
809 curr_real_fmt = 0; |
|
810 curr_imag_fmt = 0; |
|
811 |
|
812 if (free_format) |
|
813 return; |
|
814 |
|
815 Matrix rp = real (cm); |
|
816 Matrix ip = imag (cm); |
|
817 |
2387
|
818 bool sign = rp.any_element_is_negative (); |
1658
|
819 |
2387
|
820 bool inf_or_nan = cm.any_element_is_inf_or_nan (); |
1658
|
821 |
2387
|
822 bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan () |
|
823 && ip.all_elements_are_int_or_inf_or_nan ()); |
1658
|
824 |
2387
|
825 Matrix r_m_abs = rp.abs (); |
1658
|
826 double r_max_abs = pr_max_internal (r_m_abs); |
|
827 double r_min_abs = pr_min_internal (r_m_abs); |
|
828 |
2387
|
829 Matrix i_m_abs = ip.abs (); |
1658
|
830 double i_max_abs = pr_max_internal (i_m_abs); |
|
831 double i_min_abs = pr_min_internal (i_m_abs); |
|
832 |
2800
|
833 int r_x_max = r_max_abs == 0.0 |
|
834 ? 0 : static_cast<int> (floor (log10 (r_max_abs) + 1.0)); |
|
835 |
|
836 int r_x_min = r_min_abs == 0.0 |
|
837 ? 0 : static_cast<int> (floor (log10 (r_min_abs) + 1.0)); |
1658
|
838 |
2800
|
839 int i_x_max = i_max_abs == 0.0 |
|
840 ? 0 : static_cast<int> (floor (log10 (i_max_abs) + 1.0)); |
|
841 |
|
842 int i_x_min = i_min_abs == 0.0 |
|
843 ? 0 : static_cast<int> (floor (log10 (i_min_abs) + 1.0)); |
1658
|
844 |
|
845 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
846 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
847 |
3164
|
848 scale = (x_max == 0 || int_or_inf_or_nan) ? 1.0 : pow (10.0, x_max - 1); |
3105
|
849 |
1658
|
850 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
1715
|
851 inf_or_nan, int_or_inf_or_nan, r_fw, i_fw); |
1658
|
852 } |
|
853 |
1
|
854 static inline void |
164
|
855 set_format (const ComplexMatrix& cm) |
1
|
856 { |
|
857 int r_fw, i_fw; |
3105
|
858 double scale; |
|
859 set_format (cm, r_fw, i_fw, scale); |
1
|
860 } |
|
861 |
|
862 static void |
3608
|
863 set_range_format (bool sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
864 { |
3608
|
865 static float_format fmt; |
1
|
866 |
2165
|
867 int prec = Voutput_precision; |
1
|
868 |
|
869 int ld, rd; |
|
870 |
|
871 if (bank_format) |
|
872 { |
|
873 int digits = x_max > x_min ? x_max : x_min; |
|
874 fw = sign + digits < 0 ? 4 : digits + 3; |
|
875 rd = 2; |
|
876 } |
1282
|
877 else if (hex_format) |
|
878 { |
|
879 fw = 2 * sizeof (double); |
|
880 rd = 0; |
|
881 } |
1309
|
882 else if (bit_format) |
|
883 { |
|
884 fw = 8 * sizeof (double); |
|
885 rd = 0; |
|
886 } |
1658
|
887 else if (all_ints) |
1
|
888 { |
|
889 int digits = x_max > x_min ? x_max : x_min; |
|
890 fw = sign + digits; |
3682
|
891 rd = fw; |
1
|
892 } |
3105
|
893 else if (Vfixed_point_format) |
|
894 { |
|
895 rd = prec; |
|
896 fw = rd + 2 + sign; |
|
897 } |
1
|
898 else |
|
899 { |
|
900 int ld_max, rd_max; |
|
901 if (x_max > 0) |
|
902 { |
|
903 ld_max = x_max; |
1658
|
904 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
905 x_max++; |
|
906 } |
|
907 else |
|
908 { |
|
909 ld_max = 1; |
1658
|
910 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
911 x_max = -x_max + 1; |
|
912 } |
|
913 |
|
914 int ld_min, rd_min; |
|
915 if (x_min > 0) |
|
916 { |
|
917 ld_min = x_min; |
1658
|
918 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
919 x_min++; |
|
920 } |
|
921 else |
|
922 { |
|
923 ld_min = 1; |
1658
|
924 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
925 x_min = -x_min + 1; |
|
926 } |
|
927 |
|
928 ld = ld_max > ld_min ? ld_max : ld_min; |
|
929 rd = rd_max > rd_min ? rd_max : rd_min; |
|
930 |
|
931 fw = sign + ld + 1 + rd; |
|
932 } |
|
933 |
1309
|
934 if (! (bank_format || hex_format || bit_format) |
3105
|
935 && (print_e |
|
936 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
937 { |
|
938 int exp_field = 4; |
|
939 if (x_max > 100 || x_min > 100) |
|
940 exp_field++; |
|
941 |
|
942 fw = sign + 2 + prec + exp_field; |
|
943 |
3608
|
944 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
945 |
1
|
946 if (print_big_e) |
3608
|
947 fmt.uppercase (); |
1
|
948 } |
3611
|
949 else if (all_ints) |
|
950 fmt = float_format (fw, rd); |
1
|
951 else |
3608
|
952 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
953 |
3608
|
954 curr_real_fmt = &fmt; |
1
|
955 } |
|
956 |
1658
|
957 static void |
3105
|
958 set_format (const Range& r, int& fw, double& scale) |
1658
|
959 { |
|
960 curr_real_fmt = 0; |
|
961 curr_imag_fmt = 0; |
|
962 |
|
963 if (free_format) |
|
964 return; |
|
965 |
|
966 double r_min = r.base (); |
|
967 double r_max = r.limit (); |
|
968 |
|
969 if (r_max < r_min) |
|
970 { |
|
971 double tmp = r_max; |
|
972 r_max = r_min; |
|
973 r_min = tmp; |
|
974 } |
|
975 |
2387
|
976 bool sign = (r_min < 0.0); |
1658
|
977 |
2387
|
978 bool all_ints = r.all_elements_are_ints (); |
1658
|
979 |
|
980 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
981 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
982 |
2800
|
983 int x_max = max_abs == 0.0 |
|
984 ? 0 : static_cast<int> (floor (log10 (max_abs) + 1.0)); |
|
985 |
|
986 int x_min = min_abs == 0.0 |
|
987 ? 0 : static_cast<int> (floor (log10 (min_abs) + 1.0)); |
1658
|
988 |
3164
|
989 scale = (x_max == 0 || all_ints) ? 1.0 : pow (10.0, x_max - 1); |
3105
|
990 |
1658
|
991 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
992 } |
|
993 |
1
|
994 static inline void |
164
|
995 set_format (const Range& r) |
1
|
996 { |
|
997 int fw; |
3105
|
998 double scale; |
|
999 set_format (r, fw, scale); |
1
|
1000 } |
|
1001 |
1282
|
1002 union equiv |
|
1003 { |
|
1004 double d; |
|
1005 unsigned char i[sizeof (double)]; |
|
1006 }; |
|
1007 |
1309
|
1008 #define PRINT_CHAR_BITS(os, c) \ |
|
1009 do \ |
|
1010 { \ |
|
1011 unsigned char ctmp = c; \ |
|
1012 char stmp[9]; \ |
1488
|
1013 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
1014 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1015 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1016 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1017 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1018 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1019 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1020 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
1021 stmp[8] = '\0'; \ |
3013
|
1022 os << stmp; \ |
1309
|
1023 } \ |
|
1024 while (0) |
|
1025 |
|
1026 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
1027 do \ |
|
1028 { \ |
|
1029 unsigned char ctmp = c; \ |
|
1030 char stmp[9]; \ |
1488
|
1031 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
1032 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
1033 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
1034 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
1035 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
1036 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
1037 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
1038 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
1039 stmp[8] = '\0'; \ |
3013
|
1040 os << stmp; \ |
1309
|
1041 } \ |
|
1042 while (0) |
|
1043 |
2522
|
1044 static void |
3608
|
1045 pr_any_float (const float_format *fmt, std::ostream& os, double d, int fw = 0) |
1
|
1046 { |
2522
|
1047 #if defined (SCO) |
|
1048 // Apparently on some SCO systems NaN == -0.0 is true. Compiler bug? |
|
1049 if (d == -0.0 && ! xisnan (d)) |
|
1050 d = 0.0; |
|
1051 #else |
1
|
1052 if (d == -0.0) |
|
1053 d = 0.0; |
2522
|
1054 #endif |
1
|
1055 |
529
|
1056 if (fmt) |
1
|
1057 { |
1282
|
1058 if (hex_format) |
|
1059 { |
|
1060 equiv tmp; |
|
1061 tmp.d = d; |
|
1062 |
|
1063 // Unless explicitly asked for, always print in big-endian |
|
1064 // format. |
|
1065 |
|
1066 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1067 // formats and not for Cray? |
|
1068 |
3013
|
1069 // XXX FIXME XXX -- will bad things happen if we are |
|
1070 // interrupted before resetting the format flags and fill |
|
1071 // character? |
|
1072 |
2317
|
1073 oct_mach_info::float_format flt_fmt = |
|
1074 oct_mach_info::native_float_format (); |
|
1075 |
3013
|
1076 char ofill = os.fill ('0'); |
|
1077 |
3608
|
1078 std::ios::fmtflags oflags |
|
1079 = os.flags (std::ios::right | std::ios::hex); |
3013
|
1080 |
1282
|
1081 if (hex_format > 1 |
2317
|
1082 || flt_fmt == oct_mach_info::ieee_big_endian |
|
1083 || flt_fmt == oct_mach_info::cray |
|
1084 || flt_fmt == oct_mach_info::unknown) |
1282
|
1085 { |
1322
|
1086 for (size_t i = 0; i < sizeof (double); i++) |
3548
|
1087 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1088 } |
|
1089 else |
|
1090 { |
1328
|
1091 for (int i = sizeof (double) - 1; i >= 0; i--) |
3548
|
1092 os << std::setw (2) << static_cast<int> (tmp.i[i]); |
1282
|
1093 } |
3013
|
1094 |
|
1095 os.fill (ofill); |
|
1096 os.setf (oflags); |
1282
|
1097 } |
1309
|
1098 else if (bit_format) |
|
1099 { |
|
1100 equiv tmp; |
|
1101 tmp.d = d; |
|
1102 |
|
1103 // Unless explicitly asked for, always print in big-endian |
|
1104 // format. |
|
1105 |
|
1106 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1107 // formats and not for Cray? |
|
1108 |
2317
|
1109 oct_mach_info::float_format flt_fmt = |
|
1110 oct_mach_info::native_float_format (); |
|
1111 |
|
1112 if (flt_fmt == oct_mach_info::ieee_big_endian |
|
1113 || flt_fmt == oct_mach_info::cray |
|
1114 || flt_fmt == oct_mach_info::unknown) |
1309
|
1115 { |
1322
|
1116 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1117 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1118 } |
|
1119 else |
|
1120 { |
|
1121 if (bit_format > 1) |
|
1122 { |
1328
|
1123 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1124 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1125 } |
|
1126 else |
|
1127 { |
|
1128 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1129 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1130 } |
|
1131 } |
|
1132 } |
1282
|
1133 else if (xisinf (d)) |
1
|
1134 { |
2804
|
1135 const char *s; |
1
|
1136 if (d < 0.0) |
|
1137 s = "-Inf"; |
|
1138 else |
|
1139 s = "Inf"; |
|
1140 |
|
1141 if (fw > 0) |
3548
|
1142 os << std::setw (fw) << s; |
1
|
1143 else |
|
1144 os << s; |
|
1145 } |
|
1146 else if (xisnan (d)) |
|
1147 { |
|
1148 if (fw > 0) |
3548
|
1149 os << std::setw (fw) << "NaN"; |
1
|
1150 else |
|
1151 os << "NaN"; |
|
1152 } |
|
1153 else |
3608
|
1154 os << pr_formatted_float (*fmt, d); |
1
|
1155 } |
529
|
1156 else |
|
1157 os << d; |
1
|
1158 } |
|
1159 |
|
1160 static inline void |
3608
|
1161 pr_float (std::ostream& os, double d, int fw = 0, double scale = 1.0) |
1
|
1162 { |
3608
|
1163 if (Vfixed_point_format && scale != 1.0) |
|
1164 d /= scale; |
|
1165 |
1
|
1166 pr_any_float (curr_real_fmt, os, d, fw); |
|
1167 } |
|
1168 |
|
1169 static inline void |
3523
|
1170 pr_imag_float (std::ostream& os, double d, int fw = 0) |
1
|
1171 { |
|
1172 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1173 } |
|
1174 |
2522
|
1175 static void |
3608
|
1176 pr_complex (std::ostream& os, const Complex& c, int r_fw = 0, |
|
1177 int i_fw = 0, double scale = 1.0) |
1
|
1178 { |
3608
|
1179 Complex tmp = (Vfixed_point_format && scale != 1.0) ? c / scale : c; |
|
1180 |
|
1181 double r = tmp.real (); |
|
1182 |
1
|
1183 pr_float (os, r, r_fw); |
3608
|
1184 |
1
|
1185 if (! bank_format) |
|
1186 { |
3608
|
1187 double i = tmp.imag (); |
1309
|
1188 if (! (hex_format || bit_format) && i < 0) |
1
|
1189 { |
|
1190 os << " - "; |
|
1191 i = -i; |
|
1192 pr_imag_float (os, i, i_fw); |
|
1193 } |
|
1194 else |
|
1195 { |
1309
|
1196 if (hex_format || bit_format) |
1282
|
1197 os << " "; |
|
1198 else |
|
1199 os << " + "; |
|
1200 |
1
|
1201 pr_imag_float (os, i, i_fw); |
|
1202 } |
|
1203 os << "i"; |
|
1204 } |
|
1205 } |
|
1206 |
626
|
1207 static void |
3523
|
1208 print_empty_matrix (std::ostream& os, int nr, int nc, bool pr_as_read_syntax) |
626
|
1209 { |
|
1210 assert (nr == 0 || nc == 0); |
|
1211 |
|
1212 if (pr_as_read_syntax) |
|
1213 { |
|
1214 if (nr == 0 && nc == 0) |
|
1215 os << "[]"; |
|
1216 else |
|
1217 os << "zeros (" << nr << ", " << nc << ")"; |
|
1218 } |
|
1219 else |
|
1220 { |
|
1221 os << "[]"; |
2165
|
1222 if (Vprint_empty_dimensions) |
626
|
1223 os << "(" << nr << "x" << nc << ")"; |
|
1224 } |
|
1225 } |
|
1226 |
1186
|
1227 static void |
3523
|
1228 pr_scale_header (std::ostream& os, double scale) |
3105
|
1229 { |
|
1230 if (Vfixed_point_format && scale != 1.0) |
|
1231 { |
3568
|
1232 os << " " |
|
1233 << std::setw (8) << std::setprecision (1) |
|
1234 << std::setiosflags (std::ios::scientific|std::ios::left) |
|
1235 << scale |
|
1236 << std::resetiosflags (std::ios::scientific|std::ios::left) |
|
1237 << " *\n"; |
3105
|
1238 |
|
1239 if (! compact_format) |
|
1240 os << "\n"; |
|
1241 } |
|
1242 } |
|
1243 |
|
1244 static void |
3523
|
1245 pr_col_num_header (std::ostream& os, int total_width, int max_width, |
1972
|
1246 int lim, int col, int extra_indent) |
1186
|
1247 { |
2165
|
1248 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1249 { |
|
1250 if (col != 0 && ! compact_format) |
2915
|
1251 os << "\n\n"; |
1186
|
1252 |
|
1253 int num_cols = lim - col; |
|
1254 |
3548
|
1255 os << std::setw (extra_indent) << ""; |
1972
|
1256 |
1186
|
1257 if (num_cols == 1) |
|
1258 os << " Column " << col + 1 << ":\n"; |
|
1259 else if (num_cols == 2) |
|
1260 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1261 else |
|
1262 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
2915
|
1263 |
|
1264 if (! compact_format) |
|
1265 os << "\n"; |
1186
|
1266 } |
|
1267 } |
|
1268 |
3248
|
1269 static inline void |
3608
|
1270 pr_plus_format (std::ostream& os, double d) |
3248
|
1271 { |
|
1272 if (d == 0.0) |
|
1273 os << " "; |
|
1274 else if (d < 0.0) |
|
1275 os << "-"; |
|
1276 else |
|
1277 os << "+"; |
|
1278 } |
|
1279 |
1
|
1280 void |
3523
|
1281 octave_print_internal (std::ostream& os, double d, bool pr_as_read_syntax) |
1
|
1282 { |
|
1283 if (plus_format) |
|
1284 { |
3608
|
1285 pr_plus_format (os, d); |
1
|
1286 } |
|
1287 else |
|
1288 { |
|
1289 set_format (d); |
|
1290 if (free_format) |
|
1291 os << d; |
|
1292 else |
|
1293 pr_float (os, d); |
|
1294 } |
|
1295 } |
|
1296 |
|
1297 void |
3608
|
1298 octave_print_internal (std::ostream& os, const Matrix& m, |
|
1299 bool pr_as_read_syntax, int extra_indent) |
1
|
1300 { |
|
1301 int nr = m.rows (); |
|
1302 int nc = m.columns (); |
|
1303 |
2408
|
1304 if (nr == 0 || nc == 0) |
626
|
1305 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1306 else if (plus_format && ! pr_as_read_syntax) |
1
|
1307 { |
|
1308 for (int i = 0; i < nr; i++) |
|
1309 { |
|
1310 for (int j = 0; j < nc; j++) |
|
1311 { |
|
1312 if (j == 0) |
|
1313 os << " "; |
|
1314 |
3608
|
1315 pr_plus_format (os, m(i,j)); |
1
|
1316 } |
2907
|
1317 |
|
1318 if (i < nr - 1) |
|
1319 os << "\n"; |
1
|
1320 } |
|
1321 } |
|
1322 else |
|
1323 { |
|
1324 int fw; |
3105
|
1325 double scale = 1.0; |
|
1326 set_format (m, fw, scale); |
1
|
1327 int column_width = fw + 2; |
|
1328 int total_width = nc * column_width; |
2926
|
1329 int max_width = command_editor::terminal_cols (); |
1
|
1330 |
626
|
1331 if (pr_as_read_syntax) |
|
1332 max_width -= 4; |
1972
|
1333 else |
|
1334 max_width -= extra_indent; |
|
1335 |
|
1336 if (max_width < 0) |
|
1337 max_width = 0; |
626
|
1338 |
1
|
1339 if (free_format) |
|
1340 { |
626
|
1341 if (pr_as_read_syntax) |
|
1342 os << "[\n"; |
|
1343 |
1
|
1344 os << m; |
626
|
1345 |
|
1346 if (pr_as_read_syntax) |
|
1347 os << "]"; |
|
1348 |
1
|
1349 return; |
|
1350 } |
|
1351 |
|
1352 int inc = nc; |
2165
|
1353 if (total_width > max_width && Vsplit_long_rows) |
1
|
1354 { |
|
1355 inc = max_width / column_width; |
|
1356 if (inc == 0) |
|
1357 inc++; |
|
1358 } |
|
1359 |
626
|
1360 if (pr_as_read_syntax) |
1
|
1361 { |
|
1362 for (int i = 0; i < nr; i++) |
|
1363 { |
626
|
1364 int col = 0; |
|
1365 while (col < nc) |
1
|
1366 { |
626
|
1367 int lim = col + inc < nc ? col + inc : nc; |
|
1368 |
|
1369 for (int j = col; j < lim; j++) |
|
1370 { |
|
1371 if (i == 0 && j == 0) |
|
1372 os << "[ "; |
|
1373 else |
|
1374 { |
|
1375 if (j > col && j < lim) |
|
1376 os << ", "; |
|
1377 else |
|
1378 os << " "; |
|
1379 } |
|
1380 |
3608
|
1381 pr_float (os, m(i,j)); |
626
|
1382 } |
|
1383 |
|
1384 col += inc; |
|
1385 |
|
1386 if (col >= nc) |
|
1387 { |
|
1388 if (i == nr - 1) |
|
1389 os << " ]"; |
|
1390 else |
|
1391 os << ";\n"; |
|
1392 } |
|
1393 else |
|
1394 os << " ...\n"; |
1
|
1395 } |
|
1396 } |
626
|
1397 } |
|
1398 else |
|
1399 { |
3105
|
1400 pr_scale_header (os, scale); |
|
1401 |
626
|
1402 for (int col = 0; col < nc; col += inc) |
|
1403 { |
|
1404 int lim = col + inc < nc ? col + inc : nc; |
|
1405 |
1972
|
1406 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1407 extra_indent); |
626
|
1408 |
|
1409 for (int i = 0; i < nr; i++) |
|
1410 { |
3548
|
1411 os << std::setw (extra_indent) << ""; |
1972
|
1412 |
626
|
1413 for (int j = col; j < lim; j++) |
|
1414 { |
|
1415 os << " "; |
|
1416 |
3608
|
1417 pr_float (os, m(i,j), fw, scale); |
626
|
1418 } |
|
1419 |
2907
|
1420 if (i < nr - 1) |
|
1421 os << "\n"; |
626
|
1422 } |
|
1423 } |
1
|
1424 } |
|
1425 } |
|
1426 } |
|
1427 |
3248
|
1428 static inline void |
3608
|
1429 pr_plus_format (std::ostream& os, const Complex& c) |
3248
|
1430 { |
|
1431 double rp = c.real (); |
|
1432 double ip = c.imag (); |
|
1433 |
|
1434 if (rp == 0.0) |
|
1435 { |
|
1436 if (ip == 0.0) |
|
1437 os << " "; |
|
1438 else |
|
1439 os << "i"; |
|
1440 } |
|
1441 else if (ip == 0.0) |
3608
|
1442 pr_plus_format (os, rp); |
3248
|
1443 else |
|
1444 os << "c"; |
|
1445 } |
|
1446 |
1
|
1447 void |
3523
|
1448 octave_print_internal (std::ostream& os, const Complex& c, |
1972
|
1449 bool pr_as_read_syntax) |
1
|
1450 { |
|
1451 if (plus_format) |
|
1452 { |
3608
|
1453 pr_plus_format (os, c); |
1
|
1454 } |
|
1455 else |
|
1456 { |
|
1457 set_format (c); |
|
1458 if (free_format) |
|
1459 os << c; |
|
1460 else |
|
1461 pr_complex (os, c); |
|
1462 } |
|
1463 } |
|
1464 |
|
1465 void |
3523
|
1466 octave_print_internal (std::ostream& os, const ComplexMatrix& cm, |
1972
|
1467 bool pr_as_read_syntax, int extra_indent) |
1
|
1468 { |
|
1469 int nr = cm.rows (); |
|
1470 int nc = cm.columns (); |
|
1471 |
2408
|
1472 if (nr == 0 || nc == 0) |
626
|
1473 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1474 else if (plus_format && ! pr_as_read_syntax) |
1
|
1475 { |
|
1476 for (int i = 0; i < nr; i++) |
|
1477 { |
|
1478 for (int j = 0; j < nc; j++) |
|
1479 { |
|
1480 if (j == 0) |
|
1481 os << " "; |
|
1482 |
3608
|
1483 pr_plus_format (os, cm(i,j)); |
1
|
1484 } |
2907
|
1485 |
|
1486 if (i < nr - 1) |
|
1487 os << "\n"; |
1
|
1488 } |
|
1489 } |
|
1490 else |
|
1491 { |
|
1492 int r_fw, i_fw; |
3105
|
1493 double scale = 1.0; |
|
1494 set_format (cm, r_fw, i_fw, scale); |
1
|
1495 int column_width = i_fw + r_fw; |
1309
|
1496 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1497 int total_width = nc * column_width; |
2926
|
1498 int max_width = command_editor::terminal_cols (); |
1
|
1499 |
626
|
1500 if (pr_as_read_syntax) |
|
1501 max_width -= 4; |
1972
|
1502 else |
|
1503 max_width -= extra_indent; |
|
1504 |
|
1505 if (max_width < 0) |
|
1506 max_width = 0; |
626
|
1507 |
1
|
1508 if (free_format) |
|
1509 { |
626
|
1510 if (pr_as_read_syntax) |
|
1511 os << "[\n"; |
|
1512 |
1
|
1513 os << cm; |
626
|
1514 |
|
1515 if (pr_as_read_syntax) |
|
1516 os << "]"; |
|
1517 |
1
|
1518 return; |
|
1519 } |
|
1520 |
|
1521 int inc = nc; |
2165
|
1522 if (total_width > max_width && Vsplit_long_rows) |
1
|
1523 { |
|
1524 inc = max_width / column_width; |
|
1525 if (inc == 0) |
|
1526 inc++; |
|
1527 } |
|
1528 |
626
|
1529 if (pr_as_read_syntax) |
1
|
1530 { |
|
1531 for (int i = 0; i < nr; i++) |
|
1532 { |
626
|
1533 int col = 0; |
|
1534 while (col < nc) |
1
|
1535 { |
626
|
1536 int lim = col + inc < nc ? col + inc : nc; |
|
1537 |
|
1538 for (int j = col; j < lim; j++) |
|
1539 { |
|
1540 if (i == 0 && j == 0) |
|
1541 os << "[ "; |
|
1542 else |
|
1543 { |
|
1544 if (j > col && j < lim) |
|
1545 os << ", "; |
|
1546 else |
|
1547 os << " "; |
|
1548 } |
|
1549 |
3608
|
1550 pr_complex (os, cm(i,j)); |
626
|
1551 } |
|
1552 |
|
1553 col += inc; |
|
1554 |
|
1555 if (col >= nc) |
|
1556 { |
|
1557 if (i == nr - 1) |
|
1558 os << " ]"; |
|
1559 else |
|
1560 os << ";\n"; |
|
1561 } |
1
|
1562 else |
626
|
1563 os << " ...\n"; |
1
|
1564 } |
|
1565 } |
626
|
1566 } |
|
1567 else |
|
1568 { |
3105
|
1569 pr_scale_header (os, scale); |
|
1570 |
626
|
1571 for (int col = 0; col < nc; col += inc) |
|
1572 { |
|
1573 int lim = col + inc < nc ? col + inc : nc; |
|
1574 |
1972
|
1575 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1576 extra_indent); |
626
|
1577 |
|
1578 for (int i = 0; i < nr; i++) |
|
1579 { |
3548
|
1580 os << std::setw (extra_indent) << ""; |
1972
|
1581 |
626
|
1582 for (int j = col; j < lim; j++) |
|
1583 { |
|
1584 os << " "; |
|
1585 |
3608
|
1586 pr_complex (os, cm(i,j), r_fw, i_fw, scale); |
626
|
1587 } |
2907
|
1588 |
|
1589 if (i < nr - 1) |
|
1590 os << "\n"; |
626
|
1591 } |
|
1592 } |
1
|
1593 } |
|
1594 } |
|
1595 } |
|
1596 |
|
1597 void |
3523
|
1598 octave_print_internal (std::ostream& os, const Range& r, |
1972
|
1599 bool pr_as_read_syntax, int extra_indent) |
1
|
1600 { |
626
|
1601 double base = r.base (); |
1
|
1602 double increment = r.inc (); |
626
|
1603 double limit = r.limit (); |
1
|
1604 int num_elem = r.nelem (); |
|
1605 |
626
|
1606 if (plus_format && ! pr_as_read_syntax) |
1
|
1607 { |
|
1608 os << " "; |
|
1609 for (int i = 0; i < num_elem; i++) |
|
1610 { |
626
|
1611 double val = base + i * increment; |
1
|
1612 if (val == 0.0) |
|
1613 os << " "; |
|
1614 else |
|
1615 os << "+"; |
|
1616 } |
|
1617 } |
|
1618 else |
|
1619 { |
|
1620 int fw; |
3105
|
1621 double scale = 1.0; |
|
1622 set_format (r, fw, scale); |
1
|
1623 |
626
|
1624 if (pr_as_read_syntax) |
1
|
1625 { |
626
|
1626 if (free_format) |
|
1627 { |
|
1628 os << base << " : "; |
|
1629 if (increment != 1.0) |
|
1630 os << increment << " : "; |
|
1631 os << limit; |
|
1632 } |
|
1633 else |
|
1634 { |
|
1635 pr_float (os, base, fw); |
|
1636 os << " : "; |
|
1637 if (increment != 1.0) |
|
1638 { |
|
1639 pr_float (os, increment, fw); |
|
1640 os << " : "; |
|
1641 } |
|
1642 pr_float (os, limit, fw); |
|
1643 } |
1
|
1644 } |
626
|
1645 else |
|
1646 { |
|
1647 int column_width = fw + 2; |
|
1648 int total_width = num_elem * column_width; |
2926
|
1649 int max_width = command_editor::terminal_cols (); |
1
|
1650 |
626
|
1651 if (free_format) |
|
1652 { |
|
1653 os << r; |
|
1654 return; |
|
1655 } |
1
|
1656 |
626
|
1657 int inc = num_elem; |
2165
|
1658 if (total_width > max_width && Vsplit_long_rows) |
1
|
1659 { |
626
|
1660 inc = max_width / column_width; |
|
1661 if (inc == 0) |
|
1662 inc++; |
1
|
1663 } |
|
1664 |
1972
|
1665 max_width -= extra_indent; |
|
1666 |
|
1667 if (max_width < 0) |
|
1668 max_width = 0; |
|
1669 |
3105
|
1670 pr_scale_header (os, scale); |
|
1671 |
626
|
1672 int col = 0; |
|
1673 while (col < num_elem) |
1
|
1674 { |
626
|
1675 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1676 |
1972
|
1677 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1678 extra_indent); |
|
1679 |
3548
|
1680 os << std::setw (extra_indent) << ""; |
626
|
1681 |
|
1682 for (int i = col; i < lim; i++) |
|
1683 { |
|
1684 double val = base + i * increment; |
3105
|
1685 |
626
|
1686 os << " "; |
3105
|
1687 |
3608
|
1688 pr_float (os, val, fw, scale); |
626
|
1689 } |
|
1690 |
2907
|
1691 col += inc; |
626
|
1692 |
2907
|
1693 if (col < num_elem) |
|
1694 os << "\n"; |
1
|
1695 } |
|
1696 } |
|
1697 } |
|
1698 } |
|
1699 |
1343
|
1700 void |
3523
|
1701 octave_print_internal (std::ostream& os, const boolMatrix& bm, |
3215
|
1702 bool pr_as_read_syntax, |
|
1703 int extra_indent) |
|
1704 { |
|
1705 Matrix tmp (bm); |
|
1706 octave_print_internal (os, tmp, pr_as_read_syntax, extra_indent); |
|
1707 } |
|
1708 |
|
1709 void |
3523
|
1710 octave_print_internal (std::ostream& os, const charMatrix& chm, |
3215
|
1711 bool pr_as_read_syntax, |
|
1712 int /* extra_indent XXX FIXME XXX */, |
|
1713 bool pr_as_string) |
1343
|
1714 { |
1572
|
1715 if (pr_as_string) |
|
1716 { |
|
1717 int nstr = chm.rows (); |
1343
|
1718 |
1572
|
1719 if (pr_as_read_syntax && nstr > 1) |
|
1720 os << "[ "; |
1343
|
1721 |
2907
|
1722 if (nstr != 0) |
1343
|
1723 { |
2664
|
1724 for (int i = 0; i < nstr; i++) |
1572
|
1725 { |
3523
|
1726 std::string row = chm.row_as_string (i); |
2664
|
1727 |
|
1728 if (pr_as_read_syntax) |
|
1729 { |
|
1730 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
1731 |
2664
|
1732 if (i < nstr - 1) |
|
1733 os << "; "; |
|
1734 } |
|
1735 else |
2907
|
1736 { |
|
1737 os << row; |
|
1738 |
|
1739 if (i < nstr - 1) |
|
1740 os << "\n"; |
|
1741 } |
1572
|
1742 } |
1343
|
1743 } |
1572
|
1744 |
|
1745 if (pr_as_read_syntax && nstr > 1) |
|
1746 os << " ]"; |
1343
|
1747 } |
1572
|
1748 else |
|
1749 { |
|
1750 os << "sorry, printing char matrices not implemented yet\n"; |
|
1751 } |
1343
|
1752 } |
|
1753 |
1
|
1754 static void |
|
1755 init_format_state (void) |
|
1756 { |
2387
|
1757 free_format = false; |
|
1758 plus_format = false; |
|
1759 bank_format = false; |
3608
|
1760 hex_format = 0; |
1309
|
1761 bit_format = 0; |
2387
|
1762 print_e = false; |
|
1763 print_big_e = false; |
1
|
1764 } |
|
1765 |
|
1766 static void |
|
1767 set_output_prec_and_fw (int prec, int fw) |
|
1768 { |
2800
|
1769 bind_builtin_variable ("output_precision", static_cast<double> (prec)); |
|
1770 bind_builtin_variable ("output_max_field_width", static_cast<double> (fw)); |
1
|
1771 } |
|
1772 |
1755
|
1773 static void |
|
1774 set_format_style (int argc, const string_vector& argv) |
1
|
1775 { |
1755
|
1776 int idx = 1; |
|
1777 |
1899
|
1778 if (--argc > 0) |
1
|
1779 { |
3523
|
1780 std::string arg = argv[idx++]; |
2584
|
1781 |
1755
|
1782 if (arg == "short") |
1
|
1783 { |
1755
|
1784 if (--argc > 0) |
1
|
1785 { |
1755
|
1786 arg = argv[idx++]; |
|
1787 |
|
1788 if (arg == "e") |
1
|
1789 { |
1755
|
1790 init_format_state (); |
2387
|
1791 print_e = true; |
1755
|
1792 } |
|
1793 else if (arg == "E") |
|
1794 { |
|
1795 init_format_state (); |
2387
|
1796 print_e = true; |
|
1797 print_big_e = true; |
1
|
1798 } |
|
1799 else |
|
1800 { |
1755
|
1801 error ("format: unrecognized option `short %s'", |
|
1802 arg.c_str ()); |
|
1803 return; |
|
1804 } |
|
1805 } |
|
1806 else |
|
1807 init_format_state (); |
|
1808 |
|
1809 set_output_prec_and_fw (3, 8); |
|
1810 } |
|
1811 else if (arg == "long") |
|
1812 { |
|
1813 if (--argc > 0) |
|
1814 { |
|
1815 arg = argv[idx++]; |
|
1816 |
|
1817 if (arg == "e") |
|
1818 { |
|
1819 init_format_state (); |
2387
|
1820 print_e = true; |
1755
|
1821 } |
|
1822 else if (arg == "E") |
|
1823 { |
|
1824 init_format_state (); |
2387
|
1825 print_e = true; |
|
1826 print_big_e = true; |
1
|
1827 } |
|
1828 else |
1755
|
1829 { |
|
1830 error ("format: unrecognized option `long %s'", |
|
1831 arg.c_str ()); |
|
1832 return; |
|
1833 } |
1186
|
1834 } |
1
|
1835 else |
1755
|
1836 init_format_state (); |
|
1837 |
|
1838 set_output_prec_and_fw (15, 24); |
|
1839 } |
|
1840 else if (arg == "hex") |
|
1841 { |
|
1842 init_format_state (); |
3608
|
1843 hex_format = 1; |
1755
|
1844 } |
|
1845 else if (arg == "native-hex") |
|
1846 { |
|
1847 init_format_state (); |
|
1848 hex_format = 2; |
|
1849 } |
|
1850 else if (arg == "bit") |
|
1851 { |
|
1852 init_format_state (); |
|
1853 bit_format = 1; |
|
1854 } |
|
1855 else if (arg == "native-bit") |
|
1856 { |
|
1857 init_format_state (); |
|
1858 bit_format = 2; |
|
1859 } |
|
1860 else if (arg == "+" || arg == "plus") |
|
1861 { |
|
1862 init_format_state (); |
2387
|
1863 plus_format = true; |
1755
|
1864 } |
|
1865 else if (arg == "bank") |
|
1866 { |
|
1867 init_format_state (); |
2387
|
1868 bank_format = true; |
1755
|
1869 } |
|
1870 else if (arg == "free") |
|
1871 { |
|
1872 init_format_state (); |
2387
|
1873 free_format = true; |
1755
|
1874 } |
|
1875 else if (arg == "none") |
|
1876 { |
|
1877 init_format_state (); |
2387
|
1878 free_format = true; |
1755
|
1879 } |
|
1880 else if (arg == "compact") |
|
1881 { |
2387
|
1882 compact_format = true; |
1755
|
1883 } |
|
1884 else if (arg == "loose") |
|
1885 { |
2387
|
1886 compact_format = false; |
1
|
1887 } |
|
1888 else |
1755
|
1889 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
1890 } |
|
1891 else |
|
1892 { |
|
1893 init_format_state (); |
|
1894 set_output_prec_and_fw (5, 10); |
|
1895 } |
|
1896 } |
|
1897 |
1957
|
1898 DEFUN_TEXT (format, args, , |
3372
|
1899 "-*- texinfo -*-\n\ |
|
1900 @deffn {Command} format options\n\ |
|
1901 Control the format of the output produced by @code{disp} and Octave's\n\ |
|
1902 normal echoing mechanism. Valid options are listed in the following\n\ |
|
1903 table.\n\ |
|
1904 \n\ |
|
1905 @table @code\n\ |
|
1906 @item short\n\ |
|
1907 Octave will try to print numbers with at\n\ |
|
1908 least 3 significant figures within a field that is a maximum of 8\n\ |
|
1909 characters wide.\n\ |
|
1910 \n\ |
|
1911 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
1912 decimal point and all the numbers fit within the maximum field width,\n\ |
|
1913 it switches to an @samp{e} format.\n\ |
|
1914 \n\ |
|
1915 @item long\n\ |
|
1916 Octave will try to print numbers with at least 15 significant figures\n\ |
|
1917 within a field that is a maximum of 24 characters wide.\n\ |
|
1918 \n\ |
|
1919 As will the @samp{short} format, Octave will switch to an @samp{e}\n\ |
|
1920 format if it is unable to format a matrix so that columns line up on the\n\ |
|
1921 decimal point and all the numbers fit within the maximum field width.\n\ |
|
1922 \n\ |
|
1923 @item long e\n\ |
|
1924 @itemx short e\n\ |
|
1925 The same as @samp{format long} or @samp{format short} but always display\n\ |
|
1926 output with an @samp{e} format. For example, with the @samp{short e}\n\ |
|
1927 format, pi is displayed as @code{3.14e+00}.\n\ |
|
1928 \n\ |
|
1929 @item long E\n\ |
|
1930 @itemx short E\n\ |
|
1931 The same as @samp{format long e} or @samp{format short e} but always\n\ |
|
1932 display output with an uppercase @samp{E} format. For example, with\n\ |
|
1933 the @samp{long E} format, pi is displayed as\n\ |
|
1934 @code{3.14159265358979E+00}.\n\ |
|
1935 \n\ |
|
1936 @item free\n\ |
|
1937 @itemx none\n\ |
|
1938 Print output in free format, without trying to line up columns of\n\ |
|
1939 matrices on the decimal point. This also causes complex numbers to be\n\ |
|
1940 formatted like this @samp{(0.604194, 0.607088)} instead of like this\n\ |
|
1941 @samp{0.60419 + 0.60709i}.\n\ |
529
|
1942 \n\ |
3372
|
1943 @item bank\n\ |
|
1944 Print in a fixed format with two places to the right of the decimal\n\ |
|
1945 point.\n\ |
|
1946 \n\ |
|
1947 @item +\n\ |
|
1948 Print a @samp{+} symbol for nonzero matrix elements and a space for zero\n\ |
|
1949 matrix elements. This format can be very useful for examining the\n\ |
|
1950 structure of a large matrix.\n\ |
|
1951 \n\ |
|
1952 @item hex\n\ |
|
1953 Print the hexadecimal representation numbers as they are stored in\n\ |
|
1954 memory. For example, on a workstation which stores 8 byte real values\n\ |
|
1955 in IEEE format with the least significant byte first, the value of\n\ |
|
1956 @code{pi} when printed in @code{hex} format is @code{400921fb54442d18}.\n\ |
|
1957 This format only works for numeric values.\n\ |
|
1958 \n\ |
|
1959 @item bit\n\ |
|
1960 Print the bit representation of numbers as stored in memory.\n\ |
|
1961 For example, the value of @code{pi} is\n\ |
|
1962 \n\ |
|
1963 @example\n\ |
|
1964 @group\n\ |
|
1965 01000000000010010010000111111011\n\ |
|
1966 01010100010001000010110100011000\n\ |
|
1967 @end group\n\ |
|
1968 @end example\n\ |
|
1969 \n\ |
|
1970 (shown here in two 32 bit sections for typesetting purposes) when\n\ |
|
1971 printed in bit format on a workstation which stores 8 byte real values\n\ |
|
1972 in IEEE format with the least significant byte first. This format only\n\ |
|
1973 works for numeric types.\n\ |
|
1974 @end table\n\ |
|
1975 \n\ |
|
1976 By default, Octave will try to print numbers with at least 5 significant\n\ |
|
1977 figures within a field that is a maximum of 10 characters wide.\n\ |
|
1978 \n\ |
|
1979 If Octave is unable to format a matrix so that columns line up on the\n\ |
|
1980 decimal point and all the numbers fit within the maximum field width,\n\ |
|
1981 it switches to an @samp{e} format.\n\ |
|
1982 \n\ |
|
1983 If @code{format} is invoked without any options, the default format\n\ |
|
1984 state is restored.\n\ |
|
1985 @end deffn") |
529
|
1986 { |
2086
|
1987 octave_value_list retval; |
529
|
1988 |
1755
|
1989 int argc = args.length () + 1; |
|
1990 |
1968
|
1991 string_vector argv = args.make_argv ("format"); |
1755
|
1992 |
|
1993 if (error_state) |
|
1994 return retval; |
529
|
1995 |
|
1996 set_format_style (argc, argv); |
|
1997 |
|
1998 return retval; |
|
1999 } |
|
2000 |
2165
|
2001 static int |
3105
|
2002 fixed_point_format (void) |
|
2003 { |
|
2004 Vfixed_point_format = check_preference ("fixed_point_format"); |
|
2005 |
|
2006 return 0; |
|
2007 } |
|
2008 |
|
2009 static int |
2165
|
2010 output_max_field_width (void) |
|
2011 { |
|
2012 double val; |
|
2013 if (builtin_real_scalar_variable ("output_max_field_width", val) |
|
2014 && ! xisnan (val)) |
|
2015 { |
|
2016 int ival = NINT (val); |
2800
|
2017 if (ival > 0 && ival == val) |
2165
|
2018 { |
|
2019 Voutput_max_field_width = ival; |
|
2020 return 0; |
|
2021 } |
|
2022 } |
|
2023 gripe_invalid_value_specified ("output_max_field_width"); |
|
2024 return -1; |
|
2025 } |
|
2026 |
|
2027 static int |
|
2028 output_precision (void) |
|
2029 { |
|
2030 double val; |
|
2031 if (builtin_real_scalar_variable ("output_precision", val) |
|
2032 && ! xisnan (val)) |
|
2033 { |
|
2034 int ival = NINT (val); |
2800
|
2035 if (ival >= 0 && ival == val) |
2165
|
2036 { |
|
2037 Voutput_precision = ival; |
|
2038 return 0; |
|
2039 } |
|
2040 } |
|
2041 gripe_invalid_value_specified ("output_precision"); |
|
2042 return -1; |
|
2043 } |
|
2044 |
|
2045 static int |
|
2046 print_empty_dimensions (void) |
|
2047 { |
|
2048 Vprint_empty_dimensions = check_preference ("print_empty_dimensions"); |
|
2049 |
|
2050 return 0; |
|
2051 } |
|
2052 |
|
2053 static int |
|
2054 split_long_rows (void) |
|
2055 { |
|
2056 Vsplit_long_rows = check_preference ("split_long_rows"); |
|
2057 |
|
2058 return 0; |
|
2059 } |
|
2060 |
|
2061 void |
|
2062 symbols_of_pr_output (void) |
|
2063 { |
3258
|
2064 DEFVAR (fixed_point_format, 0.0, fixed_point_format, |
3321
|
2065 "-*- texinfo -*-\n\ |
|
2066 @defvr {Built-in Variable} fixed_point_format\n\ |
|
2067 If the value of this variable is nonzero, Octave will scale all values\n\ |
|
2068 in a matrix so that the largest may be written with one leading digit.\n\ |
|
2069 The scaling factor is printed on the first line of output. For example,\n\ |
|
2070 \n\ |
|
2071 @example\n\ |
|
2072 @group\n\ |
|
2073 octave:1> logspace (1, 7, 5)'\n\ |
|
2074 ans =\n\ |
|
2075 \n\ |
|
2076 1.0e+07 *\n\ |
|
2077 \n\ |
|
2078 0.00000\n\ |
|
2079 0.00003\n\ |
|
2080 0.00100\n\ |
|
2081 0.03162\n\ |
|
2082 1.00000\n\ |
|
2083 @end group\n\ |
|
2084 @end example\n\ |
|
2085 \n\ |
|
2086 @noindent\n\ |
|
2087 Notice that first value appears to be zero when it is actually 1. For\n\ |
|
2088 this reason, you should be careful when setting\n\ |
|
2089 @code{fixed_point_format} to a nonzero value.\n\ |
|
2090 \n\ |
|
2091 The default value of @code{fixed_point_format} is 0.\n\ |
3333
|
2092 @end defvr"); |
3105
|
2093 |
3258
|
2094 DEFVAR (output_max_field_width, 10.0, output_max_field_width, |
3321
|
2095 "-*- texinfo -*-\n\ |
|
2096 @defvr {Built-in Variable} output_max_field_width\n\ |
|
2097 This variable specifies the maximum width of a numeric output field.\n\ |
|
2098 The default value is 10.\n\ |
3333
|
2099 @end defvr"); |
2165
|
2100 |
3258
|
2101 DEFVAR (output_precision, 5.0, output_precision, |
3321
|
2102 "-*- texinfo -*-\n\ |
|
2103 @defvr {Built-in Variable} output_precision\n\ |
|
2104 This variable specifies the minimum number of significant figures to\n\ |
|
2105 display for numeric output. The default value is 5.\n\ |
3333
|
2106 @end defvr"); |
2165
|
2107 |
3258
|
2108 DEFVAR (print_empty_dimensions, 1.0, print_empty_dimensions, |
3321
|
2109 "-*- texinfo -*-\n\ |
|
2110 @defvr {Built-in Variable} print_empty_dimensions\n\ |
|
2111 If the value of @code{print_empty_dimensions} is nonzero, the\n\ |
|
2112 dimensions of empty matrices are printed along with the empty matrix\n\ |
|
2113 symbol, @samp{[]}. For example, the expression\n\ |
|
2114 \n\ |
|
2115 @example\n\ |
|
2116 zeros (3, 0)\n\ |
|
2117 @end example\n\ |
|
2118 \n\ |
|
2119 @noindent\n\ |
|
2120 will print\n\ |
|
2121 \n\ |
|
2122 @example\n\ |
|
2123 ans = [](3x0)\n\ |
|
2124 @end example\n\ |
3333
|
2125 @end defvr"); |
2165
|
2126 |
3258
|
2127 DEFVAR (split_long_rows, 1.0, split_long_rows, |
3321
|
2128 "-*- texinfo -*-\n\ |
|
2129 @defvr {Built-in Variable} split_long_rows\n\ |
|
2130 For large matrices, Octave may not be able to display all the columns of\n\ |
|
2131 a given row on one line of your screen. This can result in missing\n\ |
|
2132 information or output that is nearly impossible to decipher, depending\n\ |
|
2133 on whether your terminal truncates or wraps long lines.\n\ |
|
2134 \n\ |
|
2135 If the value of @code{split_long_rows} is nonzero, Octave will display\n\ |
|
2136 the matrix in a series of smaller pieces, each of which can fit within\n\ |
|
2137 the limits of your terminal width. Each set of rows is labeled so that\n\ |
|
2138 you can easily see which columns are currently being displayed.\n\ |
|
2139 For example:\n\ |
|
2140 \n\ |
|
2141 @smallexample\n\ |
|
2142 @group\n\ |
|
2143 octave:13> rand (2,10)\n\ |
|
2144 ans =\n\ |
|
2145 \n\ |
|
2146 Columns 1 through 6:\n\ |
|
2147 \n\ |
|
2148 0.75883 0.93290 0.40064 0.43818 0.94958 0.16467\n\ |
|
2149 0.75697 0.51942 0.40031 0.61784 0.92309 0.40201\n\ |
|
2150 \n\ |
|
2151 Columns 7 through 10:\n\ |
|
2152 \n\ |
|
2153 0.90174 0.11854 0.72313 0.73326\n\ |
|
2154 0.44672 0.94303 0.56564 0.82150\n\ |
|
2155 @end group\n\ |
|
2156 @end smallexample\n\ |
|
2157 \n\ |
|
2158 @noindent\n\ |
|
2159 The default value of @code{split_long_rows} is nonzero.\n\ |
3333
|
2160 @end defvr"); |
2165
|
2161 } |
|
2162 |
1
|
2163 /* |
|
2164 ;;; Local Variables: *** |
|
2165 ;;; mode: C++ *** |
|
2166 ;;; End: *** |
|
2167 */ |