News for hackers. Stuff that matters.
Re:Curious lack of sprintf scaling by MaulingMonkey on Friday February 25, 2022 @06:17PM (#30473554)
> why is locale stuff even in sprintf? What things get localized

Decimal places in floating point numbers is the primary standard thing (some locales use `.` as a decimal seperator and `,` as a thousands seperator... some locales swap interpretations!) Nonstandard extensions may also do things like accept unicode strings, wide unicode strings, etc. which may need to be re-encoded to whatever the ambient locale-specified narrow encoding is (UTF16 => windows-1251?).

In the modern era of sprintf being exiled to some low level internal thing, I agree it makes little sense - causing more bugs than it fixes - but in the days of using it for a lot of heavy lifting of user-facing data, it was an understandable target for this kind of treatment.

Re:Curious lack of sprintf scaling by jcelerier on Saturday February 26, 2022 @02:16AM (#30475773)
Yeah, as a french I often have to set my LANG to C because of how many buggy apps that use sprintf without fixing a locale exist (making save files at best unshareable with the world at worse unreadable again)
Re:Curious lack of sprintf scaling by butlerm on Saturday February 26, 2022 @12:22AM (#30475385)
The overhead is completely unnecessary in far too many cases. So what they should have done is kept sprintf to its traditional behavior and invented a new, slower alternative for those fairly unusual cases that need locale sensitivity. Locales tend to be all or nothing and extremely inefficient to make general use of anyway because too many things are controlled by the same setting.

Standards committees should not carelessly sit down and devise things that destroy performance by a factor of ten or more, especially when it is entirely unnecessary and where they do not keep or provide simple, fast alternatives for the most common cases where the format is known and has nothing to do with a preferred locale. What they did instead was a massive energy and time wasting imposition of unhelpful semantics on functions that did not have or need any such thing instead of a tailored let's only do this in special cases with special functions approach.

Re:Curious lack of sprintf scaling by jcelerier on Saturday February 26, 2022 @02:19AM (#30475790)
C++ has thankfully locale- independent functions now, std::from_chars, std::to_chars.. but it took way too many years
Re:Curious lack of sprintf scaling by gustavorg on Saturday February 26, 2022 @06:35AM (#30476784)
I am very curious to know why you guys are forgetting std::to_string. It is available since c++11. This and then str.c_ctr() should make the trick in most cases.
Re:Curious lack of sprintf scaling by jcelerier on Saturday February 26, 2022 @08:58AM (#30477601)
to_string is just a wrapper around sprintf... here's the (de-__uglyfied) code on libstdc++:

    inline string
    to_string(float val)
    {
      const int n = gnu_cxx::numeric_traits<float>::max_exponent10 + 20;
      return gnu_cxx::to_xstring<string>(&std::vsnprintf, n, "%f", val);
    }
it will be worse in pretty much every metric than fmt::format("{}", val);
Re:Curious lack of sprintf scaling by formerly_proven on Saturday February 26, 2022 @07:54AM (#30477158)
to_string uses the locale.
Re:Curious lack of sprintf scaling by MaulingMonkey on Saturday February 26, 2022 @04:53PM (#30482334)
sprintf/setlocale date back to at least C89, and probably even earlier - over three decades ago, this wasn't the performance and multithreading hazard it is today, because people simply weren't multithreading. Global state was the practice de jure - even reasonably so, compared to drowning everything in a sea of redundant parameterization on monochrome 80x25 terminals, in an era of smaller programs, when global state was easier to reason about.

The C89 standards committee wasn't "careless" here - it was a product of the times. Of course, when modern APIs and standards committees duplicate sprintf's issues, it's far more fustrating - even when justified in the light of "backwards compatability".