json_fmt_real Function

private 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.

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in) :: v

Return Value character(len=:), allocatable


Called by

proc~~json_fmt_real~~CalledByGraph proc~json_fmt_real json_fmt_real proc~json_real_array json_real_array proc~json_real_array->proc~json_fmt_real proc~write_key_json write_key_json proc~write_key_json->proc~json_fmt_real proc~write_key_json->proc~json_real_array proc~schema_render_json nml_schema_t%schema_render_json proc~schema_render_json->proc~write_key_json

Variables

Type Visibility Attributes Name Initial
character(len=40), private :: buf

Source Code

   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