Convert a string into a real(RK)
.
fmt=*
, rather than
fmt=real_fmt
, since it doesn’t work for some unusual cases
(e.g., when str='1E-5'
).Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK,len=*), | intent(in) | :: | str | the string to convert to a real |
||
logical(kind=LK), | intent(in) | :: | use_quiet_nan | if true, return NaN’s as |
||
real(kind=RK), | intent(out) | :: | rval |
|
||
logical(kind=LK), | intent(out) | :: | status_ok | true if there were no errors |
subroutine string_to_real(str,use_quiet_nan,rval,status_ok)
implicit none
character(kind=CK,len=*),intent(in) :: str !! the string to convert to a real
logical(LK),intent(in) :: use_quiet_nan !! if true, return NaN's as `ieee_quiet_nan`.
!! otherwise, use `ieee_signaling_nan`.
real(RK),intent(out) :: rval !! `str` converted to a real value
logical(LK),intent(out) :: status_ok !! true if there were no errors
integer(IK) :: ierr !! read iostat error code
read(str,fmt=*,iostat=ierr) rval
status_ok = (ierr==0)
if (.not. status_ok) then
rval = 0.0_RK
else
if (ieee_support_nan(rval)) then
if (ieee_is_nan(rval)) then
! make sure to return the correct NaN
if (use_quiet_nan) then
rval = ieee_value(rval,ieee_quiet_nan)
else
rval = ieee_value(rval,ieee_signaling_nan)
end if
end if
end if
end if
end subroutine string_to_real