GNU Octave 7.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-2022 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
34template <typename T>
36
37template <typename T>
39
40template <typename T>
42
43extern OCTINTERP_API int output_precision (void);
44
45extern OCTINTERP_API void set_output_prec (int prec);
46
47class
48OCTINTERP_API
50{
51public:
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 (void) = 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 (void) const
121 {
122 return static_cast<std::ios::fmtflags> (m_fmt | m_up | m_sp);
123 }
124
125 int format (void) const
126 {
127 return m_fmt;
128 }
129
130 bool is_scientific (void) const
131 {
132 return m_fmt == std::ios::scientific;
133 }
134
135 bool is_fixed (void) const
136 {
137 return m_fmt == std::ios::fixed;
138 }
139
140 bool is_general (void) const
141 {
142 return m_fmt == 0;
143 }
144
145 bool is_uppercase (void) const
146 {
147 return m_up == std::ios::uppercase;
148 }
149
150 bool is_lowercase (void) const
151 {
152 return m_up == 0;
153 }
154
155 int precision (void) const
156 {
157 return m_prec;
158 }
159
160 int width (void) const
161 {
162 return m_fw;
163 }
164
165 int exponent_width (void) const
166 {
167 return m_ex;
168 }
169
170 bool show_trailing_zeros (void) 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
187private:
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.
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
208class
209OCTINTERP_API
211{
212public:
213
214 float_display_format (void) = 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 (void) = default;
231
232 double scale_factor (void) const { return m_scale; }
233
234 float_format real_format (void) const { return m_real_fmt; }
235
236 float_format imag_format (void) 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
244private:
245
246 double m_scale;
247
249
251};
252
253#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:238
float_format m_imag_fmt
Definition: pr-flt-fmt.h:250
float_format real_format(void) 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_format m_real_fmt
Definition: pr-flt-fmt.h:248
float_display_format(const float_format &real_fmt, const float_format &imag_fmt=float_format())
Definition: pr-flt-fmt.h:221
~float_display_format(void)=default
float_display_format(void)=default
double scale_factor(void) const
Definition: pr-flt-fmt.h:232
float_format imag_format(void) const
Definition: pr-flt-fmt.h:236
float_format & uppercase(void)
Definition: pr-flt-fmt.h:83
float_format & scientific(void)
Definition: pr-flt-fmt.h:65
int precision(void) const
Definition: pr-flt-fmt.h:155
int width(void) const
Definition: pr-flt-fmt.h:160
float_format(int w=0, int p=output_precision(), int f=0)
Definition: pr-flt-fmt.h:53
int format(void) const
Definition: pr-flt-fmt.h:125
bool is_lowercase(void) const
Definition: pr-flt-fmt.h:150
bool is_fixed(void) const
Definition: pr-flt-fmt.h:135
float_format & fixed(void)
Definition: pr-flt-fmt.h:71
~float_format(void)=default
float_format & exponent_width(int w)
Definition: pr-flt-fmt.h:107
float_format & general(void)
Definition: pr-flt-fmt.h:77
float_format & precision(int p)
Definition: pr-flt-fmt.h:95
float_format(int w, int e, int p, int f)
Definition: pr-flt-fmt.h:56
float_format & width(int w)
Definition: pr-flt-fmt.h:101
float_format & lowercase(void)
Definition: pr-flt-fmt.h:89
std::ios::fmtflags format_flags(void) const
Definition: pr-flt-fmt.h:120
float_format(const float_format &)=default
float_format & trailing_zeros(bool tz=true)
Definition: pr-flt-fmt.h:113
bool is_scientific(void) const
Definition: pr-flt-fmt.h:130
bool is_general(void) const
Definition: pr-flt-fmt.h:140
bool show_trailing_zeros(void) const
Definition: pr-flt-fmt.h:170
int exponent_width(void) const
Definition: pr-flt-fmt.h:165
bool is_uppercase(void) const
Definition: pr-flt-fmt.h:145
void scale(Matrix &m, double x, double y, double z)
Definition: graphics.cc:5893
F77_RET_T const F77_DBLE const F77_DBLE * f
std::complex< double > w(std::complex< double > z, double relerr=0)
OCTINTERP_API void set_output_prec(int prec)
Definition: pr-flt-fmt.cc:46
OCTINTERP_API int output_precision(void)
Definition: pr-flt-fmt.cc:40