Skip to content

Possible to limit precision of number types? #284

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

Closed
richardeakin opened this issue May 29, 2015 · 8 comments
Closed

Possible to limit precision of number types? #284

richardeakin opened this issue May 29, 2015 · 8 comments

Comments

@richardeakin
Copy link

richardeakin commented May 29, 2015

I have a json-based config where I write some parameters, and when my application closes it uses the Json::StyledWriter to save the latest state of my root Json::Value object. I understand that round trip floating point precision isn't guaranteed to be exact, but is there a way to avoid numbers such as 0.15 being saved as 0.14999999999999999? This makes the config file difficult to read.

One solution I was wondering about is whether I could limit the precision of floating point numbers, I rarely in this case want more than 4 significant decimals and that would at least improve the use case.

@cdunn2001
Copy link
Contributor

First, please use StreamWriterBuilder. That allows us to add extra configuration without breaking binary-compatibility.

Note that, as described in issue #98, you will not have round-trip reproducibility if you have fewer than 17 digits of precision. Despite that caveat, we could add a setting to limit the precision. However, I don't have time right now, but if you submit a PR, we could pull it.

@bknecht
Copy link
Contributor

bknecht commented Aug 14, 2015

I saw you just closed this issue I'm interested in. What's the resolution? I just had a quick look at the code of StreamWriterBuilder and I think it's not too difficult to do. Is your request for a PR still open? I'll sit down and have a try at it if you're still interested.

@cdunn2001 cdunn2001 reopened this Aug 14, 2015
@cdunn2001
Copy link
Contributor

Great!

@bknecht bknecht mentioned this issue Oct 15, 2015
@longlene
Copy link

I think the reason of this problem is that: jsoncpp library use the double type to present the float-point number.
The double type use IEEE-754 standard, which may cause precision lose.

@cdunn2001
Copy link
Contributor

That makes sense. Thanks.

@shuhongwang-zzu
Copy link

anyone help?

@dota17
Copy link
Member

dota17 commented Jul 19, 2019

anyone help?

see the PR #381

@shuhongwang-zzu
Copy link

anyone help?

see the PR #381
Maybe the version of jsoncpp is inconsistent, mine is
JSONCPP_STRING valueToString(double value,
unsigned int precision,
PrecisionType precisionType) {
return valueToString(value, false, precision, precisionType);
}
can you help me again?

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

No branches or pull requests

6 participants