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

Code before:
            return 0;
        } else if (_iter_count > 10000) {
            // We are very likely stuck in a cyclic datastructure, so we assume this is
            // _not_ a Function.
            return 0;
        } else if (vt == jl_function_type) {
            return 1;
        }
        vt = vt->super;
        _iter_count += 1;
    }
    return 0;
}

static size_t jl_static_show_float(JL_STREAM *out, double v,
                                   jl_datatype_t *vt) JL_NOTSAFEPOINT
{
    size_t n = 0;
    // TODO: non-canonical NaNs do not round-trip
    // TOOD: BFloat16
    const char *size_suffix = vt == jl_float16_type ? "16" :
                              vt == jl_float32_type ? "32" :
                                                      "";
    // Digits required to print p significand bits: ceil(p * log(10, 2))
    //   Float16   4
    //   Float32   8
    //   Float64  17
    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) {
        // If no decimal point or exponent will be printed, we must add ".0"
        double ipart, fpart;
        fpart = modf(v, &ipart);
        int dotzero = v == 0 || (fpart == 0.0 && fabs(ipart) < 1e17);
        n += jl_printf(out, "%.17g%s", v, dotzero ? ".0" : "");
    }
    else if (vt == jl_float32_type) {
        n += jl_printf(out, "Float32(%.8g)", v);
    }
    else {
        assert(vt == jl_float16_type);
        n += jl_printf(out, "Float16(%.4g)", v);
    }
    return n;
}

// `v` might be pointing to a field inlined in a structure therefore

Review comment (suggestion):
These seem unlikely correct, since the standard given results are one higher for these:
https://github.com/JuliaLang/julia/pull/57977/files#r2028868985
```suggestion
    //   Float16   5
    //   Float32   9
```

Code after:
// This function checks if `vt <: Function` without triggering GC.
static int jl_static_is_function_(jl_datatype_t *vt) JL_NOTSAFEPOINT {
    if (!jl_function_type) {  // Make sure there's a Function type defined.
        return 0;
    }
    int _iter_count = 0;  // To prevent infinite loops from corrupt type objects.
    while (vt != jl_any_type) {
        if (vt == NULL) {
            return 0;
        } else if (_iter_count > 10000) {
            // We are very likely stuck in a cyclic datastructure, so we assume this is
            // _not_ a Function.
            return 0;
        } else if (vt == jl_function_type) {
            return 1;
        }
        vt = vt->super;
        _iter_count += 1;
    }
    return 0;
}

static size_t jl_static_show_float(JL_STREAM *out, double v,
                                   jl_datatype_t *vt) JL_NOTSAFEPOINT
{
    size_t n = 0;
    // 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'