Make print_number work for non-finite numbers again.

Refs #4865
This commit is contained in:
Gunnar Beutner 2014-04-20 19:21:55 +02:00
parent 6482fcec59
commit bb0b618e71
1 changed files with 13 additions and 5 deletions

View File

@ -127,12 +127,20 @@ static char *print_number(cJSON *item)
}
else
{
str=(char*)cJSON_malloc(64 + (int)log10(fabs(d))); /* This is a nice tradeoff. */
if (d != d)
{
str=(char*)cJSON_malloc(2);
if (str)
strcpy(str, "0");
}
else
{
str = (char*)cJSON_malloc(64 + (int)log10(fabs(d))); /* This is a nice tradeoff. */
if (str)
{
if (d != d) strcpy(str, "0");
else if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d);
else sprintf(str,"%.*e",(int)log10(d) + 6,d);
if (fabs(floor(d) - d) <= DBL_EPSILON) sprintf(str, "%.0f", d);
else sprintf(str, "%.*e", (int)log10(d) + 6, d);
}
}
}
return str;