std::wcstof...td::wcstold(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::wcstof, std::wcstod, std::wcstold πŸ’…

okay bestie, so this is from the C++ Standard Library manual β€” std::wcstof...td::wcstold(3) β€” and no, that truncated name isn't a typo on my end, that's literally how the page introduces itself. We're doing wide-string-to-float conversions today and honestly? iconic behavior.


✨ NAME ✨

std::wcstof, std::wcstod, std::wcstold β€” literally the whole name is just std::wcstof,std::wcstod,std::wcstold. That's it. That's the name. The manpage said "let the functions speak for themselves" and I respect it 🫢


🌸 SYNOPSIS 🌸

First things first, they live in the header <cwchar> (yes, the wide one, not the plain one, don't @ me).

float       wcstof( const wchar_t* str, wchar_t** str_end );

(since C++11)

double      wcstod( const wchar_t* str, wchar_t** str_end );
long double wcstold( const wchar_t* str, wchar_t** str_end );

(since C++11)

So basically: interprets a floating point value in a wide string pointed to by str. three flavors, three widths, one vibe πŸ’«

Here's the actual tea on how it parses, step by step:

  • first it discards any whitespace characters (as determined by std::iswspace) until the first non-whitespace character is found
  • then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value

and what counts as a "valid floating-point value"? glad you asked, there are FIVE whole categories, buckle up πŸŽ€

  • decimal floating-point expression β€” it consists of these parts:
    • (optional) plus or minus sign
    • nonempty sequence of decimal digits optionally containing decimal-point character (as determined by the current C locale) (defines significand)
    • (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 10)
  • hexadecimal floating-point expression β€” it consists of these parts:
    • (optional) plus or minus sign
    • 0x or 0X
    • nonempty sequence of hexadecimal digits optionally containing a decimal-point character (as determined by the current C locale) (defines significand)
    • (optional) p or P followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 2) (since C++11)
  • infinity expression β€” it consists of these parts:
    • (optional) plus or minus sign
    • INF or INFINITY ignoring case
  • not-a-number expression β€” it consists of these parts:
    • (optional) plus or minus sign
    • NAN or NAN(char_sequence) ignoring case of the NAN part. char_sequence can only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value.
  • any other expression that may be accepted by the currently installed C locale πŸ‘€

and then the functions set the pointer pointed to by str_end to point to the wide character past the last character interpreted. If str_end is a null pointer, it is ignored. no cap, that's the whole deal πŸ’…


πŸ¦‹ PARAMETERS πŸ¦‹

  • str β€” pointer to the null-terminated wide string to be interpreted
  • str_end β€” pointer to a pointer to a wide character

two params, both doing exactly what they say on the label. we love a low-maintenance queen πŸ‘‘


⭐ RETURN VALUE ⭐

Floating point value corresponding to the contents of str on success. slay.

but 😭 if the converted value falls out of range of corresponding return type, a range error occurs and HUGE_VAL, HUGE_VALF or HUGE_VALL is returned. so like, dramatic much, but okay.

and if no conversion can be performed, 0 is returned. zero. nothing. silence. πŸ’€


🧚 EXAMPLE 🧚

// Run this code

#include <cerrno>
#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>

int main()
{
    const wchar_t* p = L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz";
    wchar_t* end;
    std::wcout << "Parsing L\"" << p << "\":\n";
    for (double f = std::wcstod(p, &end); p != end; f = std::wcstod(p, &end))
    {
        std::wcout << "  '" << std::wstring(p, end-p) << "' -> ";
        p = end;
        if (errno == ERANGE)
        {
            std::wcout << "range error, got ";
            errno = 0;
        }
        std::wcout << f << '\n';
    }

    if (std::setlocale(LC_NUMERIC, "de_DE.utf8"))
    {
        std::wcout << L"With de_DE.utf8 locale:\n";
        std::wcout << L"  '123.45' -> " << std::wcstod(L"123.45", 0) << L'\n';
        std::wcout << L"  '123,45' -> " << std::wcstod(L"123,45", 0) << L'\n';
    }
}

and the output is giving exactly this πŸ“

Parsing L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz":
  '111.11' -> 111.11
  ' -2.22' -> -2.22
  ' 0X1.BC70A3D70A3D7P+6' -> 111.11
  ' -Inf' -> -inf
  ' 1.18973e+4932' -> range error, got inf
With de_DE.utf8 locale:
  '123.45' -> 123
  '123,45' -> 123.45

that de_DE.utf8 bit at the end is the main character of the whole example btw β€” locale changes what counts as a decimal point and the output literally shifts. observe and learn πŸ’œ


πŸ‘‘ SEE ALSO πŸ‘‘

  • strtof β€” converts a byte string to a floating-point value
  • strtod β€” (function)
  • strtold β€” the long double cousin, also a function
  • C documentation for wcstof β€” the origin story, iykyk

bestie reminder: those errno == ERANGE checks are not decoration. range errors are real and the returned HUGE_VAL / HUGE_VALF / HUGE_VALL won't flag themselves for you 😭

πŸ’– that's the whole page, bestie πŸ’– brought to you by the C++ Standard Library manual, sourced from http://cppreference.com, and dated 2024.06.10 β€” go forth and parse some wide strings, you absolute icon πŸ¦‹βœ¨

πŸ“¦ where this page comes from