Class format_float (o2scl)

O2scl : Class List

class format_float

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

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.

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

Note

This function does not warn the user if the number of significant figures requested is larger than the machine precision.

Note

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.

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 '.')

bool alt_exp_digits
std::vector<std::string> exp_digits
int remove_zeros_dpt(std::string &s)

Remove extra zeros and decimal point from mantisaa.

format_float()

Basic usage

void html_mode()

Set HTML mode.

This function is equivalent to the settings:

set_prefix("");
set_sign("-");
set_suffix("");
set_sci_prefix("");
set_times(" &times; ");
set_exp_prefix("10<sup>");
set_exp_sign("-");
set_sci_sign("-");
set_exp_suffix("</sup>");
set_sci_suffix("");
set_not_finite("Nan");
set_zero("0");
set_exp_digits(0);
set_show_exp_sign(false);

void latex_mode()

Set Latex mode.

This function is equivalent to the settings:

set_prefix("");
set_sign("$-$");
set_suffix("");
set_sci_prefix("");
set_times(" $\\times ");
set_exp_prefix("10^{");
set_exp_sign("-");
set_sci_sign("$-$");
set_exp_suffix("}");
set_sci_suffix("");
set_not_finite("Nan");
set_zero("0");
set_exp_digits(0);
set_show_exp_sign(false);

Note

This setting assumes that the user is not in LaTeX’s “math mode” already.

void unicode_mode()
void c_mode()

C-like 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:

set_prefix("");
set_sign("-");
set_suffix("");
set_sci_prefix("");
set_times("e");
set_exp_prefix("");
set_exp_sign("-");
set_sci_sign("-");
set_exp_suffix("");
set_sci_suffix("");
set_not_finite("NaN");
set_zero("0");
set_exp_limits(-4,5);
set_exp_digits(2);
set_show_exp_sign(true);

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()

inline void set_prefix(std::string prefix)

set prefix

inline void set_suffix(std::string suffix)

Set suffix.

inline void set_sci_prefix(std::string sci_prefix)

Set prefix for scientific notation.

inline void set_sci_suffix(std::string sci_suffix)

Set suffix for scientific notation.

inline void set_exp_prefix(std::string exp_prefix)

Set prefix for exponent.

inline void set_exp_suffix(std::string exp_suffix)

Set suffix for exponent.

inline void set_sign(std::string sign)

Set sign.

inline void set_exp_sign(std::string exp_sign)

Set sign for exponent.

inline void set_show_exp_sign(bool b)

Set policy for showing positive exponent sign.

inline void set_sci_sign(std::string sci_sign)

Set sign for scientific notation.

inline void set_times(std::string times)

Set times.

inline void set_zero(std::string zero)

Set zero.

inline 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()

inline void set_exp_limits(int min, int max)

Set the exponent limits.

inline void set_sig_figs(size_t sig_figs)

Set the number of significant figures (argument has maximum of 15 and cannot be zero)

inline void set_pad_zeros(bool pad)

Set pad zeros.

inline void set_dec_point(std::string dec_point)

Set decimal point.

inline 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()

inline std::string get_prefix()

Get prefix.

inline std::string get_suffix()

Get suffix.

inline std::string get_sci_prefix()

Get prefix for scientific notation.

inline std::string get_sci_suffix()

Get suffix for scientific notation.

inline std::string get_exp_prefix()

Get prefix for exponent.

inline std::string get_exp_suffix()

Get suffix for exponent.

inline std::string get_sign()

Get sign.

inline std::string get_exp_sign()

Get sign for exponent.

inline std::string get_sci_sign()

Get sign for scientific notation.

inline std::string get_times()

Get times.

inline std::string get_zero()

Get zero.

inline 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()

inline int get_exp_min()

Get minimum exponent.

inline int get_exp_max()

Get maximum exponent.

inline size_t get_sig_figs()

Get sig_figs.

inline bool get_pad_zeros()

Get pad_zeros.

inline std::string get_dec_point()

Get decimal point.