mirror of
https://github.com/pocketpy/pocketpy
synced 2026-06-20 23:47:10 +08:00
Compare commits
2 Commits
5b0c543c78
...
cee3228c61
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cee3228c61 | ||
|
|
803e82e2f9 |
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user