Skip to content

Enabling Grisu conversion generates different output #1033

@DanielaE

Description

@DanielaE

Commit 355eb6d switched the Grisu conversion algorithm active for shortest roundtrip (default) formatting. Alas, I'm a bit puzzled about the outcome compared to the snprintf-based conversion: it delivers different outcomes.

Consider this test code:

#include <fmt/core.h>
#include <string>
#include <charconv>
#include <array>

template <typename T>
std::string toChars(T Value) {
  std::array<char, 32> Buffer;
  auto b = &Buffer.front();
  auto e = &Buffer.back() + 1;
  return {b, std::to_chars(b, e, Value).ptr};
}

int main() {
  const double doublePi = 3.14159265358979;
  const float floatPi = doublePi;

  auto GrisuF = fmt::format("{}", floatPi);
  auto GrisuD = fmt::format("{}", doublePi);
  auto ToChrF = toChars(floatPi);
  auto ToChrD = toChars(doublePi);
}

My observed outcome:

              Debugger       snprintf      Grisu           std::to_chars
doublePi 3.1415926535897900  3.14159  3.14159265358979    3.14159265358979
floatPi	 3.14159274          3.14159  3.1415927410125732  3.1415927

Is this as designed?

grisu-test-float

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