GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pr-flt-fmt.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_pr_flt_fmt_h)
27 #define octave_pr_flt_fmt_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iomanip>
32 #include <iosfwd>
33 
34 template <typename T>
36 
37 template <typename T>
38 class pr_formatted_float;
39 
40 template <typename T>
41 class pr_rational_float;
42 
43 extern int output_precision (void);
44 
45 extern void set_output_prec (int prec);
46 
47 class
49 {
50 public:
51 
52  float_format (int w = 0, int p = output_precision (), int f = 0)
53  : m_fw (w), m_ex (0), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
54 
55  float_format (int w, int e, int p, int f)
56  : m_fw (w), m_ex (e), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
57 
58  float_format (const float_format&) = default;
59 
60  float_format& operator = (const float_format&) = default;
61 
62  ~float_format (void) = default;
63 
65  {
66  m_fmt = std::ios::scientific;
67  return *this;
68  }
69 
71  {
72  m_fmt = std::ios::fixed;
73  return *this;
74  }
75 
77  {
78  m_fmt = 0;
79  return *this;
80  }
81 
83  {
84  m_up = std::ios::uppercase;
85  return *this;
86  }
87 
89  {
90  m_up = 0;
91  return *this;
92  }
93 
95  {
96  m_prec = p;
97  return *this;
98  }
99 
101  {
102  m_fw = w;
103  return *this;
104  }
105 
107  {
108  m_ex = w;
109  return *this;
110  }
111 
112  float_format& trailing_zeros (bool tz = true)
113 
114  {
115  m_sp = (tz ? std::ios::showpoint : 0);
116  return *this;
117  }
118 
119  std::ios::fmtflags format_flags (void) const
120  {
121  return static_cast<std::ios::fmtflags> (m_fmt | m_up | m_sp);
122  }
123 
124  int format (void) const
125  {
126  return m_fmt;
127  }
128 
129  bool is_scientific (void) const
130  {
131  return m_fmt == std::ios::scientific;
132  }
133 
134  bool is_fixed (void) const
135  {
136  return m_fmt == std::ios::fixed;
137  }
138 
139  bool is_general (void) const
140  {
141  return m_fmt == 0;
142  }
143 
144  bool is_uppercase (void) const
145  {
146  return m_up == std::ios::uppercase;
147  }
148 
149  bool is_lowercase (void) const
150  {
151  return m_up == 0;
152  }
153 
154  int precision (void) const
155  {
156  return m_prec;
157  }
158 
159  int width (void) const
160  {
161  return m_fw;
162  }
163 
164  int exponent_width (void) const
165  {
166  return m_ex;
167  }
168 
169  bool show_trailing_zeros (void) const
170  {
171  return m_sp == std::ios::showpoint;
172  }
173 
174  template <typename T>
175  friend std::ostream&
176  operator << (std::ostream& os, const pr_engineering_float<T>& pef);
177 
178  template <typename T>
179  friend std::ostream&
180  operator << (std::ostream& os, const pr_formatted_float<T>& pff);
181 
182  template <typename T>
183  friend std::ostream&
184  operator << (std::ostream& os, const pr_rational_float<T>& prf);
185 
186 private:
187 
188  // Field width. Zero means as wide as necessary.
189  int m_fw;
190 
191  // Exponent Field width. Zero means as wide as necessary.
192  int m_ex;
193 
194  // Precision.
195  int m_prec;
196 
197  // Format.
198  int m_fmt;
199 
200  // E or e.
201  int m_up;
202 
203  // Show trailing zeros.
204  int m_sp;
205 };
206 
207 class
209 {
210 public:
211 
212  float_display_format (void) = default;
213 
214  float_display_format (double scale, const float_format& real_fmt,
215  const float_format& imag_fmt = float_format ())
216  : m_scale (scale), m_real_fmt (real_fmt), m_imag_fmt (imag_fmt)
217  { }
218 
219  explicit float_display_format (const float_format& real_fmt,
220  const float_format& imag_fmt = float_format ())
221  : m_scale (1.0), m_real_fmt (real_fmt), m_imag_fmt (imag_fmt)
222  { }
223 
225 
226  float_display_format& operator = (const float_display_format&) = default;
227 
228  ~float_display_format (void) = default;
229 
230  double scale_factor (void) const { return m_scale; }
231 
232  float_format real_format (void) const { return m_real_fmt; }
233 
234  float_format imag_format (void) const { return m_imag_fmt; }
235 
236  void set_precision (int prec)
237  {
238  m_real_fmt.precision (prec);
239  m_imag_fmt.precision (prec);
240  }
241 
242 private:
243 
244  double m_scale;
245 
247 
249 };
250 
251 #endif
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
float_display_format(const float_display_format &)=default
void set_precision(int prec)
Definition: pr-flt-fmt.h:236
float_format m_imag_fmt
Definition: pr-flt-fmt.h:248
float_format real_format(void) const
Definition: pr-flt-fmt.h:232
float_display_format(double scale, const float_format &real_fmt, const float_format &imag_fmt=float_format())
Definition: pr-flt-fmt.h:214
float_format m_real_fmt
Definition: pr-flt-fmt.h:246
float_display_format(const float_format &real_fmt, const float_format &imag_fmt=float_format())
Definition: pr-flt-fmt.h:219
~float_display_format(void)=default
float_display_format(void)=default
double scale_factor(void) const
Definition: pr-flt-fmt.h:230
float_format imag_format(void) const
Definition: pr-flt-fmt.h:234
int precision(void) const
Definition: pr-flt-fmt.h:154
int width(void) const
Definition: pr-flt-fmt.h:159
float_format & fixed(void)
Definition: pr-flt-fmt.h:70
float_format(int w=0, int p=output_precision(), int f=0)
Definition: pr-flt-fmt.h:52
int format(void) const
Definition: pr-flt-fmt.h:124
float_format & trailing_zeros(bool tz=true)
Definition: pr-flt-fmt.h:112
bool is_lowercase(void) const
Definition: pr-flt-fmt.h:149
float_format & scientific(void)
Definition: pr-flt-fmt.h:64
bool is_fixed(void) const
Definition: pr-flt-fmt.h:134
~float_format(void)=default
float_format & exponent_width(int w)
Definition: pr-flt-fmt.h:106
float_format(int w, int e, int p, int f)
Definition: pr-flt-fmt.h:55
float_format & lowercase(void)
Definition: pr-flt-fmt.h:88
std::ios::fmtflags format_flags(void) const
Definition: pr-flt-fmt.h:119
float_format & precision(int p)
Definition: pr-flt-fmt.h:94
float_format(const float_format &)=default
float_format & general(void)
Definition: pr-flt-fmt.h:76
bool is_scientific(void) const
Definition: pr-flt-fmt.h:129
bool is_general(void) const
Definition: pr-flt-fmt.h:139
bool show_trailing_zeros(void) const
Definition: pr-flt-fmt.h:169
float_format & width(int w)
Definition: pr-flt-fmt.h:100
int exponent_width(void) const
Definition: pr-flt-fmt.h:164
bool is_uppercase(void) const
Definition: pr-flt-fmt.h:144
float_format & uppercase(void)
Definition: pr-flt-fmt.h:82
void scale(Matrix &m, double x, double y, double z)
Definition: graphics.cc:5846
F77_RET_T const F77_DBLE const F77_DBLE * f
std::complex< double > w(std::complex< double > z, double relerr=0)
int output_precision(void)
Definition: pr-flt-fmt.cc:40
void set_output_prec(int prec)
Definition: pr-flt-fmt.cc:46