Protected Member Functions | List of all members
o2scl::format_float Class Reference

Format a floating point number into a Latex or HTML string. More...

#include <format_float.h>

Public Member Functions

Basic usage
void html_mode ()
 Set HTML mode. More...
 
void latex_mode ()
 Set Latex mode. More...
 
void c_mode ()
 C-like mode. More...
 
std::string convert (double x, bool debug=false)
 Convert a floating point number to a string.
 
Set text settings

These are modified by the functions html_mode() and latex_mode()

void set_prefix (std::string prefix)
 set prefix
 
void set_suffix (std::string suffix)
 Set suffix.
 
void set_sci_prefix (std::string sci_prefix)
 Set prefix for scientific notation.
 
void set_sci_suffix (std::string sci_suffix)
 Set suffix for scientific notation.
 
void set_exp_prefix (std::string exp_prefix)
 Set prefix for exponent.
 
void set_exp_suffix (std::string exp_suffix)
 Set suffix for exponent.
 
void set_sign (std::string sign)
 Set sign.
 
void set_exp_sign (std::string exp_sign)
 Set sign for exponent.
 
void set_show_exp_sign (bool b)
 Set policy for showing positive exponent sign.
 
void set_sci_sign (std::string sci_sign)
 Set sign for scientific notation.
 
void set_times (std::string times)
 Set times.
 
void set_zero (std::string zero)
 Set zero.
 
void set_not_finite (std::string not_finite)
 Set string for numbers which are not finite.
 
Set other settings

These are not modified by the functions html_mode() and latex_mode()

void set_exp_limits (int min, int max)
 Set the exponent limits.
 
void set_sig_figs (size_t sig_figs)
 Set the number of significant figures (argument has maximum of 15 and cannot be zero)
 
void set_pad_zeros (bool pad)
 Set pad zeros.
 
void set_dec_point (std::string dec_point)
 Set decimal point.
 
void set_exp_digits (size_t d)
 Set minimum number of digits in the exponent.
 
Get text settings

These are modified by the functions html_mode() and latex_mode()

std::string get_prefix ()
 Get prefix.
 
std::string get_suffix ()
 Get suffix.
 
std::string get_sci_prefix ()
 Get prefix for scientific notation.
 
std::string get_sci_suffix ()
 Get suffix for scientific notation.
 
std::string get_exp_prefix ()
 Get prefix for exponent.
 
std::string get_exp_suffix ()
 Get suffix for exponent.
 
std::string get_sign ()
 Get sign.
 
std::string get_exp_sign ()
 Get sign for exponent.
 
std::string get_sci_sign ()
 Get sign for scientific notation.
 
std::string get_times ()
 Get times.
 
std::string get_zero ()
 Get zero.
 
std::string get_not_finite ()
 Get string for numbers which are not finite.
 
Get other settings

These are not modified by the functions html_mode() and latex_mode()

int get_exp_min ()
 Get minimum exponent.
 
int get_exp_max ()
 Get maximum exponent.
 
size_t get_sig_figs ()
 Get sig_figs.
 
bool get_pad_zeros ()
 Get pad_zeros.
 
std::string get_dec_point ()
 Get decimal point.
 

Protected Member Functions

int remove_zeros_dpt (std::string &s)
 Remove extra zeros and decimal point from mantisaa.
 

Protected Attributes

Base text settings
std::string prefx
 Prefix (default "")
 
std::string suffx
 Suffix (default "")
 
std::string sgn
 Sign string (default "-")
 
std::string sci_sgn
 Sign string in scientific mode (default "-")
 
std::string exp_sgn
 Sign string for exponent in scientific mode (default "-")
 
std::string sci_prefx
 Prefix in scientific mode (default "")
 
std::string sci_suffx
 Suffix in scientific mode (default "")
 
std::string exp_prefx
 Exponent prefix (default "")
 
std::string exp_suffx
 Exponent suffix (default "")
 
std::string tmes
 Times symbol for scientific mode (default " x ")
 
std::string not_finte
 String for numbers which are not finite (default "Nan")
 
std::string zeros
 String for zeros (default "0")
 
Other settings
size_t sig_fgs
 Number of significant figures (default 5)
 
size_t exp_dgs
 Number of digits in exponent (default 0 which prints the minimum)
 
int ex_mn
 Lower limit for automatic mode (default -2)
 
int ex_mx
 Upper limit for automatic mode (default 3)
 
bool pad_zeros
 If true, pad with zeros (default false)
 
bool show_exp_sgn
 If true, show the sign of the exponent when it's positive (default false)
 
std::string dpt
 The decimal point (default '.')
 

Detailed Description

This class formats floating point strings into something useful for HTML or Latex documents. For basic use, simply call either html_mode() or latex_mode() and then use convert().

The base-10 logarithm of the smallest and largest numbers to be represented without a string akin to "times 10 to the nth power" can be specified in set_exp_limits(). The number of significant figures can be specified with set_sig_figs() (the default is 5).

To force convert() to adds zeros to the right side of the mantissa to guarantee that the requested number of significant digits is given, call set_pad_zeros() with a true argument.

To force scientific notation for all numbers, set the maximum exponent to be smaller than the minimum exponent.

Note
This function does not warn the user if the number of significant figures requested is larger than the machine precision.
If the absolute magnitude for either the minimum or maximum exponent is larger than or equal to the number of significant figures, then rounding will automatically occur.

The format for a normal number is

prefix (sign-string) number suffix

and in scientific notation is

sci-prefix (sci-sign-string) number times-string 
exp-prefix (exp-sign-string) exponent exp-suffix sci-suffix

Examples

The code

format_float fd;
fd.latex_mode();
cout << fd.convert(-sqrt(2.0)*1.0e-5) << endl;
cout << fd.convert(sqrt(2.0)*1.0e-2) << endl;
cout << fd.convert(-sqrt(2.0)*1.0e-1) << endl;

outputs

$-$1.4142 $\\times 10^{-5}$
0.014142
$-$0.14142

and the code

format_float fd;
fd.html_mode();
fd.set_sig_figs(7);
fd.set_pad_zeros(true);
cout << fd.convert(1.414e-5) << endl;
cout << fd.convert(1.414e-2) << endl;

outputs

1.414000 &times; 10<sup>-5</sup>
0.01414000
Idea for Future:

Handle inf's and nan's correctly.

Allow change of string for the "+" sign for the exponent

Definition at line 113 of file format_float.h.

Member Function Documentation

◆ c_mode()

void o2scl::format_float::c_mode ( )

This reproduces the default settings of cout in automatic mode. Obviously it is faster to use iostreams than to format numbers with this class. Nevertheless, this mode is very useful for testing to ensure that this class processes the numbers correctly at the requested precision.

This function is equivalent to the settings:

◆ html_mode()

void o2scl::format_float::html_mode ( )

This function is equivalent to the settings:

◆ latex_mode()

void o2scl::format_float::latex_mode ( )

This function is equivalent to the settings:

Note
This setting assumes that the user is not in LaTeX's "math mode" already.

The documentation for this class was generated from the following file:

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