Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Stroika::Foundation::Characters::FloatConversion Namespace Reference

Classes

struct  SignificantFigures
 
struct  ToStringOptions
 

Enumerations

enum class  TrimTrailingZerosType
 
enum class  PredefinedLocale { eUseCLocale , eUseCurrentLocale }
 
enum class  FloatFormatType {
  eDefaultFloat , eFixedPointWithoutWhitespaceTrimmed , eFixedPoint = eFixedPointWithoutWhitespaceTrimmed , eFixedPointWithWhitespaceTrimmed ,
  eScientificWithoutWhitespaceTrimmed , eScientific = eScientificWithoutWhitespaceTrimmed , eScientificWithWhitespaceTrimmed , eStandard ,
  eAutomaticScientific , eDEFAULT = eDefaultFloat , Stroika_Define_Enum_Bounds =(eDefaultFloat, eAutomaticScientific)
}
 

Functions

template<Common::IAnyOf< String, string, wstring > STRING_TYPE = String, floating_point FLOAT_TYPE = float>
STRING_TYPE ToString (FLOAT_TYPE f, const ToStringOptions &options={})
 
template<floating_point T = double, IUNICODECanUnambiguouslyConvertFrom CHAR_T>
ToFloat (span< const CHAR_T > s)
 

Detailed Description

TODO:

Enumeration Type Documentation

◆ TrimTrailingZerosType

Control needless trailing zeros. For example, 3.000 instead of 3, or 4.2000 versus 4.2.

Sometimes eDontTrimZeros desirable (to show precision): but often not.

Definition at line 33 of file FloatConversion.h.

◆ PredefinedLocale

Enumerator
eUseCLocale 

