Compare commits

...

2 Commits

View File

@ -7,8 +7,6 @@
#include <stdarg.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void c11_sbuf__ctor(c11_sbuf* self) {
@ -55,24 +53,18 @@ void c11_sbuf__write_f64(c11_sbuf* self, double val, int precision) {
char b[32];
int size;
if(precision < 0) {
// Pick the shortest decimal width that round-trips to the same binary double.
for(int prec = 15; prec <= 17; prec++) {
size = snprintf(b, sizeof(b), "%.*g", prec, val);
double parsed = strtod(b, NULL);
if(memcmp(&parsed, &val, sizeof(double)) == 0) break;
}
size = snprintf(b, sizeof(b), "%.17g", val);
c11_sbuf__write_cstr(self, b);
bool all_is_digit = true;
for(int i = 1; i < size; i++) {
if(!isdigit(b[i])) {
if(!isdigit((unsigned char)b[i])) {
all_is_digit = false;
break;
}
}
c11_sbuf__write_cstr(self, b);
if(all_is_digit) c11_sbuf__write_cstr(self, ".0");
} else {
int prec = precision;
size = snprintf(b, sizeof(b), "%.*f", prec, val);
size = snprintf(b, sizeof(b), "%.*f", precision, val);
c11_sbuf__write_cstr(self, b);
}
}