-
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
cJSON: Fix print_number to print significant digits of doubles #153
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it still makes sense to print integers without engineering notation, as long as they can be printed with full precision.
So instead of removing this integer handling completely, 1e60 should be replaced with 2^53.
I'm just not sure if this integer detection with
DBL_EPSILON
is correct, this might have to be fixed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update:
modf
can be used to test if it is an integer.If you don't want to tackle this, I can do this myself after the PR is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since -0 is allowed, there is no need to change it to 0.
As for printing integers without engineering notation as long as they can be printed with full precision, the code as in the pull request mostly handles this. 2^53 is 9,007,199,254,740,992, a little less than 10^17 (17 decimal places). The %1.17g format already prints 17 decimal places.
9007199254740991, 9007199254740992, and 9007199254740994 all print as 16-digit integers as desired. (9007199254740993 is not representable exactly as a double.)
The problem is with the test-print to 15 digits (%1.15g). It causes integers ending in 0 that require more than 15 decimal digits to print in exponential notation.
100,000,000,000,000 prints as desired, with 15 decimal digits: 100000000000000
(I manually added comma separators to help myself count.)
1,000,000,000,000,000 prints as 1e+15, but we want 1000000000000000
10,000,000,000,000,000 prints as 1e+16, but we want 10000000000000000
100,000,000,000,000,000 prints as 1e+17, but that is OK according to your proposed rule because it is greater than 2^53.
The solution is to qualify the test-print. I will have to think more carefully about the value of LIMIT_FOR_TEST_PRINTING - is it 1e+15? I'll have more time to think tomorrow night.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it works that way because a lot of doubles are bigger than
LIMIT_FOR_TEST_PRINTING
but should still be recoverable with just 15 decimal places of precision.Anyway, if the printing of Integers in engineering notation is only a special case for numbers bigger than 10^15 and smaller than -10^15 (or somewhere around that) I can live with it. I though this happened to more numbers than just that.
In this case let's go for simplicity and keep the code as it is. Just remove the special case for negative zero and it can be merged.