From 3fc9bc06480f89540e7aa4a91ca4585c48ea654b Mon Sep 17 00:00:00 2001 From: Debora Grosse Date: Thu, 20 Apr 2017 10:55:09 -0400 Subject: [PATCH] cJSON: Fix print_number to print significant digits of doubles --- cJSON.c | 56 +++++++++++--------------------------------- tests/print_number.c | 24 ++++++------------- 2 files changed, 21 insertions(+), 59 deletions(-) diff --git a/cJSON.c b/cJSON.c index a25556d..9125de4 100644 --- a/cJSON.c +++ b/cJSON.c @@ -410,27 +410,6 @@ static void update_offset(printbuffer * const buffer) buffer->offset += strlen((const char*)buffer_pointer); } -/* Removes trailing zeroes from the end of a printed number */ -static int trim_trailing_zeroes(const unsigned char * const number, int length, const unsigned char decimal_point) -{ - if ((number == NULL) || (length <= 0)) - { - return -1; - } - - while ((length > 0) && (number[length - 1] == '0')) - { - length--; - } - if ((length > 0) && (number[length - 1] == decimal_point)) - { - /* remove trailing decimal_point */ - length--; - } - - return length; -} - /* Render the number nicely from the given item into a string. */ static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) { @@ -438,9 +417,9 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out double d = item->valuedouble; int length = 0; size_t i = 0; - cJSON_bool trim_zeroes = true; /* should zeroes at the end be removed? */ - unsigned char number_buffer[64]; /* temporary buffer to print the number into */ + unsigned char number_buffer[26]; /* temporary buffer to print the number into */ unsigned char decimal_point = get_decimal_point(); + double test; if (output_buffer == NULL) { @@ -452,20 +431,22 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out { length = sprintf((char*)number_buffer, "null"); } - else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60)) + /* This checks for negative zero */ + else if (d == 0) { - /* integer */ - length = sprintf((char*)number_buffer, "%.0f", d); - trim_zeroes = false; /* don't remove zeroes for "big integers" */ - } - else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9)) - { - length = sprintf((char*)number_buffer, "%e", d); - trim_zeroes = false; /* don't remove zeroes in engineering notation */ + length = sprintf((char*)number_buffer, "0"); } else { - length = sprintf((char*)number_buffer, "%f", d); + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } } /* sprintf failed or buffer overrun occured */ @@ -474,15 +455,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out return false; } - if (trim_zeroes) - { - length = trim_trailing_zeroes(number_buffer, length, decimal_point); - if (length <= 0) - { - return false; - } - } - /* reserve appropriate space in the output */ output_pointer = ensure(output_buffer, (size_t)length, hooks); if (output_pointer == NULL) diff --git a/tests/print_number.c b/tests/print_number.c index 16f08c2..be10550 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -63,19 +63,19 @@ static void print_number_should_print_positive_integers(void) static void print_number_should_print_positive_reals(void) { assert_print_number("0.123", 0.123); - assert_print_number("1.000000e-09", 10e-10); + assert_print_number("1e-09", 10e-10); assert_print_number("1000000000000", 10e11); - assert_print_number("1.230000e+129", 123e+127); - assert_print_number("0", 123e-128); /* TODO: Maybe this shouldn't be 0 */ + assert_print_number("1.23e+129", 123e+127); + assert_print_number("1.23e-126", 123e-128); } static void print_number_should_print_negative_reals(void) { assert_print_number("-0.0123", -0.0123); - assert_print_number("-1.000000e-09", -10e-10); - assert_print_number("-1000000000000000000000", -10e20); - assert_print_number("-1.230000e+129", -123e+127); - assert_print_number("-1.230000e-126", -123e-128); + assert_print_number("-1e-09", -10e-10); + assert_print_number("-1e+21", -10e20); + assert_print_number("-1.23e+129", -123e+127); + assert_print_number("-1.23e-126", -123e-128); } static void print_number_should_print_non_number(void) @@ -87,15 +87,6 @@ static void print_number_should_print_non_number(void) /* assert_print_number("null", -INFTY); */ } -static void trim_trailing_zeroes_should_trim_trailing_zeroes(void) -{ - TEST_ASSERT_EQUAL_INT(2, trim_trailing_zeroes((const unsigned char*)"10.00", (int)(sizeof("10.00") - 1), '.')); - TEST_ASSERT_EQUAL_INT(0, trim_trailing_zeroes((const unsigned char*)".00", (int)(sizeof(".00") - 1), '.')); - TEST_ASSERT_EQUAL_INT(0, trim_trailing_zeroes((const unsigned char*)"00", (int)(sizeof("00") - 1), '.')); - TEST_ASSERT_EQUAL_INT(-1, trim_trailing_zeroes(NULL, 10, '.')); - TEST_ASSERT_EQUAL_INT(-1, trim_trailing_zeroes((const unsigned char*)"", 0, '.')); -} - int main(void) { /* initialize cJSON item */ @@ -107,7 +98,6 @@ int main(void) RUN_TEST(print_number_should_print_positive_reals); RUN_TEST(print_number_should_print_negative_reals); RUN_TEST(print_number_should_print_non_number); - RUN_TEST(trim_trailing_zeroes_should_trim_trailing_zeroes); return UNITY_END(); }