Stringify floats with more digits

This commit is contained in:
K Lange 2021-02-20 21:48:47 +09:00
parent a5ff538dc1
commit 379e1846a9
3 changed files with 30 additions and 31 deletions

View File

@ -62,7 +62,10 @@ KRK_METHOD(float,__float__,{ return argv[0]; })
KRK_METHOD(float,__str__,{ KRK_METHOD(float,__str__,{
char tmp[100]; char tmp[100];
size_t l = snprintf(tmp, 100, "%g", self); size_t l = snprintf(tmp, 97, "%.16g", self);
if (!strstr(tmp,".") && !strstr(tmp,"e")) {
l = snprintf(tmp,100,"%.16g.0",self);
}
return OBJECT_VAL(krk_copyString(tmp, l)); return OBJECT_VAL(krk_copyString(tmp, l));
}) })

View File

@ -28,33 +28,6 @@ void krk_freeValueArray(KrkValueArray * array) {
} }
void krk_printValue(FILE * f, KrkValue printable) { void krk_printValue(FILE * f, KrkValue printable) {
if (!IS_OBJECT(printable)) {
switch (printable.type) {
case VAL_INTEGER: fprintf(f, PRIkrk_int, AS_INTEGER(printable)); break;
case VAL_BOOLEAN: fprintf(f, "%s", AS_BOOLEAN(printable) ? "True" : "False"); break;
case VAL_FLOATING: fprintf(f, "%g", AS_FLOATING(printable)); break;
case VAL_NONE: fprintf(f, "None"); break;
case VAL_HANDLER: fprintf(f, "{%s->%d}", AS_HANDLER(printable).type == OP_PUSH_TRY ? "try" : "with", (int)AS_HANDLER(printable).target); break;
case VAL_KWARGS: {
if (AS_INTEGER(printable) == LONG_MAX) {
fprintf(f, "{unpack single}");
} else if (AS_INTEGER(printable) == LONG_MAX-1) {
fprintf(f, "{unpack list}");
} else if (AS_INTEGER(printable) == LONG_MAX-2) {
fprintf(f, "{unpack dict}");
} else if (AS_INTEGER(printable) == LONG_MAX-3) {
fprintf(f, "{unpack nil}");
} else if (AS_INTEGER(printable) == 0) {
fprintf(f, "{unset default}");
} else {
fprintf(f, "{sentinel=" PRIkrk_int "}",AS_INTEGER(printable));
}
break;
}
default: break;
}
return;
}
KrkClass * type = krk_getType(printable); KrkClass * type = krk_getType(printable);
if (type->_tostr) { if (type->_tostr) {
krk_push(printable); krk_push(printable);
@ -75,7 +48,30 @@ void krk_printValue(FILE * f, KrkValue printable) {
void krk_printValueSafe(FILE * f, KrkValue printable) { void krk_printValueSafe(FILE * f, KrkValue printable) {
if (!IS_OBJECT(printable)) { if (!IS_OBJECT(printable)) {
krk_printValue(f,printable); switch (printable.type) {
case VAL_INTEGER: fprintf(f, PRIkrk_int, AS_INTEGER(printable)); break;
case VAL_BOOLEAN: fprintf(f, "%s", AS_BOOLEAN(printable) ? "True" : "False"); break;
case VAL_FLOATING: fprintf(f, "%.16g", AS_FLOATING(printable)); break;
case VAL_NONE: fprintf(f, "None"); break;
case VAL_HANDLER: fprintf(f, "{%s->%d}", AS_HANDLER(printable).type == OP_PUSH_TRY ? "try" : "with", (int)AS_HANDLER(printable).target); break;
case VAL_KWARGS: {
if (AS_INTEGER(printable) == LONG_MAX) {
fprintf(f, "{unpack single}");
} else if (AS_INTEGER(printable) == LONG_MAX-1) {
fprintf(f, "{unpack list}");
} else if (AS_INTEGER(printable) == LONG_MAX-2) {
fprintf(f, "{unpack dict}");
} else if (AS_INTEGER(printable) == LONG_MAX-3) {
fprintf(f, "{unpack nil}");
} else if (AS_INTEGER(printable) == 0) {
fprintf(f, "{unset default}");
} else {
fprintf(f, "{sentinel=" PRIkrk_int "}",AS_INTEGER(printable));
}
break;
}
default: break;
}
} else if (IS_STRING(printable)) { } else if (IS_STRING(printable)) {
fprintf(f, "'"); fprintf(f, "'");
/* /*

View File

@ -1,5 +1,5 @@
1.90665 1.906645911848772
1.90665 1.906645911848772
246 246
246 246
123 123