Repository: JuliaLang/julia
File: src/rtutils.c (C)

Code before:
    // TODO: non-canonical NaNs do not round-trip
    // TOOD: BFloat16
    const char *size_suffix = vt == jl_float16_type ? "16" :
                              vt == jl_float32_type ? "32" :
                                                      "";
    // Requires minimum 1 (sign) + 17 (sig) + 1 (dot) + 5 ("e-123") + 1 (null)
    char buf[32];
    // Base B significand digits required to print n base-b significand bits
    // (including leading 1):  N = 2 + floor(n/log(b, B))
    //   Float16   5
    //   Float32   9
    //   Float64  17
    // REF: https://dl.acm.org/doi/pdf/10.1145/93542.93559
    if (isnan(v)) {
        n += jl_printf(out, "NaN%s", size_suffix);
    }
    else if (isinf(v)) {
        n += jl_printf(out, "%sInf%s", v < 0 ? "-" : "", size_suffix);
    }
    else if (vt == jl_float64_type) {
        size_t m = snprintf(buf, sizeof buf, "%.17g", v);
        jl_uv_puts(out, buf, m);
        n += m;
        // If there is no decimal point or exponent, disambiguate
        if (!strpbrk(buf, ".e"))
            jl_printf(out, ".0");
    }
    else if (vt == jl_float32_type) {
        size_t m = snprintf(buf, sizeof buf, "%.9g", v);
        // If the exponent was printed, replace it with 'f'
        char *p = memchr(buf, 'e', m);
        if (p)
            *p = 'f';
        jl_uv_puts(out, buf, m);
        // If no exponent was printed, we must add one
        if (!p)
            jl_printf(out, "f0");
    }
    else {
        assert(vt == jl_float16_type);
        n += jl_printf(out, "Float16(%.5g)", v);
    }
    return n;
}

// `v` might be pointing to a field inlined in a structure therefore
// `jl_typeof(v)` may not be the same with `vt` and only `vt` should be
// used to determine the type of the value.
// This is necessary to make sure that this function doesn't allocate any
// memory through the Julia GC
static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt,

Review comment (suggestion):
While clever, would it be better to use the standard flag `#` for this to preserve all trailing zeros, which will end with simply `.` (rather than to imply the trailing digit is `.0` which may not be entirely true given final digit rounding, though I think it would have to round-trip the same since 17 digits is full accuracy)
```suggestion
        n += jl_printf(buf, sizeof buf, "%#.17g", v);
```

Code after:
    // TODO: non-canonical NaNs do not round-trip
    // TOOD: BFloat16
    const char *size_suffix = vt == jl_float16_type ? "16" :
                              vt == jl_float32_type ? "32" :
                                                      "";
    // Requires minimum 1 (sign) + 17 (sig) + 1 (dot) + 5 ("e-123") + 1 (null)
    char buf[32];
    // Base B significand digits required to print n base-b significand bits
    // (including leading 1):  N = 2 + floor(n/log(b, B))
    //   Float16   5
    //   Float32   9
    //   Float64  17
    // REF: https://dl.acm.org/doi/pdf/10.1145/93542.93559
    if (isnan(v)) {
        n += jl_printf(out, "NaN%s", size_suffix);
    }
    else if (isinf(v)) {
        n += jl_printf(out, "%sInf%s", v < 0 ? "-" : "", size_suffix);
    }
    else if (vt == jl_float64_type) {
        n += jl_printf(out, "%#.17g", v);
    }
    else if (vt == jl_float32_type) {
        size_t m = snprintf(buf, sizeof buf, "%.9g", v);
        // If the exponent was printed, replace it with 'f'
        char *p = (char *)memchr(buf, 'e', m);
        if (p)
            *p = 'f';
        jl_uv_puts(out, buf, m);
        n += m;
        // If no exponent was printed, we must add one
        if (!p)
            n += jl_printf(out, "f0");
    }
    else {
        assert(vt == jl_float16_type);
        n += jl_printf(out, "Float16(%#.5g)", v);
    }
    return n;
}

// `v` might be pointing to a field inlined in a structure therefore
// `jl_typeof(v)` may not be the same with `vt` and only `vt` should be
// used to determine the type of the value.
// This is necessary to make sure that this function doesn't allocate any
// memory through the Julia GC
static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt,
                                struct recur_list *depth, jl_static_show_config_t ctx) JL_NOTSAFEPOINT
{
    size_t n = 0;
    if ((uintptr_t)vt < 4096U) {