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

@ -126,15 +126,23 @@ static char *print_number(cJSON *item)
if (str) sprintf(str,"%d",item->valueint); if (str) sprintf(str,"%d",item->valueint);
} }
else else
{
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. */ str = (char*)cJSON_malloc(64 + (int)log10(fabs(d))); /* This is a nice tradeoff. */
if (str) if (str)
{ {
if (d != d) strcpy(str, "0"); if (fabs(floor(d) - d) <= DBL_EPSILON) sprintf(str, "%.0f", d);
else if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d);
else sprintf(str, "%.*e", (int)log10(d) + 6, d); else sprintf(str, "%.*e", (int)log10(d) + 6, d);
} }
} }
}
return str; return str;
} }