Skip to content

Commit 56604dd

Browse files
committed
fix: decide a zero spelling from the text, not the parse
Review found that testing `value == 0.0` discarded the digits of any value too small for a float. '0.' followed by 400 zeros and a 1 -- plain decimal text, exactly what a <floatstring> is for -- underflowed and rendered 0.0, silently a different number. The cutoff was an artifact of binary64 rather than anything in the template: 1e-320 is subnormal and survived, 1e-400 did not. Ask the text instead. An all-zero mantissa spells zero whatever the exponent, so '0.00', '-0.0' and '0e5' lose their sign and keep their digits, while '1e-400' and '0.000...1' keep both. Mirrors openjd_expr::value::text_spells_zero, so openjd-rs#354 renders every one of these identically. Verification. Model suite 5536 passed, 24 skipped, 3 xfailed; ruff, black and mypy clean. Full 2023-09 conformance 1162 passed, 0 failed. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent ccf4cba commit 56604dd

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,14 @@ def _validate_target_runtime_seconds(cls, value: Any, info: ValidationInfo) -> A
12491249
# digit, so '0.50' keeps the zero that is its integer part.
12501250
_REDUNDANT_LEADING_ZEROS = re.compile(r"^([+-]?)0+(?=[0-9])")
12511251

1252+
# An all-zero mantissa, whatever the exponent: '0.00', '-0.0' and '0e5' spell zero,
1253+
# '1e-400' does not. Mirrors openjd_expr::value::text_spells_zero.
1254+
_SPELLS_ZERO = re.compile(r"^[+-]?[0.]*[0][0.]*(?:[eE][+-]?[0-9]+)?$")
1255+
1256+
1257+
def _spells_zero(text: str) -> bool:
1258+
return _SPELLS_ZERO.match(text) is not None
1259+
12521260

12531261
def _normalized_range_element(elem: str, to_int: bool) -> Any:
12541262
"""The value an ``<intstring>``/``<floatstring>`` range element denotes.
@@ -1262,21 +1270,19 @@ def _normalized_range_element(elem: str, to_int: bool) -> Any:
12621270
return int(elem) # int() already drops leading zeros
12631271
# Parsed only to check it is a number; the text is what renders. Not a
12641272
# Decimal -- re-rendering one is context-sensitive and unbounded (§7.5).
1265-
value = float(elem)
1273+
float(elem)
12661274
except ValueError:
12671275
# A resolved format string can be non-numeric. Literals are checked at
12681276
# template parse time, so carry it through rather than rejecting here.
12691277
return elem
12701278
# Trimmed because the text reaches a command line and float() ignores
12711279
# surrounding whitespace; openjd-rs trims here too.
12721280
text = _REDUNDANT_LEADING_ZEROS.sub(r"\1", elem.strip())
1273-
if value == 0.0:
1274-
# Zero has no sign, so drop one without dropping the decimal places:
1275-
# '-0.00' renders `0.00`. Text that reaches zero only by underflow, like
1276-
# '1e-400', does not render the value at all and is not kept.
1277-
unsigned = text.lstrip("+-")
1278-
return unsigned if set(unsigned) <= {"0", "."} else "0.0"
1279-
return text
1281+
# Zero has no sign, so drop one without dropping the decimal places: '-0.00'
1282+
# renders `0.00`. Decided from the text, not from `value` -- the parse
1283+
# underflows to 0.0 below ~5e-324, and a tiny value's digits are exactly what
1284+
# a <floatstring> is for.
1285+
return text.lstrip("+-") if _spells_zero(text) else text
12801286

12811287

12821288
# Target model for task parameters when instantiating a job.

‎test/openjd/model_v0/v2023_09/test_parameter_space.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,12 @@ def test_intstring_elements_carry_their_value(self, param_type: str) -> None:
489489
# Dropping it must not drop the decimal places with it.
490490
pytest.param("-0.0", "0.0", id="negative zero has no sign"),
491491
pytest.param("-0.00", "0.00", id="unsigned, but still two places"),
492-
# Reaches zero only by underflow, so the text does not render the
493-
# value and is not kept.
494-
pytest.param("1e-400", "0.0", id="underflow to zero"),
492+
# An all-zero mantissa spells zero whatever the exponent.
493+
pytest.param("0E+2", "0E+2", id="exponent form of zero"),
494+
pytest.param("-0e5", "0e5", id="signed exponent form of zero"),
495+
# Underflow is not zero being spelled: the digits are the request.
496+
pytest.param("1e-400", "1e-400", id="underflow keeps its digits"),
497+
pytest.param("0." + "0" * 400 + "1", "0." + "0" * 400 + "1", id="tiny plain decimal"),
495498
# float() ignores surrounding whitespace and this text reaches a
496499
# command line, so it is trimmed rather than forwarded.
497500
pytest.param(" 1.5 ", "1.5", id="surrounding whitespace"),
@@ -524,10 +527,10 @@ def test_floatstring_elements_keep_their_scale(self, element: str, expected: str
524527
# characters, which is the whole point: nothing expands it.
525528
pytest.param("1e999999999", "1e999999999", id="huge positive exponent"),
526529
pytest.param("1E+1022", "1E+1022", id="positive exponent past the field cap"),
527-
# Too small, and the value underflows to zero, so the text no longer
528-
# renders it. openjd-rs resolves these to zero and renders 0.0 too.
529-
pytest.param("1e-999999999", "0.0", id="huge negative exponent"),
530-
pytest.param("1E-1023", "0.0", id="negative exponent past the field cap"),
530+
# Too small for a float either way, and the digits still stand:
531+
# underflowing to 0.0 is a parse limit, not the author writing zero.
532+
pytest.param("1e-999999999", "1e-999999999", id="huge negative exponent"),
533+
pytest.param("1E-1023", "1E-1023", id="negative exponent past the field cap"),
531534
),
532535
)
533536
def test_a_huge_exponent_costs_only_its_own_characters(

0 commit comments

Comments
 (0)