Skip to content

to_chars general format doesn't produce shortest output in some cases #235

Description

@ergpudb

Using to_chars with general format seems to not produce the shortest output in some simple cases.

For example, comparing boost charconv, std charconv (gcc 14), and fmt:

#include <iostream>
#include <charconv>
#include <boost/charconv.hpp>
#include <fmt/format.h>

int main()
{
    double v = 0.1;

    char buffer1[64] {};
    auto r1 = boost::charconv::to_chars(buffer1, buffer1 + sizeof(buffer1), v, boost::charconv::chars_format::general);
    std::cout << buffer1 << "\n";

    char buffer2[64] {};
    auto r2 = std::to_chars(buffer2, buffer2 + sizeof(buffer2), v, std::chars_format::general);
    std::cout << buffer2 << "\n";

    char buffer3[64] {};    
    fmt::format_to(buffer3, "{}", v);
    std::cout << buffer3 << "\n";
}

Output:

1e-01
0.1
0.1

boost is selecting scientific instead of fixed here, even though fixed is shorter.

It seems like it may be an issue with values < 1, since 0.1234 and 0.12345678 also select the longer scientific format, but 1.1, 1.1234, and 1.12345678 select fixed as expected.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions