Skip to content

Commit

Permalink
floats on command-line always in decimal notation
Browse files Browse the repository at this point in the history
Never in scientific notation
  • Loading branch information
mr-c committed Apr 24, 2023
1 parent b4fd590 commit 539dd49
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cwltool/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import copy
import logging
import math
from typing import ( # pylint: disable=unused-import
from decimal import Decimal
from typing import (
IO,
TYPE_CHECKING,
Any,
Expand All @@ -28,6 +29,8 @@
from schema_salad.validate import validate

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.representer import RoundTripRepresenter
from ruamel.yaml.scalarfloat import ScalarFloat

from .errors import WorkflowException
from .loghandler import _logger
Expand Down Expand Up @@ -604,6 +607,12 @@ def tostr(self, value: Union[MutableMapping[str, str], Any]) -> str:
'{} object missing "path": {}'.format(value["class"], value)
)
return value["path"]
elif isinstance(value, ScalarFloat):
rep = RoundTripRepresenter()
dec_value = Decimal(rep.represent_scalar_float(value).value)
if "E" in str(dec_value):
return str(dec_value.quantize(1))
return str(dec_value)
else:
return str(value)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1829,3 +1829,14 @@ def test_res_req_expr_float_1_2() -> None:
assert exit_code == 0, stderr
assert json.loads(stdout)["result"]["outdirSize"] >= 708
assert json.loads(stdout)["result"]["tmpdirSize"] >= 708


def test_very_small_and_large_floats() -> None:
"""Confirm that very small or large numbers are not transformed into scientific notation."""
exit_code, stdout, stderr = get_main_output(
[
get_data("tests/wf/floats_small_and_large.cwl"),
]
)
assert exit_code == 0, stderr
assert json.loads(stdout)["result"] == "0.00001 0.0000123 123000 1230000"
39 changes: 39 additions & 0 deletions tests/wf/floats_small_and_large.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cwlVersion: v1.0
class: CommandLineTool
baseCommand: echo
requirements:
InlineJavascriptRequirement: {}

inputs:
annotation_prokka_evalue:
type: float
default: 0.00001
inputBinding: {}

annotation_prokka_evalue2:
type: float
default: 1.23e-05
inputBinding: {}

annotation_prokka_evalue3:
type: float
default: 1.23e5
inputBinding: {}

annotation_prokka_evalue4:
type: float
default: 1230000
inputBinding: {}


arguments: [ -n ]

stdout: dump

outputs:
result:
type: string
outputBinding:
glob: dump
loadContents: true
outputEval: $(self[0].contents)

0 comments on commit 539dd49

Please sign in to comment.