cpptoml icon indicating copy to clipboard operation
cpptoml copied to clipboard

ability to specify double precision when writing out

Open nephatrine opened this issue 6 years ago • 1 comments

Would love to have the ability to specify the precision/digits to use when writing out floats. Currently cpptoml is writing out numbers like 0.29799999999999999 and 0.60999999999999999 which contain way too much precision for the source #s (0.298 and 0.610) and simply being able to tell cpptoml to round all doubles to X digits would not only fix that but make these much friendlier and more accurate numbers for human readers/editors of the config.

nephatrine avatar Feb 07 '19 20:02 nephatrine

Inspired by this we've done the following in my visitor function, to output double values in a better way:

  void visit(const cpptoml::value<double>& v) {
    // Don't use default precision of stringstream
    std::stringstream ss;
    ss << std::showpoint << std::setprecision(std::numeric_limits<double>::max_digits10 - 1) << v.get();
    m_stream << ss.str();
  }
...
...
  std::ostringstream m_stream;

pkbehera avatar Jan 18 '22 12:01 pkbehera