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