Skip to content

Fix exact double recovery when printing numbers - #1099

Open
sergioperezcheco wants to merge 1 commit into
DaveGamble:masterfrom
sergioperezcheco:fix/exact-double-print-recovery
Open

sergioperezcheco wants to merge 1 commit into
DaveGamble:masterfrom
sergioperezcheco:fix/exact-double-print-recovery

Conversation

@sergioperezcheco

Copy link
Copy Markdown

The recovery check in print_number accepts the 15-digit representation when it is only approximately equal to the original double. This can lose significant digits and even print DBL_MAX as a value that parses back as infinity.

Use ordered comparisons to require exact numeric recovery before keeping the shorter representation, otherwise retain the existing 17-digit fallback. This avoids introducing another -Wfloat-equal comparison and leaves the public approximate cJSON_Compare behavior unchanged. Regression tests cover the maximum finite values, numbers requiring additional significant digits, and public print/parse round trips.

Fixes #838. Verified locally with the C89 strict build and all 22 CTest targets, plus an independent public API round-trip reproduction that fails on the original code and passes with this change. Windows/Linux and other locale/rounding-mode matrices were not run locally. Prepared with AI assistance and independently checked against the issue, source, and regression results.

Assisted-by: Hermes Agent
Signed-off-by: sergioperezcheco <checo520@outlook.com>
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.

Regression on double precision in print_number() when fix a clang compile warning

1 participant