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