miniwdl icon indicating copy to clipboard operation
miniwdl copied to clipboard

Standard library function `round()` does not round half numbers up.

Open stxue1 opened this issue 8 months ago • 0 comments

Half numbers in miniwdl are rounded down instead of rounding up, which differs with the spec: https://github.com/openwdl/wdl/blob/9c0b9cf4586508a9e6260cc5c5e562e21f625aac/SPEC.md?plain=1#L6279

Rounds a floating point number to the nearest integer based on standard rounding rules ("round half up").

The spec also has an example of this behavior (though the example json output is supposed to be [true, true] instead of true. https://github.com/openwdl/wdl/blob/9c0b9cf4586508a9e6260cc5c5e562e21f625aac/SPEC.md?plain=1#L6289-L6306

With the WDL workflow below:

version 1.1

workflow test_round {
  input {
    Int i1 = 2
  }

  Float f1 = i1 + 0.50
  Float f2 = i1 + 0.51
  
  output {
    Int round_f1 = round(f1)
    Int round_f2 = round(f2)
  }
}

The output is:

{
  "dir": "/home/heaucques/Documents/wdl-conformance-tests/20240701_180506_test_round",
  "outputs": {
    "test_round.round_f1": 2,
    "test_round.round_f2": 3
  }
}

Even though both integer outputs should be 3.

This may be due to how the round function is implemented. https://github.com/chanzuckerberg/miniwdl/blob/5ccead060fd3755df28d1e89d3d160eb7a5f3ee9/WDL/StdLib.py#L69

It looks like the standard library function depends on python's round function, and python rounds half numbers towards the "even" choice. https://docs.python.org/3/library/functions.html#round

Python 3.12.4 (main, Jun  8 2024, 18:29:57) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0

stxue1 avatar Jul 02 '24 01:07 stxue1