jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
333 stars 82 forks source link

Fixed an issue when IEEE floating-point rounding mode is IEEE_DOWN #545

Closed amanotk closed 3 months ago

amanotk commented 9 months ago

This solves an issue when IEEE floating-point rounding mode is manually set to IEEE_DOWN. The original code in the string_to_integer subroutine

        ndigits_digits = floor(log10(real(ndigits)))+1

erroneously returns 1 even when ndigits is 10 (while the correct value should be 2).

The following code is the minimum reproducible example of the problem. The last deserialize will result in a crush.

program jsontest
  use :: ieee_arithmetic
  use :: json_module

  type(json_file) :: file

  call ieee_set_rounding_mode(ieee_down) 

  call file%initialize()

  call file%deserialize('{"foo" : 100}')
  print *, "ok 1"
  call file%deserialize('{"foo" : 100, "bar" : 1000}')
  print *, "ok 2"
  call file%deserialize('{"foo" : 100, "bar" : 1000, "baz" : 10000}')
  print *, "ok 3"

  call file%destroy()

end program jsontest

I assume simply changing floor to nint should solve the issue without any side effects.

jacobwilliams commented 9 months ago

thanks! I'll take a look at this...

codecov[bot] commented 9 months ago

Codecov Report

Merging #545 (e4bfffc) into master (7819919) will not change coverage. The diff coverage is 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #545 +/- ## ======================================= Coverage 89.48% 89.48% ======================================= Files 7 7 Lines 5356 5356 ======================================= Hits 4793 4793 Misses 563 563 ```