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

Classes

struct  Precision
 
struct  ToStringOptions
 

Enumerations

enum class  TrimTrailingZerosType
 
enum class  PredefinedLocale { }
 
enum class  FloatFormatType
 

Functions

template<typename 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:

using PrecisionType = uint16_t;

Enumeration Type Documentation

◆ TrimTrailingZerosType

Control needless trailing zeros. For example, 3.000 instead of 3, or 4.2000 versus 4.2. Sometimes desirable (to show precision). But often not.

Definition at line 40 of file FloatConversion.h.

◆ PredefinedLocale

Enumerator
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 49 of file FloatConversion.h.

◆ FloatFormatType

Automatic picks based on the precision and the number used, so for example, 0.0000001 will show as '1e-7', but 4 will show as '4'

 eScientific corresponds to ios_base::scientific
 eFixedPoint corresponds to ios_base::fixed
 eDefaultFloat corresponds to unsetf (floatfield) - which may be different than scientific or fixed point

Definition at line 153 of file FloatConversion.h.

Function Documentation

◆ ToString()

template<typename 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

◆ 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 645 of file FloatConversion.inl.