1
0
mirror of https://github.com/DaveGamble/cJSON synced 2024-12-23 15:12:04 +00:00

cJSON: Fix print_number to print significant digits of doubles

This commit is contained in:
Debora Grosse 2017-04-20 10:55:09 -04:00
parent 6ccfd5d603
commit 3fc9bc0648
2 changed files with 21 additions and 59 deletions

56
cJSON.c
View File

@ -410,27 +410,6 @@ static void update_offset(printbuffer * const buffer)
buffer->offset += strlen((const char*)buffer_pointer); 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. */ /* 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) 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; double d = item->valuedouble;
int length = 0; int length = 0;
size_t i = 0; size_t i = 0;
cJSON_bool trim_zeroes = true; /* should zeroes at the end be removed? */ unsigned char number_buffer[26]; /* temporary buffer to print the number into */
unsigned char number_buffer[64]; /* temporary buffer to print the number into */
unsigned char decimal_point = get_decimal_point(); unsigned char decimal_point = get_decimal_point();
double test;
if (output_buffer == NULL) 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"); 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, "0");
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 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 */ /* sprintf failed or buffer overrun occured */
@ -474,15 +455,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
return false; 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 */ /* reserve appropriate space in the output */
output_pointer = ensure(output_buffer, (size_t)length, hooks); output_pointer = ensure(output_buffer, (size_t)length, hooks);
if (output_pointer == NULL) if (output_pointer == NULL)

View File

@ -63,19 +63,19 @@ static void print_number_should_print_positive_integers(void)
static void print_number_should_print_positive_reals(void) static void print_number_should_print_positive_reals(void)
{ {
assert_print_number("0.123", 0.123); 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("1000000000000", 10e11);
assert_print_number("1.230000e+129", 123e+127); assert_print_number("1.23e+129", 123e+127);
assert_print_number("0", 123e-128); /* TODO: Maybe this shouldn't be 0 */ assert_print_number("1.23e-126", 123e-128);
} }
static void print_number_should_print_negative_reals(void) static void print_number_should_print_negative_reals(void)
{ {
assert_print_number("-0.0123", -0.0123); assert_print_number("-0.0123", -0.0123);
assert_print_number("-1.000000e-09", -10e-10); assert_print_number("-1e-09", -10e-10);
assert_print_number("-1000000000000000000000", -10e20); assert_print_number("-1e+21", -10e20);
assert_print_number("-1.230000e+129", -123e+127); assert_print_number("-1.23e+129", -123e+127);
assert_print_number("-1.230000e-126", -123e-128); assert_print_number("-1.23e-126", -123e-128);
} }
static void print_number_should_print_non_number(void) 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); */ /* 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) int main(void)
{ {
/* initialize cJSON item */ /* initialize cJSON item */
@ -107,7 +98,6 @@ int main(void)
RUN_TEST(print_number_should_print_positive_reals); RUN_TEST(print_number_should_print_positive_reals);
RUN_TEST(print_number_should_print_negative_reals); RUN_TEST(print_number_should_print_negative_reals);
RUN_TEST(print_number_should_print_non_number); RUN_TEST(print_number_should_print_non_number);
RUN_TEST(trim_trailing_zeroes_should_trim_trailing_zeroes);
return UNITY_END(); return UNITY_END();
} }