瀏覽代碼

Merge pull request #153 from DeboraG/print_number_prec

cJSON: Fix print_number to print significant digits of doubles
Max Bruckner 8 年之前
父節點
當前提交
87fad25c2b
共有 2 個文件被更改,包括 18 次插入61 次删除
  1. 11 44
      cJSON.c
  2. 7 17
      tests/print_number.c

+ 11 - 44
cJSON.c

@@ -415,27 +415,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)
 {
@@ -443,9 +422,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)
     {
@@ -457,20 +436,17 @@ 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))
-    {
-        /* 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 */
-    }
     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 */
@@ -479,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);
     if (output_pointer == NULL)

+ 7 - 17
tests/print_number.c

@@ -64,19 +64,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)
@@ -88,15 +88,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 */
@@ -108,7 +99,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();
 }