Numerical conversion

The C ++ 11 header has three new functions for converting between a number and a string.

std::string std::to_string(unsigned long long); std::string std::to_string(long double); std::string std::to_string(long long); 

The first question is why are there only 3 functions? What about simple int or unsigned int etc.?

Second question: why doesn't to_string throw an exception in the following code?

 long double x = std::numeric_limits<long double>::quiet_NaN(); std::string i = std::to_string( x ); long double c = std::stold( i ); // i = "1.#QNAN" 

And the third question - why is c equal to 1.0?

+6
source share
2 answers
  • "As long as this gives the described behavior, do what you like."

    All internal numeric types can be implicitly converted to unsigned long long , long double or long long and still hold the required precision, so you no longer need to overload.

    The standard says that the following functions should be defined, although the lib that confirms the standard can freely do "whatever it wants" if it gives the same behavior as described.


  • Why should this be ruled out?

    std::numeric_limits<long double>::quiet_NaN(); is a valid value, and std::to_string (T) described in the standard to get the same behavior as calling sprintf with the appropriate format string.

Β§ 21.5 / 6 Numerical conversions

  • string to_string(int val);
  • string to_string(unsigned val);
  • string to_string(long val);
  • string to_string(unsigned long val);
  • string to_string(long long val);
  • string to_string(unsigned long long val);
  • string to_string(float val);
  • string to_string(double val);
  • string to_string(long double val);

    ..

Return:

  • Each function returns a string object containing a character representing the value of its argument, which will be generated by calling sprintf (buf, fmt, val) with the format specifier "% d", "% u", "% ld", "% lu", "% lld ","% llu ","% f ","% f "or"% Lf ", respectively, where buf denotes an internal character buffer of sufficient size.

  • In which compiler is c equal to 1.0 ?

    The conversion must yield a NaN if i is a string representation of NaN (containing no digits).

    If no suitable conversion is found, the function is described to throw invalid_argument .

    MSVC will give 1.#QNAN when trying to convert std::numeric_limits<long double>::quiet_NaN(); in std::string .

    When using std::stold it will look for the first space character, and then use as many digits as it found (in this case, only 1 ), so c will be 1.0 after the function call.

+5
source

I find the whole package in my copy of the standard:

 string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float val); string to_string(double val); string to_string(long double val); 

Perhaps your compiler has not yet completed all of them?

Functionality is described as

Returns: each function returns a string object containing a symbolic representation of the value of its argument, which is generated by calling sprintf(buf, fmt, val) with the format specifier "%d" , "%u" , "%ld" , "%lu" , "%lld" , "%llu" , "%f" , "%f" or "%Lf" respectively, where buf is an internal character buffer of sufficient size.

Since this is supposed to be a wrapper around sprintf , it was probably decided not to throw any exceptions, since sprintf does not work.

+1
source

Source: https://habr.com/ru/post/911298/


All Articles