avoid pointless digits when printing single-precision floats

Closes PR 12118
This commit is contained in:
Matthew Flatt 2011-08-18 13:20:48 -06:00
parent 39edc5a599
commit 1d6c3aa4ea

View File

@ -1566,11 +1566,15 @@ static char *double_to_string (double d, int alloc, int was_single)
#endif #endif
s = "0.0"; s = "0.0";
} else { } else {
/* Initial count for significant digits is 14. That's big enough to /* Initial count for significant digits is 14 (double) or 6 digits
get most right, small enough to avoid nonsense digits. But we'll (single). That's big enough to get most right, small enough to
loop in case it's not precise enough to get read-write invariance: */ avoid nonsense digits. But we'll loop in case it's not precise
enough to get read-write invariance: */
GC_CAN_IGNORE char *loc; GC_CAN_IGNORE char *loc;
digits = 14; if (was_single)
digits = 6;
else
digits = 14;
loc = scheme_push_c_numeric_locale(); loc = scheme_push_c_numeric_locale();
while (digits < 30) { while (digits < 30) {
double check; double check;
@ -1580,8 +1584,13 @@ static char *double_to_string (double d, int alloc, int was_single)
/* Did we get read-write invariance, yet? */ /* Did we get read-write invariance, yet? */
check = strtod(buffer, &ptr); check = strtod(buffer, &ptr);
if (check == d) if (was_single) {
break; if ((float)check == (float)d)
break;
} else {
if (check == d)
break;
}
digits++; digits++;
} }