Real -> JSON number, robust to extreme magnitude (e.g. huge(wp)
floor/ceiling sentinels like kv_max’s default): fmt_real’s
E18.10 is too narrow for a 3-digit exponent and gfortran drops
the E literal to fit (“0.1797693135+309”), which is not valid
JSON. ES.E3 explicitly requests a 3-digit exponent field, which
forces the E to stay, at a width sized for the worst case. 16
fraction digits (17 significant figures) is real64’s round-trip
guarantee – one digit short (as an earlier version of this
function used) rounds huge(wp)’s text UP past the true maximum
finite double, so re-parsing it (e.g. Python’s json.load)
overflows to infinity, which is not valid JSON and breaks the
generator (tools/gen_python_config.py) that reads this file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=rk), | intent(in) | :: | v |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=40), | private | :: | buf |
pure function json_fmt_real(v) result(str) !! Real -> JSON number, robust to extreme magnitude (e.g. `huge(wp)` !! floor/ceiling sentinels like `kv_max`'s default): `fmt_real`'s !! `E18.10` is too narrow for a 3-digit exponent and gfortran drops !! the `E` literal to fit ("0.1797693135+309"), which is not valid !! JSON. `ES.E3` explicitly requests a 3-digit exponent field, which !! forces the `E` to stay, at a width sized for the worst case. 16 !! fraction digits (17 significant figures) is real64's round-trip !! guarantee -- one digit short (as an earlier version of this !! function used) rounds `huge(wp)`'s text UP past the true maximum !! finite double, so re-parsing it (e.g. Python's `json.load`) !! overflows to infinity, which is not valid JSON and breaks the !! generator (`tools/gen_python_config.py`) that reads this file. real(rk), intent(in) :: v character(len=:), allocatable :: str character(len=40) :: buf write (buf, "(ES26.16E3)") v str = trim(adjustl(buf)) end function json_fmt_real