问题
I am reading from a JSON file using jsoncpp. When I write back to the file, my float values are slightly off. For the sake of testing, I decided to parse the file to a Json::Value and then write that value back to the file. I would expect it to look the same, but instead the float values are different.
Example:
"Parameters":
{
"MinXValue": 0.1,
"MaxXValue": 0.15,
"MinYValue": 0.25,
"MaxYValue": 1.1,
"MinObjectSizeValue": 1
}
writes as:
"Parameters":
{
"MinXValue": 0.10000000000000001,
"MaxXValue": 0.14999999999999999,
"MinYValue": 0.25,
"MaxYValue": 1.1000000238418579,
"MinObjectSizeValue": 1
}
You may notice that 0.25 did not change, even though all of the other floats did. Any idea what's going on here?
回答1:
It is actually an issue of floating point number parsing/printing implementation. Although floating point numbers can only represent some decimal numbers exactly (0.25 is one of ~2^64), it is necessary to parse a string representation to the nearest binary representation. When printing floating point, it is also necessary to print the (preferably the shortest) string representation which can be restored to the binary representation.
I admit that I had not investigate JsonCPP to see if there is a solution for this. But as I am the author of RapidJSON, I tried to see how RapidJSON performs for this:
const char json[] =
"{"
"\"MinXValue\": 0.1,"
"\"MaxXValue\": 0.15,"
"\"MinYValue\": 0.25,"
"\"MaxYValue\": 1.1,"
"\"MinObjectSizeValue\": 1"
"}";
using namespace rapidjson;
Document d;
d.Parse(json);
StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb);
d.Accept(writer);
std::cout << sb.GetString();
And the result:
{
"MinXValue": 0.1,
"MaxXValue": 0.15,
"MinYValue": 0.25,
"MaxYValue": 1.1,
"MinObjectSizeValue": 1
}
RapidJSON implemented both parsing and printing algorithms internally. Normal precision parsing will have maximum 3 ULP errors, but with full precision parsing flag (kParseFullPrecisionFlag
) it can always parse to nearest representation. The printing part implemented Grisu2 algorithm. It does always generate an exact result, and more than 99% of time to be shortest (optimal).
Actually, using strtod()
and sprintf(..., "%.17g", ...)
can solve this problem too. But they are much slower in current C/C++ standard library. For example, I have done a benchmark for printing double
. So in RapidJSON we implemented its own optimized header-only solutions.
回答2:
One solution is to a make small change to the jsoncpp source file.
Replace the 17 with a 15 on the following line such that it reads (line 4135 in my copy):
std::string valueToString(double value) { return valueToString(value, false, 15); }
Basically it's reducing the max number of printed digits from 17 to 15, but if you're ok with that it seems to fix all the undesirable printing artefacts you mention. I think one could also argue you shouldn't be using json's to pass around >15 significant digits anyways (near the limit of double precision), but that's another story...
E.g. what used to print for me as:
"yo" : 1726.6969999999999,
now prints as:
"yo" : 1726.697,
来源:https://stackoverflow.com/questions/29832984/jsoncpp-writing-float-values-incorrectly