Skip to content

Commit

Permalink
Standardize the output of tiny real values
Browse files Browse the repository at this point in the history
  Added code to harmonize differences in how different compilers format the
output of tiny (< 1e-99) or huge (>= 1e100) real values.  All answers are
bitwise identical, but output could change with the PGI compiler for any cases
that are writing out such extreme values.
  • Loading branch information
Hallberg-NOAA authored and marshallward committed Jan 7, 2023
1 parent 3cecac4 commit d7d1351
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/framework/MOM_document.F90
Original file line number Diff line number Diff line change
Expand Up @@ -637,19 +637,33 @@ function real_string(val)
elseif (val == 0.) then
real_string = "0.0"
else
if ((abs(val) <= 1.0e-100) .or. (abs(val) >= 1.0e100)) then
write(real_string(1:32), '(ES24.14E3)') val
if (.not.testFormattedFloatIsReal(real_string,val)) &
write(real_string(1:32), '(ES24.15E3)') val
if ((abs(val) < 1.0e-99) .or. (abs(val) >= 1.0e100)) then
write(real_string(1:32), '(ES24.14E4)') val
if (scan(real_string, "eE") == 0) then ! Fix a bug with a missing E in PGI formatting
ind = scan(real_string, "-+", back=.true.)
if (ind > index(real_string, ".") ) & ! Avoid changing a leading sign.
real_string = real_string(1:ind-1)//"E"//real_string(ind:)
endif
if (.not.testFormattedFloatIsReal(real_string, val)) then
write(real_string(1:32), '(ES25.15E4)') val
if (scan(real_string, "eE") == 0) then ! Fix a bug with a missing E in PGI formatting
ind = scan(real_string, "-+", back=.true.)
if (ind > index(real_string, ".") ) & ! Avoid changing a leading sign.
real_string = real_string(1:ind-1)//"E"//real_string(ind:)
endif
endif
! Remove a leading 0 from the exponent, if it is there.
ind = max(index(real_string, "E+0"), index(real_string, "E-0"))
if (ind > 0) real_string = real_string(1:ind+1)//real_string(ind+3:)
else
write(real_string(1:32), '(ES23.14)') val
if (.not.testFormattedFloatIsReal(real_string,val)) &
if (.not.testFormattedFloatIsReal(real_string, val)) &
write(real_string(1:32), '(ES23.15)') val
endif
do
ind = index(real_string,"0E")
do ! Remove extra trailing 0s before the exponent.
ind = index(real_string, "0E")
if (ind == 0) exit
if (real_string(ind-1:ind-1) == ".") exit
if (real_string(ind-1:ind-1) == ".") exit ! Leave at least one digit after the decimal point.
real_string = real_string(1:ind-1)//real_string(ind+1:)
enddo
endif
Expand Down

0 comments on commit d7d1351

Please sign in to comment.