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> |
1728
|
34 #include <string> |
|
35 |
453
|
36 #include "CMatrix.h" |
1
|
37 #include "Range.h" |
2926
|
38 #include "cmd-edit.h" |
1352
|
39 #include "dMatrix.h" |
2891
|
40 #include "lo-mappers.h" |
4051
|
41 #include "lo-sstream.h" |
2317
|
42 #include "mach-info.h" |
1651
|
43 #include "oct-cmplx.h" |
4153
|
44 #include "quit.h" |
1755
|
45 #include "str-vec.h" |
1
|
46 |
3933
|
47 #include "Cell.h" |
1352
|
48 #include "defun.h" |
|
49 #include "error.h" |
2165
|
50 #include "gripes.h" |
1755
|
51 #include "oct-obj.h" |
3685
|
52 #include "oct-stream.h" |
1352
|
53 #include "pager.h" |
|
54 #include "pr-output.h" |
1282
|
55 #include "sysdep.h" |
1
|
56 #include "utils.h" |
1352
|
57 #include "variables.h" |
1
|
58 |
3105
|
59 // TRUE means use a scaled fixed point format for `format long' and |
|
60 // `format short'. |
|
61 static bool Vfixed_point_format; |
|
62 |
2165
|
63 // The maximum field width for a number printed by the default output |
|
64 // routines. |
|
65 static int Voutput_max_field_width; |
|
66 |
|
67 // The precision of the numbers printed by the default output |
|
68 // routines. |
|
69 static int Voutput_precision; |
|
70 |
|
71 // TRUE means that the dimensions of empty matrices should be printed |
|
72 // like this: x = [](2x0). |
|
73 static bool Vprint_empty_dimensions; |
|
74 |
|
75 // TRUE means that the rows of big matrices should be split into |
|
76 // smaller slices that fit on the screen. |
|
77 static bool Vsplit_long_rows; |
|
78 |
3018
|
79 // TRUE means don't do any fancy formatting. |
2387
|
80 static bool free_format = false; |
1
|
81 |
3018
|
82 // TRUE means print plus sign for nonzero, blank for zero. |
2387
|
83 static bool plus_format = false; |
1
|
84 |
3018
|
85 // TRUE means always print like dollars and cents. |
2387
|
86 static bool bank_format = false; |
1282
|
87 |
3018
|
88 // TRUE means print data in hexadecimal format. |
3608
|
89 static int hex_format = 0; |
1282
|
90 |
3018
|
91 // TRUE means print data in binary-bit-pattern format. |
1309
|
92 static int bit_format = 0; |
|
93 |
3018
|
94 // TRUE means don't put newlines around the column number headers. |
2387
|
95 static bool compact_format = false; |
1186
|
96 |
3018
|
97 // TRUE means use an e format. |
2387
|
98 static bool print_e = false; |
1
|
99 |
4509
|
100 // TRUE means use a g format. |
|
101 static bool print_g = false; |
|
102 |
3018
|
103 // TRUE means print E instead of e for exponent field. |
2387
|
104 static bool print_big_e = false; |
1
|
105 |
3608
|
106 class pr_formatted_float; |
|
107 |
4509
|
108 static int |
|
109 current_output_max_field_width (void) |
|
110 { |
|
111 return Voutput_max_field_width; |
|
112 } |
|
113 |
|
114 static int |
|
115 current_output_precision (void) |
|
116 { |
|
117 return Voutput_precision; |
|
118 } |
|
119 |
3608
|
120 class |
|
121 float_format |
|
122 { |
|
123 public: |
|
124 |
4509
|
125 float_format (int w = current_output_max_field_width (), |
|
126 int p = current_output_precision (), int f = 0) |
3608
|
127 : fw (w), prec (p), fmt (f), up (0), sp (0) { } |
|
128 |
|
129 float_format (const float_format& ff) |
|
130 : fw (ff.fw), prec (ff.prec), fmt (ff.fmt), up (ff.up), sp (ff.sp) { } |
|
131 |
|
132 float_format& operator = (const float_format& ff) |
|
133 { |
|
134 if (&ff != this) |
|
135 { |
|
136 fw = ff.fw; |
|
137 prec = ff.prec; |
|
138 fmt = ff.fmt; |
|
139 up = ff.up; |
|
140 sp = ff.sp; |
|
141 } |
|
142 |
|
143 return *this; |
|
144 } |
|
145 |
|
146 ~float_format (void) { } |
|
147 |
|
148 float_format& scientific (void) { fmt = std::ios::scientific; return *this; } |
|
149 float_format& fixed (void) { fmt = std::ios::fixed; return *this; } |
|
150 float_format& general (void) { fmt = 0; return *this; } |
|
151 |
|
152 float_format& uppercase (void) { up = std::ios::uppercase; return *this; } |
|
153 float_format& lowercase (void) { up = 0; return *this; } |
|
154 |
|
155 float_format& precision (int p) { prec = p; return *this; } |
|
156 |
|
157 float_format& width (int w) { fw = w; return *this; } |
|
158 |
|
159 float_format& trailing_zeros (bool tz = true) |
|
160 { sp = tz ? std::ios::showpoint : 0; return *this; } |
|
161 |
|
162 friend std::ostream& operator << (std::ostream& os, |
|
163 const pr_formatted_float& pff); |
|
164 |
|
165 private: |
|
166 |
|
167 // Field width. Zero means as wide as necessary. |
|
168 int fw; |
|
169 |
|
170 // Precision. |
|
171 int prec; |
|
172 |
|
173 // Format. |
|
174 int fmt; |
|
175 |
|
176 // E or e. |
|
177 int up; |
|
178 |
|
179 // Show trailing zeros. |
|
180 int sp; |
|
181 }; |
|
182 |
|
183 class |
|
184 pr_formatted_float |
|
185 { |
|
186 public: |
|
187 |
|
188 const float_format& f; |
|
189 |
|
190 double val; |
|
191 |
|
192 pr_formatted_float (const float_format& f_arg, double val_arg) |
|
193 : f (f_arg), val (val_arg) { } |
|
194 }; |
|
195 |
|
196 std::ostream& |
|
197 operator << (std::ostream& os, const pr_formatted_float& pff) |
|
198 { |
3682
|
199 if (pff.f.fw >= 0) |
3608
|
200 os << std::setw (pff.f.fw); |
|
201 |
3682
|
202 if (pff.f.prec >= 0) |
3608
|
203 os << std::setprecision (pff.f.prec); |
|
204 |
3775
|
205 std::ios::fmtflags oflags = |
|
206 os.flags (static_cast<std::ios::fmtflags> |
|
207 (pff.f.fmt | pff.f.up | pff.f.sp)); |
3608
|
208 |
|
209 os << pff.val; |
|
210 |
|
211 os.flags (oflags); |
|
212 |
|
213 return os; |
|
214 } |
|
215 |
|
216 // Current format for real numbers and the real part of complex |
|
217 // numbers. |
|
218 static float_format *curr_real_fmt = 0; |
|
219 |
|
220 // Current format for the imaginary part of complex numbers. |
|
221 static float_format *curr_imag_fmt = 0; |
1309
|
222 |
1
|
223 static double |
164
|
224 pr_max_internal (const Matrix& m) |
1
|
225 { |
|
226 int nr = m.rows (); |
|
227 int nc = m.columns (); |
|
228 |
3130
|
229 double result = -DBL_MAX; |
1
|
230 |
|
231 for (int j = 0; j < nc; j++) |
|
232 for (int i = 0; i < nr; i++) |
|
233 { |
3608
|
234 double val = m(i,j); |
4025
|
235 if (xisinf (val) || octave_is_NaN_or_NA (val)) |
1
|
236 continue; |
|
237 |
|
238 if (val > result) |
|
239 result = val; |
|
240 } |
3608
|
241 |
1
|
242 return result; |
|
243 } |
|
244 |
|
245 static double |
164
|
246 pr_min_internal (const Matrix& m) |
1
|
247 { |
|
248 int nr = m.rows (); |
|
249 int nc = m.columns (); |
|
250 |
|
251 double result = DBL_MAX; |
|
252 |
|
253 for (int j = 0; j < nc; j++) |
|
254 for (int i = 0; i < nr; i++) |
|
255 { |
3608
|
256 double val = m(i,j); |
4025
|
257 if (xisinf (val) || octave_is_NaN_or_NA (val)) |
1
|
258 continue; |
|
259 |
|
260 if (val < result) |
|
261 result = val; |
|
262 } |
3608
|
263 |
1
|
264 return result; |
|
265 } |
|
266 |
1658
|
267 // XXX FIXME XXX -- it would be nice to share more code among these |
|
268 // functions,.. |
|
269 |
1
|
270 static void |
3611
|
271 set_real_format (bool sign, int digits, bool inf_or_nan, bool int_only, |
1658
|
272 int &fw) |
1
|
273 { |
3608
|
274 static float_format fmt; |
1
|
275 |
2165
|
276 int prec = Voutput_precision; |
1
|
277 |
|
278 int ld, rd; |
|
279 |
|
280 if (bank_format) |
|
281 { |
|
282 fw = digits < 0 ? 4 : digits + 3; |
|
283 if (inf_or_nan && fw < 3) |
|
284 fw = 3; |
|
285 fw += sign; |
|
286 rd = 2; |
|
287 } |
1282
|
288 else if (hex_format) |
|
289 { |
|
290 fw = 2 * sizeof (double); |
|
291 rd = 0; |
|
292 } |
1309
|
293 else if (bit_format) |
|
294 { |
|
295 fw = 8 * sizeof (double); |
|
296 rd = 0; |
|
297 } |
3611
|
298 else if (inf_or_nan || int_only) |
1
|
299 { |
|
300 fw = digits; |
|
301 if (inf_or_nan && fw < 3) |
|
302 fw = 3; |
|
303 fw += sign; |
3682
|
304 rd = fw; |
1
|
305 } |
|
306 else |
|
307 { |
|
308 if (digits > 0) |
|
309 { |
|
310 ld = digits; |
1658
|
311 rd = prec > digits ? prec - digits : prec; |
1
|
312 digits++; |
|
313 } |
|
314 else |
|
315 { |
|
316 ld = 1; |
1658
|
317 rd = prec > digits ? prec - digits : prec; |
1
|
318 digits = -digits + 1; |
|
319 } |
|
320 |
|
321 fw = ld + 1 + rd; |
|
322 if (inf_or_nan && fw < 3) |
|
323 fw = 3; |
|
324 fw += sign; |
|
325 } |
|
326 |
1309
|
327 if (! (bank_format || hex_format || bit_format) |
4509
|
328 && (fw > Voutput_max_field_width || print_e || print_g)) |
1
|
329 { |
4509
|
330 if (print_g) |
|
331 fmt = float_format (); |
|
332 else |
|
333 { |
|
334 int exp_field = 4; |
|
335 if (digits > 100) |
|
336 exp_field++; |
1
|
337 |
4509
|
338 fw = 2 + prec + exp_field; |
|
339 if (inf_or_nan && fw < 3) |
|
340 fw = 3; |
|
341 fw += sign; |
1
|
342 |
4509
|
343 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
344 } |
3608
|
345 |
1
|
346 if (print_big_e) |
3608
|
347 fmt.uppercase (); |
1
|
348 } |
3611
|
349 else if (inf_or_nan || int_only) |
|
350 fmt = float_format (fw, rd); |
1
|
351 else |
3608
|
352 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
353 |
3608
|
354 curr_real_fmt = &fmt; |
1
|
355 } |
|
356 |
1658
|
357 static void |
|
358 set_format (double d, int& fw) |
|
359 { |
|
360 curr_real_fmt = 0; |
|
361 curr_imag_fmt = 0; |
|
362 |
|
363 if (free_format) |
|
364 return; |
|
365 |
2387
|
366 bool sign = (d < 0.0); |
1658
|
367 |
2387
|
368 bool inf_or_nan = (xisinf (d) || xisnan (d)); |
1658
|
369 |
3611
|
370 bool int_only = (! inf_or_nan && D_NINT (d) == d); |
1658
|
371 |
|
372 double d_abs = d < 0.0 ? -d : d; |
|
373 |
2800
|
374 int digits = (inf_or_nan || d_abs == 0.0) |
|
375 ? 0 : static_cast<int> (floor (log10 (d_abs) + 1.0)); |
1658
|
376 |
3611
|
377 set_real_format (sign, digits, inf_or_nan, int_only, fw); |
1658
|
378 } |
|
379 |
1
|
380 static inline void |
|
381 set_format (double d) |
|
382 { |
|
383 int fw; |
|
384 set_format (d, fw); |
|
385 } |
|
386 |
|
387 static void |
2387
|
388 set_real_matrix_format (bool sign, int x_max, int x_min, |
|
389 bool inf_or_nan, int int_or_inf_or_nan, int& fw) |
1
|
390 { |
3608
|
391 static float_format fmt; |
1
|
392 |
2165
|
393 int prec = Voutput_precision; |
1
|
394 |
|
395 int ld, rd; |
|
396 |
|
397 if (bank_format) |
|
398 { |
|
399 int digits = x_max > x_min ? x_max : x_min; |
|
400 fw = digits <= 0 ? 4 : digits + 3; |
|
401 if (inf_or_nan && fw < 3) |
|
402 fw = 3; |
|
403 fw += sign; |
|
404 rd = 2; |
|
405 } |
1282
|
406 else if (hex_format) |
|
407 { |
|
408 fw = 2 * sizeof (double); |
|
409 rd = 0; |
|
410 } |
1309
|
411 else if (bit_format) |
|
412 { |
|
413 fw = 8 * sizeof (double); |
|
414 rd = 0; |
|
415 } |
4509
|
416 else if (Vfixed_point_format && ! print_g) |
3268
|
417 { |
|
418 rd = prec; |
|
419 fw = rd + 2; |
|
420 if (inf_or_nan && fw < 3) |
|
421 fw = 3; |
|
422 fw += sign; |
|
423 } |
1715
|
424 else if (int_or_inf_or_nan) |
1
|
425 { |
|
426 int digits = x_max > x_min ? x_max : x_min; |
|
427 fw = digits <= 0 ? 1 : digits; |
|
428 if (inf_or_nan && fw < 3) |
|
429 fw = 3; |
|
430 fw += sign; |
3682
|
431 rd = fw; |
1
|
432 } |
|
433 else |
|
434 { |
|
435 int ld_max, rd_max; |
|
436 if (x_max > 0) |
|
437 { |
|
438 ld_max = x_max; |
1658
|
439 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
440 x_max++; |
|
441 } |
|
442 else |
|
443 { |
|
444 ld_max = 1; |
1658
|
445 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
446 x_max = -x_max + 1; |
|
447 } |
|
448 |
|
449 int ld_min, rd_min; |
|
450 if (x_min > 0) |
|
451 { |
|
452 ld_min = x_min; |
1658
|
453 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
454 x_min++; |
|
455 } |
|
456 else |
|
457 { |
|
458 ld_min = 1; |
1658
|
459 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
460 x_min = -x_min + 1; |
|
461 } |
|
462 |
|
463 ld = ld_max > ld_min ? ld_max : ld_min; |
|
464 rd = rd_max > rd_min ? rd_max : rd_min; |
|
465 |
|
466 fw = ld + 1 + rd; |
|
467 if (inf_or_nan && fw < 3) |
|
468 fw = 3; |
|
469 fw += sign; |
|
470 } |
|
471 |
1658
|
472 if (! (bank_format || hex_format || bit_format) |
3105
|
473 && (print_e |
4509
|
474 || print_g |
3105
|
475 || (! Vfixed_point_format && fw > Voutput_max_field_width))) |
1
|
476 { |
4509
|
477 if (print_g) |
|
478 fmt = float_format (); |
|
479 else |
|
480 { |
|
481 int exp_field = 4; |
|
482 if (x_max > 100 || x_min > 100) |
|
483 exp_field++; |
1
|
484 |
4509
|
485 fw = 2 + prec + exp_field; |
|
486 if (inf_or_nan && fw < 3) |
|
487 fw = 3; |
|
488 fw += sign; |
1
|
489 |
4509
|
490 fmt = float_format (fw, prec - 1, std::ios::scientific); |
|
491 } |
3608
|
492 |
1
|
493 if (print_big_e) |
3608
|
494 fmt.uppercase (); |
1
|
495 } |
3611
|
496 else if (int_or_inf_or_nan) |
|
497 fmt = float_format (fw, rd); |
1
|
498 else |
3608
|
499 fmt = float_format (fw, rd, std::ios::fixed); |
1
|
500 |
3608
|
501 curr_real_fmt = &fmt; |
1
|
502 } |
|
503 |
1658
|
504 static void |
3105
|
505 set_format (const Matrix& m, int& fw, double& scale) |
1658
|
506 { |
|
507 curr_real_fmt = 0; |
|
508 curr_imag_fmt = 0; |
|
509 |
|
510 if (free_format) |
|
511 return; |
|
512 |
4431
|
513 bool sign = m.any_element_is_negative (true); |
1658
|
514 |
2387
|
515 bool inf_or_nan = m.any_element_is_inf_or_nan (); |
1658
|
516 |
2387
|
517 bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan (); |
1658
|
518 |
2387
|
519 Matrix m_abs = m.abs (); |