mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 00:10:27 +00:00
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.
This commit is contained in:
parent
cb25a2ecd6
commit
f18ad36b1b
@ -31,6 +31,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
/* Implements a pretty printer for Janet. The pretty printer
|
/* Implements a pretty printer for Janet. The pretty printer
|
||||||
* is simple and not that flexible, but fast. */
|
* is simple and not that flexible, but fast. */
|
||||||
@ -38,11 +39,15 @@
|
|||||||
/* Temporary buffer size */
|
/* Temporary buffer size */
|
||||||
#define BUFSIZE 64
|
#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) {
|
static void number_to_string_b(JanetBuffer *buffer, double x) {
|
||||||
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
||||||
const char *fmt = (x == floor(x) &&
|
const char *fmt = (x == floor(x) &&
|
||||||
x <= JANET_INTMAX_DOUBLE &&
|
x <= JANET_INTMAX_DOUBLE &&
|
||||||
x >= JANET_INTMIN_DOUBLE) ? "%.0f" : "%g";
|
x >= JANET_INTMIN_DOUBLE) ? "%.0f" : ("%." STR(DBL_DIG) "g");
|
||||||
int count;
|
int count;
|
||||||
if (x == 0.0) {
|
if (x == 0.0) {
|
||||||
/* Prevent printing of '-0' */
|
/* Prevent printing of '-0' */
|
||||||
|
Loading…
Reference in New Issue
Block a user