-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Precision problem of cJSON_AddNumberToObject(); with double and float. #117
Comments
I am aware of this problem and it is one of the problem areas of cJSON. There are two conflicting goals.
Currently cJSON doesn't do a good job at both of them. Printing numbers in a way that they look good for humans is hard, you have to come up with heuristics on what to do in several cases and I haven't found a good solution yet. (Ideally there would be some code that somebody else wrote, so I can get inspired). One thing I could probably do is to trim trailing zeroes. |
Consider using a byte array buffer instead of explicit data type in cJSON structure to store data could be a good approach. That is to say we only use the buffer and flag it's type information. |
@mapleez I'm not sure what you are proposing. If I understand you correctly, you want me to store numbers as a string in the cJSON struct. I don't want to do this in the parser, because it will take much more memory, but if you create the cJSON struct yourself, you can make a struct with the type |
Hmm... Could i understand a cJSON struct entity as a JSON object or an JSON array? Was i correct? If so ... cJSON {
union {
int v_int;
double v_double;
float v_float;
char* v_whatever;
}
} |
@ironengineer The problem with many zero digits at the end has been fixed in release 1.4.0. Fixing the precision in general is still a problem that has to be addressed. Closing this issue for now. |
Since version 1.5.0 cJSON doesn't loose any precision when printing anymore at least. |
Question:
How can I add the double value only with given precision as provided 100.2 instead of 100.200000. Is it possible to generate the expected result below?
(Also when I gave the same number as float number = 100.2`; generated json: {"number value":100.199997})
Example code:
Expected result:
{"number value":100.2}
Generated result:
{"number value":100.200000}
The text was updated successfully, but these errors were encountered: