Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-defined double output precision #19

Merged
merged 3 commits into from
Jun 26, 2014

Conversation

pah
Copy link
Contributor

@pah pah commented Jun 25, 2014

Writing a double to an OutputStream current prints at most 6 significant
digits (according to the C standard).

The functions Set/GetDoublePrecision(), added to the Writer classes
can be used to fluently set the precision, i.e. the number of significant
digits to use for writing the double:

  Writer<...> writer(...);
  d.Accept(writer.SetDoublePrecision(12)); // print up to 12 significant digits

Additionally, a new Double(double,int) overload is added to the Writer classes,
allowing to programmatically write a single double value with a custom output precision:

writer.StartArray()
    .Double(1.2345) // == Double(..., writer.GetDoublePrecision())
    .Double(1.2345678,9) // explicit precision
    .EndArray();

An additional unit test Writer.DoublePrecision is added as well.
See upstream issues

(resubmitted #15 against master)

pah added 3 commits June 25, 2014 18:09
Writing a double to an OutputStream current prints at most 6 significant
digits (according to the C standard).

The function SetDoublePrecision(), added to the Writer classes
can be used to fluently set the precision, i.e. the number of
significant digits to use for writing the double:

  Writer<...> writer(...);
  d.Accept(writer.SetDoublePrecision(12));
As proposed in other patches, it is convenient to pass a user-defined
precision for the (programmatic) output of a single double value
to an OutputStream.

This patch adds an additional overload with an explicit precision
argument to the (Pretty)Writer class templates.
miloyip added a commit that referenced this pull request Jun 26, 2014
User-defined double output precision
@miloyip miloyip merged commit d5add05 into Tencent:master Jun 26, 2014
@pah pah deleted the feature/double-precision branch June 26, 2014 07:43
@miloyip miloyip mentioned this pull request Aug 9, 2014
@drewnoakes
Copy link
Contributor

I cannot find the code form c9c2d06 in current master. Writer<>::Double does not appear to be overloaded, nor is there a SetDoublePrecision function. Am I missing something?

@pah
Copy link
Contributor Author

pah commented Oct 7, 2014

This feature has been superseded by the new dtoa implementation in #97, which always provides the shortest, exact output. Therefore, the need for a "limited precision" has been considered obsolete.

Still, in this comment, this question has popped up before.

@drewnoakes
Copy link
Contributor

@pah, thanks for the update. Shame, as I would really like to have your commit in the lib. I know for a fact that my my code should never be sending more than 3dp, but I'm seeing cumulative rounding errors. Is this truly obsolete? For example, starting with the number 1.23 then repeatedly adding 0.01 to it will eventually result in numbers with long sequences of recurring digits. I'm happy to have this imprecision in my application's state so long as it disappears at the edges -- i.e. where I serialise data to JSON or otherwise.

What would you recommend as a viable alternative using the current master?

@pah
Copy link
Contributor Author

pah commented Oct 9, 2014

Yes, some 3-decimal digit numbers cannot be represented exactly as double values, consequently the output will have trailing decimal digits. So for now, you may be out of luck with the current master, if you need to limit the output precision. Unfortunately, there have been quite some fixes in the current master since the merge of #97. see here. So it may not be acceptable to stick with 34dd0fd (last commit before the merge).

I sketched a solution to readd something like this in this comment. The main challenge is to properly define the output semantics for the different cases in Prettify).

I would suggest that you open a new issue and include a proposal how to handle the different cases based on a given "precision limit" from the user. Of course, you're welcome to submit a pull-request as well. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants