From f18ad36b1bd22a3b2a0888472b46bed9c252e65e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 11 Oct 2023 00:58:00 -0500 Subject: [PATCH] Rework #1306 - better default for pretty printing numbers. Not perfect for serialization, but a representation that plays well with both safe integers (z where abs(z) < 2^54) and non-integer floats. --- src/core/pp.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/pp.c b/src/core/pp.c index 261fca042..89ecc141c 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -31,6 +31,7 @@ #include #include #include +#include /* Implements a pretty printer for Janet. The pretty printer * is simple and not that flexible, but fast. */ @@ -38,11 +39,15 @@ /* Temporary buffer size */ #define BUFSIZE 64 +/* Preprocessor hacks */ +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) + static void number_to_string_b(JanetBuffer *buffer, double x) { janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2); const char *fmt = (x == floor(x) && x <= JANET_INTMAX_DOUBLE && - x >= JANET_INTMIN_DOUBLE) ? "%.0f" : "%g"; + x >= JANET_INTMIN_DOUBLE) ? "%.0f" : ("%." STR(DBL_DIG) "g"); int count; if (x == 0.0) { /* Prevent printing of '-0' */