format_float.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_FORMAT_FLOAT_H
24 #define O2SCL_FORMAT_FLOAT_H
25 
26 /** \file format_float.h
27  \brief File defining \ref o2scl::format_float
28 */
29 #include <cstdio>
30 #include <cstdlib>
31 #include <iostream>
32 #include <o2scl/err_hnd.h>
33 #include <o2scl/misc.h>
34 #include <o2scl/string_conv.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /** \brief Format a floating point number into a Latex or HTML string
41 
42  This class formats floating point strings into something
43  useful for HTML or Latex documents. For basic use,
44  simply call either \ref html_mode() or \ref latex_mode() and
45  then use \ref convert().
46 
47  The base-10 logarithm of the smallest and largest numbers to be
48  represented without a string akin to "times 10 to the nth power"
49  can be specified in \ref set_exp_limits(). The number
50  of significant figures can be specified with set_sig_figs() (the
51  default is 5).
52 
53  To force \ref convert() to adds zeros to the right side of the
54  mantissa to guarantee that the requested number of significant
55  digits is given, call \ref set_pad_zeros() with a <tt>true</tt>
56  argument.
57 
58  To force scientific notation for all numbers, set the maximum
59  exponent to be smaller than the minimum exponent.
60 
61  \note This function does not warn the user if the number
62  of significant figures requested is larger than the machine
63  precision.
64  \note If the absolute magnitude for either the minimum or
65  maximum exponent is larger than or equal to the number of
66  significant figures, then rounding will automatically
67  occur.
68 
69  The format for a normal number is
70  \verbatim
71  prefix (sign-string) number suffix
72  \endverbatim
73  and in scientific notation is
74  \verbatim
75  sci-prefix (sci-sign-string) number times-string
76  exp-prefix (exp-sign-string) exponent exp-suffix sci-suffix
77  \endverbatim
78 
79  \b Examples
80 
81  The code
82  \code
83  format_float fd;
84  fd.latex_mode();
85  cout << fd.convert(-sqrt(2.0)*1.0e-5) << endl;
86  cout << fd.convert(sqrt(2.0)*1.0e-2) << endl;
87  cout << fd.convert(-sqrt(2.0)*1.0e-1) << endl;
88  \endcode
89  outputs
90  \verbatim
91  $-$1.4142 $\\times 10^{-5}$
92  0.014142
93  $-$0.14142
94  \endverbatim
95  and the code
96  \code
97  format_float fd;
98  fd.html_mode();
99  fd.set_sig_figs(7);
100  fd.set_pad_zeros(true);
101  cout << fd.convert(1.414e-5) << endl;
102  cout << fd.convert(1.414e-2) << endl;
103  \endcode
104  outputs
105  \verbatim
106  1.414000 &times; 10<sup>-5</sup>
107  0.01414000
108  \endverbatim
109 
110  \future Handle inf's and nan's correctly.
111  \future Allow change of string for the "+" sign for the exponent
112  */
113  class format_float {
114 
115  protected:
116 
117  /// \name Base text settings
118  //@{
119  /// Prefix (default "")
120  std::string prefx;
121  /// Suffix (default "")
122  std::string suffx;
123  /// Sign string (default "-")
124  std::string sgn;
125  /// Sign string in scientific mode (default "-")
126  std::string sci_sgn;
127  /// Sign string for exponent in scientific mode (default "-")
128  std::string exp_sgn;
129  /// Prefix in scientific mode (default "")
130  std::string sci_prefx;
131  /// Suffix in scientific mode (default "")
132  std::string sci_suffx;
133  /// Exponent prefix (default "")
134  std::string exp_prefx;
135  /// Exponent suffix (default "")
136  std::string exp_suffx;
137  /// Times symbol for scientific mode (default " x ")
138  std::string tmes;
139  /// String for numbers which are not finite (default "Nan")
140  std::string not_finte;
141  /// String for zeros (default "0")
142  std::string zeros;
143  //@}
144 
145  /// \name Other settings
146  //@{
147  /// Number of significant figures (default 5)
148  size_t sig_fgs;
149  /// Number of digits in exponent (default 0 which prints the minimum)
150  size_t exp_dgs;
151  /// Lower limit for automatic mode (default -2)
152  int ex_mn;
153  /// Upper limit for automatic mode (default 3)
154  int ex_mx;
155  /// If true, pad with zeros (default false)
156  bool pad_zeros;
157  /** \brief If true, show the sign of the exponent when
158  it's positive (default false)
159  */
161  /// The decimal point (default <tt>'.'</tt>)
162  std::string dpt;
163  //@}
164 
165  /** \brief Remove extra zeros and decimal point from mantisaa
166  */
167  int remove_zeros_dpt(std::string &s);
168 
169  public:
170 
171  format_float();
172 
173  /// \name Basic usage
174  //@{
175  /** \brief Set HTML mode
176 
177  This function is equivalent to the settings:
178  \code
179  set_prefix("");
180  set_sign("-");
181  set_suffix("");
182  set_sci_prefix("");
183  set_times(" &times; ");
184  set_exp_prefix("10<sup>");
185  set_exp_sign("-");
186  set_sci_sign("-");
187  set_exp_suffix("</sup>");
188  set_sci_suffix("");
189  set_not_finite("Nan");
190  set_zero("0");
191  set_exp_digits(0);
192  set_show_exp_sign(false);
193  \endcode
194  */
195  void html_mode();
196 
197  /** \brief Set Latex mode
198 
199  This function is equivalent to the settings:
200  \code
201  set_prefix("");
202  set_sign("$-$");
203  set_suffix("");
204  set_sci_prefix("");
205  set_times(" $\\times ");
206  set_exp_prefix("10^{");
207  set_exp_sign("-");
208  set_sci_sign("$-$");
209  set_exp_suffix("}");
210  set_sci_suffix("");
211  set_not_finite("Nan");
212  set_zero("0");
213  set_exp_digits(0);
214  set_show_exp_sign(false);
215  \endcode
216 
217  \note This setting assumes that the user is not
218  in LaTeX's "math mode" already.
219  */
220  void latex_mode();
221 
222  /** \brief C-like mode
223 
224  This reproduces the default settings of \c cout in automatic
225  mode. Obviously it is faster to use iostreams than to format
226  numbers with this class. Nevertheless, this mode is very
227  useful for testing to ensure that this class processes the
228  numbers correctly at the requested precision.
229 
230  This function is equivalent to the settings:
231  \code
232  set_prefix("");
233  set_sign("-");
234  set_suffix("");
235  set_sci_prefix("");
236  set_times("e");
237  set_exp_prefix("");
238  set_exp_sign("-");
239  set_sci_sign("-");
240  set_exp_suffix("");
241  set_sci_suffix("");
242  set_not_finite("NaN");
243  set_zero("0");
244  set_exp_limits(-4,5);
245  set_exp_digits(2);
246  set_show_exp_sign(true);
247  \endcode
248  */
249  void c_mode();
250 
251  /// Convert a floating point number to a string
252  std::string convert(double x, bool debug=false);
253  //@}
254 
255  /** \name Set text settings
256 
257  These are modified by the functions html_mode() and latex_mode()
258  */
259  //@{
260  /// set prefix
261  void set_prefix(std::string prefix) {
262  prefx=prefix;
263  return;
264  }
265 
266  /// Set suffix
267  void set_suffix(std::string suffix) {
268  suffx=suffix;
269  return;
270  }
271 
272  /// Set prefix for scientific notation
273  void set_sci_prefix(std::string sci_prefix) {
274  sci_prefix=sci_prefx;
275  return;
276  }
277 
278  /// Set suffix for scientific notation
279  void set_sci_suffix(std::string sci_suffix) {
280  sci_suffx=sci_suffix;
281  return;
282  }
283 
284  /// Set prefix for exponent
285  void set_exp_prefix(std::string exp_prefix) {
286  exp_prefx=exp_prefix;
287  return;
288  }
289 
290  /// Set suffix for exponent
291  void set_exp_suffix(std::string exp_suffix) {
292  exp_suffx=exp_suffix;
293  return;
294  }
295 
296  /// Set sign
297  void set_sign(std::string sign) {
298  sgn=sign;
299  return;
300  }
301 
302  /// Set sign for exponent
303  void set_exp_sign(std::string exp_sign) {
304  exp_sgn=exp_sign;
305  return;
306  }
307 
308  /// Set policy for showing positive exponent sign
309  void set_show_exp_sign(bool b) {
310  show_exp_sgn=b;
311  return;
312  }
313 
314  /// Set sign for scientific notation
315  void set_sci_sign(std::string sci_sign) {
316  sci_sgn=sci_sign;
317  return;
318  }
319 
320  /// Set times
321  void set_times(std::string times) {
322  tmes=times;
323  return;
324  }
325 
326  /// Set zero
327  void set_zero(std::string zero) {
328  zeros=zero;
329  return;
330  }
331 
332  /// Set string for numbers which are not finite
333  void set_not_finite(std::string not_finite) {
334  not_finte=not_finite;
335  return;
336  }
337  //@}
338 
339  /** \name Set other settings
340 
341  These are not modified by the functions html_mode() and latex_mode()
342  */
343  //@{
344  /// Set the exponent limits
345  void set_exp_limits(int min, int max) {
346  ex_mn=min;
347  ex_mx=max;
348  return;
349  }
350 
351  /** \brief Set the number of significant figures (argument has
352  maximum of 15 and cannot be zero)
353  */
354  void set_sig_figs(size_t sig_figs) {
355  if (sig_fgs==0 || sig_figs>15) {
356  O2SCL_ERR2("Argument must be less than or equal to 15",
357  "in format_float::set_sig_figs().",exc_einval);
358  }
359  sig_fgs=sig_figs;
360  return;
361  }
362 
363  /// Set pad zeros
364  void set_pad_zeros(bool pad) {
365  pad_zeros=pad;
366  return;
367  }
368 
369  /// Set decimal point
370  void set_dec_point(std::string dec_point) {
371  dpt=dec_point;
372  return;
373  }
374 
375  /// Set minimum number of digits in the exponent
376  void set_exp_digits(size_t d) {
377  exp_dgs=d;
378  return;
379  }
380  //@}
381 
382  /** \name Get text settings
383 
384  These are modified by the functions html_mode() and latex_mode()
385  */
386  //@{
387  /// Get prefix
388  std::string get_prefix() {
389  return prefx;
390  }
391 
392  /// Get suffix
393  std::string get_suffix() {
394  return suffx;
395  }
396 
397  /// Get prefix for scientific notation
398  std::string get_sci_prefix() {
399  return sci_prefx;
400  }
401 
402  /// Get suffix for scientific notation
403  std::string get_sci_suffix() {
404  return sci_suffx;
405  }
406 
407  /// Get prefix for exponent
408  std::string get_exp_prefix() {
409  return exp_prefx;
410  }
411 
412  /// Get suffix for exponent
413  std::string get_exp_suffix() {
414  return exp_suffx;
415  }
416 
417  /// Get sign
418  std::string get_sign() {
419  return sgn;
420  }
421 
422  /// Get sign for exponent
423  std::string get_exp_sign() {
424  return exp_sgn;
425  }
426 
427  /// Get sign for scientific notation
428  std::string get_sci_sign() {
429  return sci_sgn;
430  }
431 
432  /// Get times
433  std::string get_times() {
434  return tmes;
435  }
436 
437  /// Get zero
438  std::string get_zero() {
439  return zeros;
440  }
441 
442  /// Get string for numbers which are not finite
443  std::string get_not_finite() {
444  return not_finte;
445  }
446  //@}
447 
448  /** \name Get other settings
449 
450  These are not modified by the functions html_mode() and latex_mode()
451  */
452  //@{
453  /// Get minimum exponent
454  int get_exp_min() {
455  return ex_mn;
456  }
457 
458  /// Get maximum exponent
459  int get_exp_max() {
460  return ex_mx;
461  }
462 
463  /// Get sig_figs
464  size_t get_sig_figs() {
465  return sig_fgs;
466  }
467 
468  /// Get pad_zeros
469  bool get_pad_zeros() {
470  return pad_zeros;
471  }
472 
473  /// Get decimal point
474  std::string get_dec_point() {
475  return dpt;
476  }
477  //@}
478 
479  };
480 
481 #ifndef DOXYGEN_NO_O2NS
482 }
483 #endif
484 
485 #endif
std::string get_sign()
Get sign.
Definition: format_float.h:418
void set_dec_point(std::string dec_point)
Set decimal point.
Definition: format_float.h:370
void set_pad_zeros(bool pad)
Set pad zeros.
Definition: format_float.h:364
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
std::string exp_suffx
Exponent suffix (default "")
Definition: format_float.h:136
std::string tmes
Times symbol for scientific mode (default " x ")
Definition: format_float.h:138
std::string get_prefix()
Get prefix.
Definition: format_float.h:388
std::string suffx
Suffix (default "")
Definition: format_float.h:122
std::string zeros
String for zeros (default "0")
Definition: format_float.h:142
int get_exp_max()
Get maximum exponent.
Definition: format_float.h:459
void set_exp_suffix(std::string exp_suffix)
Set suffix for exponent.
Definition: format_float.h:291
invalid argument supplied by user
Definition: err_hnd.h:59
int get_exp_min()
Get minimum exponent.
Definition: format_float.h:454
std::string get_sci_prefix()
Get prefix for scientific notation.
Definition: format_float.h:398
std::string get_dec_point()
Get decimal point.
Definition: format_float.h:474
std::string exp_prefx
Exponent prefix (default "")
Definition: format_float.h:134
std::string sci_prefx
Prefix in scientific mode (default "")
Definition: format_float.h:130
std::string get_suffix()
Get suffix.
Definition: format_float.h:393
void set_sci_suffix(std::string sci_suffix)
Set suffix for scientific notation.
Definition: format_float.h:279
bool get_pad_zeros()
Get pad_zeros.
Definition: format_float.h:469
std::string get_not_finite()
Get string for numbers which are not finite.
Definition: format_float.h:443
void set_sign(std::string sign)
Set sign.
Definition: format_float.h:297
std::string dpt
The decimal point (default &#39;.&#39;)
Definition: format_float.h:162
std::string get_zero()
Get zero.
Definition: format_float.h:438
void set_zero(std::string zero)
Set zero.
Definition: format_float.h:327
int ex_mx
Upper limit for automatic mode (default 3)
Definition: format_float.h:154
void set_exp_sign(std::string exp_sign)
Set sign for exponent.
Definition: format_float.h:303
void set_prefix(std::string prefix)
set prefix
Definition: format_float.h:261
bool show_exp_sgn
If true, show the sign of the exponent when it&#39;s positive (default false)
Definition: format_float.h:160
void set_sci_prefix(std::string sci_prefix)
Set prefix for scientific notation.
Definition: format_float.h:273
void c_mode()
C-like mode.
std::string sgn
Sign string (default "-")
Definition: format_float.h:124
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
std::string sci_sgn
Sign string in scientific mode (default "-")
Definition: format_float.h:126
Format a floating point number into a Latex or HTML string.
Definition: format_float.h:113
size_t get_sig_figs()
Get sig_figs.
Definition: format_float.h:464
std::string not_finte
String for numbers which are not finite (default "Nan")
Definition: format_float.h:140
void latex_mode()
Set Latex mode.
std::string exp_sgn
Sign string for exponent in scientific mode (default "-")
Definition: format_float.h:128
void set_sci_sign(std::string sci_sign)
Set sign for scientific notation.
Definition: format_float.h:315
std::string prefx
Prefix (default "")
Definition: format_float.h:120
void set_exp_digits(size_t d)
Set minimum number of digits in the exponent.
Definition: format_float.h:376
void set_not_finite(std::string not_finite)
Set string for numbers which are not finite.
Definition: format_float.h:333
std::string convert(double x, bool debug=false)
Convert a floating point number to a string.
std::string get_times()
Get times.
Definition: format_float.h:433
std::string get_exp_sign()
Get sign for exponent.
Definition: format_float.h:423
std::string get_exp_prefix()
Get prefix for exponent.
Definition: format_float.h:408
void set_show_exp_sign(bool b)
Set policy for showing positive exponent sign.
Definition: format_float.h:309
void set_suffix(std::string suffix)
Set suffix.
Definition: format_float.h:267
std::string get_sci_sign()
Get sign for scientific notation.
Definition: format_float.h:428
int remove_zeros_dpt(std::string &s)
Remove extra zeros and decimal point from mantisaa.
size_t exp_dgs
Number of digits in exponent (default 0 which prints the minimum)
Definition: format_float.h:150
std::string sci_suffx
Suffix in scientific mode (default "")
Definition: format_float.h:132
void set_times(std::string times)
Set times.
Definition: format_float.h:321
std::string get_exp_suffix()
Get suffix for exponent.
Definition: format_float.h:413
void set_exp_prefix(std::string exp_prefix)
Set prefix for exponent.
Definition: format_float.h:285
bool pad_zeros
If true, pad with zeros (default false)
Definition: format_float.h:156
std::string get_sci_suffix()
Get suffix for scientific notation.
Definition: format_float.h:403
void html_mode()
Set HTML mode.
int ex_mn
Lower limit for automatic mode (default -2)
Definition: format_float.h:152
void set_exp_limits(int min, int max)
Set the exponent limits.
Definition: format_float.h:345
size_t sig_fgs
Number of significant figures (default 5)
Definition: format_float.h:148
void set_sig_figs(size_t sig_figs)
Set the number of significant figures (argument has maximum of 15 and cannot be zero) ...
Definition: format_float.h:354

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).