GNU Octave  9.1.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-2024 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 OCTINTERP_API int output_precision ();
44 
45 extern OCTINTERP_API void set_output_prec (int prec);
46 
47 class
48 OCTINTERP_API
50 {
51 public:
52 
53  float_format (int w = 0, int p = output_precision (), int f = 0)
54  : m_fw (w), m_ex (0), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
55 
56  float_format (int w, int e, int p, int f)
57  : m_fw (w), m_ex (e), m_prec (p), m_fmt (f), m_up (0), m_sp (0) { }
58 
59  float_format (const float_format&) = default;
60 
61  float_format& operator = (const float_format&) = default;
62 
63  ~float_format () = default;
64 
66  {
67  m_fmt = std::ios::scientific;
68  return *this;
69  }
70 
72  {
73  m_fmt = std::ios::fixed;
74  return *this;
75  }
76 
78  {
79  m_fmt = 0;
80  return *this;
81  }
82 
84  {
85  m_up = std::ios::uppercase;
86  return *this;
87  }
88 
90  {
91  m_up = 0;
92  return *this;
93  }
94 
96  {
97  m_prec = p;
98  return *this;
99  }
100 
102  {
103  m_fw = w;
104  return *this;
105  }
106 
108  {
109  m_ex = w;
110  return *this;
111  }
112 
113  float_format& trailing_zeros (bool tz = true)
114 
115  {
116  m_sp = (tz ? std::ios::showpoint : 0);
117  return *this;
118  }
119 
120  std::ios::fmtflags format_flags () const
121  {
122  return static_cast<std::ios::fmtflags> (m_fmt | m_up | m_sp);
123  }
124 
125  int format () const
126  {
127  return m_fmt;
128  }
129 
130  bool is_scientific () const
131  {
132  return m_fmt == std::ios::scientific;
133  }
134 
135  bool is_fixed () const
136  {
137  return m_fmt == std::ios::fixed;
138  }
139 
140  bool is_general () const
141  {
142  return m_fmt == 0;
143  }
144 
145  bool is_uppercase () const
146  {
147  return m_up == std::ios::uppercase;
148  }
149 
150  bool is_lowercase () const
151  {
152  return m_up == 0;
153  }
154 
155  int precision () const
156  {
157  return m_prec;
158  }
159 
160  int width () const
161  {
162  return m_fw;
163  }
164 
165  int exponent_width () const
166  {
167  return m_ex;
168  }
169 
170  bool show_trailing_zeros () const
171  {
172  return m_sp == std::ios::showpoint;
173  }
174 
175  template <typename T>
176  friend std::ostream&
177  operator << (std::ostream& os, const pr_engineering_float<T>& pef);
178 
179  template <typename T>
180  friend std::ostream&
181  operator << (std::ostream& os, const pr_formatted_float<T>& pff);
182 
183  template <typename T>
184  friend std::ostream&
185  operator << (std::ostream& os, const pr_rational_float<T>& prf);
186 
187 private:
188 
189  // Field width. Zero means as wide as necessary.
190  int m_fw;
191 
192  // Exponent Field width. Zero means as wide as necessary.
193  int m_ex;
194 
195  // Precision.
196  int m_prec;
197 
198  // Format.
199  int m_fmt;
200 
201  // E or e.
202  int m_up;
203 
204  // Show trailing zeros.
205  int m_sp;
206 };
207 
208 class
209 OCTINTERP_API
211 {
212 public:
213 
214  float_display_format () = default;
215 
216  float_display_format (double scale, const float_format& real_fmt,
217  const float_format& imag_fmt = float_format ())
218  : m_scale (scale), m_real_fmt (real_fmt), m_imag_fmt (imag_fmt)
219  { }
220 
221  explicit float_display_format (const float_format& real_fmt,
222  const float_format& imag_fmt = float_format ())
223  : m_scale (1.0), m_real_fmt (real_fmt), m_imag_fmt (imag_fmt)
224  { }
225 
227 
228  float_display_format& operator = (const float_display_format&) = default;
229 
230  ~float_display_format () = default;
231 
232  double scale_factor () const { return m_scale; }
233 
234  float_format real_format () const { return m_real_fmt; }
235 
236  float_format imag_format () const { return m_imag_fmt; }
237 
238  void set_precision (int prec)
239  {
240  m_real_fmt.precision (prec);
241  m_imag_fmt.precision (prec);
242  }
243 
244 private:
245 
246  double m_scale = 1.0;
247 
248  float_format m_real_fmt;
249 
250  float_format m_imag_fmt;
251 };
252 
253 #endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
float_display_format(const float_display_format &)=default
float_format imag_format() const
Definition: pr-flt-fmt.h:236
void set_precision(int prec)
Definition: pr-flt-fmt.h:238
float_format real_format() const
Definition: pr-flt-fmt.h:234
float_display_format(double scale, const float_format &real_fmt, const float_format &imag_fmt=float_format())
Definition: pr-flt-fmt.h:216
float_display_format(const float_format &real_fmt, const float_format &imag_fmt=float_format())
Definition: pr-flt-fmt.h:221
~float_display_format()=default
double scale_factor() const
Definition: pr-flt-fmt.h:232
float_display_format()=default
bool is_general() const
Definition: pr-flt-fmt.h:140
bool is_lowercase() const
Definition: pr-flt-fmt.h:150
bool is_uppercase() const
Definition: pr-flt-fmt.h:145
float_format & fixed()
Definition: pr-flt-fmt.h:71
bool is_scientific() const
Definition: pr-flt-fmt.h:130
float_format(int w=0, int p=output_precision(), int f=0)
Definition: pr-flt-fmt.h:53
float_format & trailing_zeros(bool tz=true)
Definition: pr-flt-fmt.h:113
float_format & exponent_width(int w)
Definition: pr-flt-fmt.h:107
bool show_trailing_zeros() const
Definition: pr-flt-fmt.h:170
bool is_fixed() const
Definition: pr-flt-fmt.h:135
float_format & scientific()
Definition: pr-flt-fmt.h:65
int precision() const
Definition: pr-flt-fmt.h:155
float_format(int w, int e, int p, int f)
Definition: pr-flt-fmt.h:56
int exponent_width() const
Definition: pr-flt-fmt.h:165
float_format & uppercase()
Definition: pr-flt-fmt.h:83
int width() const
Definition: pr-flt-fmt.h:160
float_format & precision(int p)
Definition: pr-flt-fmt.h:95
float_format(const float_format &)=default
std::ios::fmtflags format_flags() const
Definition: pr-flt-fmt.h:120
int format() const
Definition: pr-flt-fmt.h:125
~float_format()=default
float_format & general()
Definition: pr-flt-fmt.h:77
float_format & width(int w)
Definition: pr-flt-fmt.h:101
float_format & lowercase()
Definition: pr-flt-fmt.h:89
void scale(Matrix &m, double x, double y, double z)
Definition: graphics.cc:5500
F77_RET_T const F77_DBLE const F77_DBLE * f
std::complex< double > w(std::complex< double > z, double relerr=0)
int output_precision()
Definition: pr-flt-fmt.cc:40
void set_output_prec(int prec)
Definition: pr-flt-fmt.cc:46