Use the hardwired 'C' locale (not exactly a real locale in the sense of localization. But a good choice for the 'locale' to use for non-localized code (e.g. much serverside code).

eUseCurrentLocale 
Note
- this selects the current locale at the time the preference is used, whereas in Stroika v2.1, it used the current locale at the time the preference object was created.

Definition at line 48 of file FloatConversion.h.

◆ FloatFormatType

Enumerator
eDefaultFloat 

corresponds to unsetf (floatfield) - which may be different than scientific or fixed point. This is basically what the 'C' standard decided would be the default way to format floating point numbers.

Precision Field: The stream's precision setting (managed by std::setprecision or std::ios_base::precision()) specifies the maximum number of meaningful digits to display in total (both before and after the decimal point), not a fixed number of digits after the decimal point.

Trailing Zeros: It does not pad the output with trailing zeros if the number can be displayed with fewer digits than the precision.

Notation: It automatically switches between fixed-point and scientific notation as needed to best represent the value within the given precision. For example, a large number or a number very close to zero might be shown in scientific notation, while others will be in fixed-point notation.

For example (first 3 from https://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0 0.01 │ 0.01 0.00001 │ 1e-05 (probably could be 0.00001) 3.12e12 │ 3.12e+12 3.12 │ 3.12 212312345.0 │ 2.12312e+08 -44.2 │ -44.2 0.0000001234567 │ 1.23457e-07

eFixedPointWithoutWhitespaceTrimmed 

corresponds to ios_base::fixed (numbers are displayed without an exponent part, not actually fixed with display)

For example (first 3 from https://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0.000000 0.01 │ 0.010000 0.00001 │ 0.000010 3.12e12 │ 3120000000000 3.12 │ 3.12 212312345.0 │ 212312345 -44.2 │ -44.2 0.0000001234567 │ 0.000000

eFixedPoint 

eFixedPointWithoutWhitespaceTrimmed

eFixedPointWithWhitespaceTrimmed 

For example (first 3 from https://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0 0.01 │ 0.01 0.00001 │ 0.00001 3.12e12 │ 3120000000000 3.12 │ 3.12 212312345.0 │ 212312345 -44.2 │ -44.2 0.0000001234567 │ 0

eScientificWithoutWhitespaceTrimmed 

corresponds to ios_base::scientific

For example (first 3 from https ://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0.000000e+00 0.01 │ 1.000000e-02 0.00001 │ 1.000000e-05 3.12e12 │ 3.12000e+12 3.12 │ 3.12000e+00 212312345.0 │ 2.12312e+08 -44.2 │ -4.42000e+01 0.0000001234567 │ 1.23457e-07

eScientific 

eScientificWithoutWhitespaceTrimmed

eScientificWithWhitespaceTrimmed 

For example (first 3 from https ://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0.0e+00 0.01 │ 1.0e-02 0.00001 │ 1.0e-05 3.12e12 │ 3.12e+12 3.12 │ 3.12e+00 212312345.0 │ 2.12312e+08 -44.2 │ -4.42e+01 0.0000001234567 │ 1.23457e-07

eStandard 

Somewhat like defaultfloat, but never uses scientific notation.

not scientific (no e+nn), but otherwise somewhat like fixed point or defaultfloat.

Really no good name for this (unscientific, defaultfloat means something differnt in C++). Sort of like defaultfloat but NEVER deciding to use scientific notation.

For example (first 3 from https://en.cppreference.com/w/cpp/io/manip/fixed.html) number │ output ----------------—│-------------------— 0.0 │ 0 0.01 │ 0.01 0.00001 │ 0.00001 3.12e12 │ 3120000000000 3.12 │ 3.12 212312345.0 │ 212312345 -44.2 │ -44.2 0.0000001234567 │ 0.000000123457

eDEFAULT 

If its good enuf for C, its good enuf for me ;-). Seriously - sensible default.

Definition at line 209 of file FloatConversion.h.

Function Documentation

◆ ToString()

template<Common::IAnyOf< String, string, wstring > STRING_TYPE = String, floating_point FLOAT_TYPE = float>
STRING_TYPE Stroika::Foundation::Characters::FloatConversion::ToString ( FLOAT_TYPE  f,
const ToStringOptions options = {} 
)

ToString converts a floating point number to a string, controlled by parameterized options.

See also
ToStringOptions

ToString () maps NAN values to the string "NAN", and negative infinite values to "-INF", and positive infinite values to "INF" (note NAN/INF are case insensitive).

See also
http://en.cppreference.com/w/cpp/string/byte/strtof

The supported type values for FLOAT_TYPE are: o float o double o long double

The supported type values for RESULT_TYPE are: o String o string o wstring o ... but this could sensibly be extended in the future

Definition at line 816 of file FloatConversion.inl.

◆ ToFloat()

template<floating_point T = double, IUNICODECanUnambiguouslyConvertFrom CHAR_T>
T Stroika::Foundation::Characters::FloatConversion::ToFloat ( span< const CHAR_T >  s)

ToFloat all overloads: Convert the given decimal-format floating point string to an float, double, or long double.

ToFloat will return nan () if no valid parse (for example, -1.#INF000000000000 is, invalid and returns nan, despite the fact that this is often emitted by the MSFT sprintf() for inf values).

The overloads taking string or const char* arguments Require() that the input is ASCII ('C' locale required/assumed). (

If the argument value is too large or too small to fit in 'T' (ERANGE) - then the value will be pinned to -numeric_limits<T>::infinity () or numeric_limits<T>::infinity ().

If the input string is INF or INFINITY (with an optional +/- prefix) - the returned value will be the appropriate version of infinity.

If the argument is the string "NAN", a quiet NAN will be returned. If the string -INF or -INFINITY, a negative infinite float will be returned, and if INF or INFINITY is passed, a positive infinite value will be returned:

See also
http://en.cppreference.com/w/cpp/string/byte/strtof

ToFloat (no remainder parameter): The argument should be pre-trimmed (whitespace). If there is any leading or trailing garbage (even whitespace) this function will return nan() (note - unlike overload with 'remainder' arg).

ToFloat (with remainder parameter): Logically a simple wrapper on std::wcstof, std::wcstod, std::wcstold - except using String class, and returns the unused portion of the string in the REQUIRED remainder OUT parameter.

This means it ALLOWS leading whitespace (skipped). And it allows junk at the end (remainder parameter filled in with what).

Note
SEE http://stroika-bugs.sophists.com/browse/STK-748 We will PROBABLY change this API to take a ToFloatOptions parameter to handle proper locale/conversions of strings to numbers but so far I've not been able to get any of that working, so ignore for now...
Precondition
start <= end; for overloads with start/end, and must point to valid string in that range
remainder != nullptr
// @todo redo all these with some concept to make it shorter - like ISCOVNERTIBLE TO STRING
Note
when called with CHAR_T=char, we REQUIRE the argument string is ALL ASCII

Definition at line 839 of file FloatConversion.inl.